وحدة:قيمة ويكي بيانات/ملعب

local p = {}
local getArgs = require('Module:Arguments with aliases').getArgs
local Wikidata2

local aliases = {
	use_sandbox = { "use_sandbox", "ملعب" },
	barlist = { "قائمة شريطية" },
	list = { "قائمة" },
	new_line = { "فاصل سطر" },
	no_wd = { "ويكي بيانات" },
	sort_before_filter = { "sort_before_filter" },

	entityId = { "صفحة", "id", "معرف ويكي بيانات", "qid", "entityId" },
	is_number = { "رقم", "is_number" },
	page = { "مقالة", "page" },
	noref = { "مراجع", "noref", "مراجع ويكي بيانات" },
	numberofreferences = { "numberofreferences", "عدد المراجع" },

	property = { "خاصية", "property", "pid" },
	otherproperty = { "خاصية أخرى", "otherproperty" },
	property2 = { "property2" },
	otherproperty2 = { "خاصية أخرى2", "otherproperty2" },
	property3 = { "property3" },
	propertyimage = { "propertyimage" },
	property1 = { "خاصية قيمة", "property1" },
	property1rank = { "property1rank" },
	property1after = { "property1after" },
	property1formatting = { "property1formatting" },
	property1pref = { "سابقة خاصية قيمة", "property1pref" },
	property1suff = { "لاحقة خاصية قيمة", "property1suff" },
	formatcharacters = { "formatcharacters" },
	addTrackingCat = { "addTrackingCat" },
	category = { "تصنيف", "category" },
	noicon = { "noicon", "رمز", "إيقونة" },
	nocate = { "دون تصنيف", "nocate" },
	icon2 = { "icon2" },
	urllabel = { "urllabel" },
	center = { "center" },
	image = { "صورة", "image" },
	size = { "حجم صورة", "size" },
	separator = { "separator" },
	conjunction = { "conjunction" },
	formatting = { "formatting" },
	NoPropValue = { "تصنيف 3", "تصنيف3", "بديل" },
	limit = { "limit" },
	showlang = { "showlang" },
	langpref = { "langpref" },
	justthisqual = { "تصفية فقط", "justthisqual" },
	bothdates = { "بداية ونهاية", "bothdates" },
	withdate = { "نقطة", "withdate" },

	modifytime = { "modifytime" },
	modifyqualifiertime = { "modifyqualifiertime", "تنسيق تاريخ" },

	enlabelcate = { "enlabelcate", "use_en_labels" },
	qual1 = { "تصفية" },
	qual1pref = { "سابقة تصفية", "qual1pref" },
	qual1a = { "تصفية1", "qual1a" },
	qp1apref = { "سابقة تصفية1", "qp1apref" },
	qual2 = { "تصفية2", "qual2" },
	qual2pref = { "سابقة تصفية2", "qual2pref" },
	qual3 = { "تصفية3", "qual3" },
	qual3pref = { "سابقة تصفية3", "qual3pref" },
	qual4 = { "تصفية4", "qual4" },
	qual4pref = { "سابقة تصفية4", "qual4pref" },
	qual5 = { "تصفية5", "qual5" },
	qual5pref = { "سابقة تصفية5", "qual5pref" },
	sortbytime = { "ترتيب", "sortbytime" },
	option1 = { "خيار", "option1" },
	option1value = { "قيمة خيار", "option1value" },
	value_module = { "value-module" },
	value_function = { "value-function" },

	qualifierprefix = { "بادئة تصفيات", "qualifierprefix" },
	qualifiersuffix = { "لاحقة تصفيات", "qualifiersuffix" },

	unitshort = { "unitshort" },
	prefervalue = { "prefervalue" },
	avoidvalue = { "avoidvalue" },
	avoidqualifier = { "تجاهل تصفية", "avoidqualifier" },
	preferqualifiervalue = { "preferqualifiervalue" },
	preferqualifier = { "preferqualifier" },

	mainprefix = { "سابقة", "mainprefix", "بادئة" },
	mainsuffix = { "لاحقة", "mainsuffix" },
	mainsuffixAfterIcon = { "mainsuffixAfterIcon", "لاحقة2" },

	full_link = { "وصلة كاملة" },
	rank = { "rank" },
	firstvalue = { "firstvalue", "واحد فقط" },
	pattern = { "pattern", "نمط" },

	link = { "وصلة" },
	linkname = { "تسمية وصلة" },

	illwd2y = { "illwd2y" },
	illwd2 = { "illwd2" }
}

local function anyvalid(x)
	if x and x ~= "" then return x end
	return nil
end
local function isvalid(x)
	if x and x ~= "" and x ~= "لا" and x ~= "no" then return x end
	return nil
end

local function switch_qualls(property, value, props)
	--local property
	if isvalid(value) then
		return value
	end
	if property:upper():sub(1, 1) == "P" then
		property = property:sub(2)
		property = tonumber(property)
	end

	return props[property] or ""
end

local function switch_contry(property, value, props)
	if property:upper():sub(1, 1) == "P" then
		property = property:sub(2)
		property = tonumber(property)
	end

	for i, v in pairs(props) do
		if v == property then
			return value
		end
	end

	return ""
end

local function AddArgs(args)
	local contries = { 17, 1532, 27, 495 }
	local property = args.property

	if not isvalid(args.formatting) then
		args.formatting = switch_contry(args.property, "sitelink", { 1811, 1881 })
	end

	if not isvalid(args.property1) then
		args.property1 = switch_contry(property, "P41", contries)
	end
	if not isvalid(args.image) then
		args.image = switch_contry(property, "image", contries)
	end
	if not isvalid(args.size) then
		args.size = switch_contry(property, "20", contries)
	end
	if not isvalid(args.bothdates) then
		args.bothdates = switch_contry(property, "true", { 26, 551, 27, 69, 1376, 1532, 17, 495, 102, 1451 })
	end
	if not isvalid(args.onlyvaluewithref) then
		args.onlyvaluewithref = switch_contry(property, "t", { 172, 140, 91, 1853 })
	end

	-- value-module / value-function
	local value_module = switch_qualls(property, args.value_module, {
		[106] = "Wikidata2/P106",
		[511] = "Wikidata2/P106",
		[413] = "Wikidata2/P106",
		[172] = "Wikidata2/P172",
	})
	local value_function = switch_qualls(property, args.value_function, {
		[106] = "formatEntityWithGenderClaim",
		[511] = "formatEntityWithGenderClaim",
		[413] = "formatEntityWithGenderClaim",
		[172] = "get_P172_lab",
	})
	if isvalid(value_module) and isvalid(value_function) then
		args["value-module"] = value_module
		args["value-function"] = value_function
	end

	args.qual1 = switch_qualls(property, args.qual1, {
		[856] = "P407",   -- لغة الموقع الرسمي
		[159] = "P17",    -- بلد مقر رسمي
		[1120] = "P2868", -- خاصية دور شخص
		[1561] = "P2868", -- خاصية دور شخص
		[1132] = "P2868", -- خاصية دور شخص
	})

	-- {{#تبديل:{{lc:{{{خاصية|{{{property|}}} }}}}}| p27| p1532| p17| p495=y}}
	local sw = switch_contry(property, "true", { 27, 1532, 17, 495 })

	if isvalid(args.list) or isvalid(args.withdate) or isvalid(args.bothdates) or isvalid(args.new_line)
		or isvalid(sw)
	then
		args.separator = "br"
		args.conjunction = "br"
	elseif args.separator == "empty" then
		args.separator = ""
		args.conjunction = ""
	else
		args.separator = isvalid(args.separator) or "، و"
		args.conjunction = isvalid(args.conjunction) or "، "
	end

	if not isvalid(args.barlist) and not isvalid(args["claim-module"]) and not isvalid(args["property-module"]) then
		args.hidden = isvalid(args.hidden) or "t"
	end
	
	return args
end

local function parseArgs(args)
	-- dont use isvalid for it because some use as | مراجع = لا
	if anyvalid(args.noref) then
		args.noref = "t"
	end

	args.modifytime = isvalid(args.modifytime) or "dmy"
	args.modifyqualifiertime = isvalid(args.modifyqualifiertime) or "dmy"

	if not isvalid(args.property1rank) then args.property1rank = "all" end

	if not anyvalid(args.addTrackingCat) then args.addTrackingCat = "yes" end

	if not isvalid(args.illwd2) then args.illwd2 = "t" end

	if not isvalid(args.firstvalue) and isvalid(args.full_link) then
		args.firstvalue = "true"
	end

	if not isvalid(args.rank) then
		args.rank = "best"
	end

	if isvalid(args.pattern) then
		if args.pattern == "وصلة" then args.pattern = "autourl2" end
	else
		if not isvalid(args.full_link) and not isvalid(args.is_number) and isvalid(args.link) then
			args.pattern = "[" .. args.link .. " " .. (args.linkname or "$1") .. "]"
		end
	end

	-- قائمة شريطية
	--[[
	if isvalid(args.barlist) then
		args.mainprefix =
			'<div style="max-height:180px; overflow: auto; padding: 2px 0 2px 5px; background-color:transparent;vertical-align:top;font-size:100%">' ..
			(args.mainprefix or "")
		args.mainsuffixAfterIcon = (args.mainsuffixAfterIcon or "") .. "</div>"
	end
	]]

	return args
end

function p.main(frame)
	local options = getArgs(frame, { aliases = aliases })

	-- لا تستخدم ويكي بيانات
	if anyvalid(options.no_wd) then
		return ""
	end

	-- إيجاد qid
	if not isvalid(options.entityId) then
		if isvalid(options.page) then
			options.entityId = mw.wikibase.getEntityIdForTitle(options.page)
		else
			options.entityId = mw.wikibase.getEntityIdForCurrentPage()
		end
	end

	if not isvalid(options.property) or not isvalid(options.entityId) then
		return ""
	end

	options.NosValue = options.NoPropValue
	options.showlang = "t"

	options = parseArgs(options)

	options = AddArgs(options)

	local sandbox = ""

	if isvalid(options.use_sandbox) then
		sandbox = "/ملعب"
	end

	Wikidata2 = require('Module:Wikidata2' .. sandbox).formatStatementsFromLua
	local result = Wikidata2(options)

	return result
end

return p