local addonName, ApiVault = ...

ApiVault.Examples = ApiVault.Examples or {}

ApiVault.Examples.defaults = {
	["C_AddOns.DoesAddOnExist"] = { "ApiVault" },
	["C_AddOns.GetAddOnMetadata"] = { "ApiVault", "Version" },
	["C_Item.IsEquippableItem"] = { "6948" },
	["C_Item.DoesItemExistByID"] = { "6948" },
	["C_Container.GetContainerItemInfo"] = { "0", "1" },
}

function ApiVault.Examples:GetDefaults(fullName, args)
	local values = self.defaults[fullName]
	if values then
		return values
	end
	values = {}
	for index, arg in ipairs(args or {}) do
		local argType = tostring(arg.t or arg.Type or "")
		local argName = tostring(arg.n or arg.Name or "")
		local lowerName = string.lower(argName)
		local lowerType = string.lower(argType)
		if string.find(lowerType, "bool", 1, true) then
			values[index] = "false"
		elseif string.find(lowerType, "number", 1, true) or string.find(lowerType, "luaindex", 1, true) or string.find(lowerName, "index", 1, true) or string.find(lowerName, "id", 1, true) then
			values[index] = "0"
		elseif string.find(lowerType, "unit", 1, true) then
			values[index] = "player"
		elseif string.find(lowerType, "cstring", 1, true) or string.find(lowerType, "string", 1, true) then
			values[index] = ""
		else
			values[index] = ""
		end
	end
	return values
end

function ApiVault.Examples:BuildSnippet(entry, systemInfo, objectInfo)
	if not entry or not objectInfo then
		return ""
	end
	if entry.kind == "Function" then
		local fullName = entry.display
		local argNames = {}
		for index, arg in ipairs(objectInfo.args or {}) do
			argNames[index] = arg.n or ("arg" .. index)
		end
		local args = table.concat(argNames, ", ")
		return "local ok, result1, result2, result3 = pcall(function()\n" ..
			"    return " .. fullName .. "(" .. args .. ")\n" ..
			"end)\n\n" ..
			"if ok then\n" ..
			"    print(result1, result2, result3)\n" ..
			"else\n" ..
			"    print(result1)\n" ..
			"end"
	elseif entry.kind == "Event" then
		local eventName = objectInfo.literal or objectInfo.n or entry.display
		return "local frame = CreateFrame(\"Frame\")\n" ..
			"frame:RegisterEvent(\"" .. eventName .. "\")\n" ..
			"frame:SetScript(\"OnEvent\", function(_, event, ...)\n" ..
			"    print(event, ...)\n" ..
			"end)"
	elseif entry.kind == "Table" then
		return "-- Documentation table: " .. entry.display .. "\n-- Use the field list shown in ApiVault."
	end
	return ""
end
