require "Амодуль:No globals"

local p = {
	common = require "Амодуль:Functions",
	props = {
		begin = { 'P569', 'P580' },
		ending = { 'P570', 'P582' },
		lang = { 'P364', 'P407' },
		point = { 'P571', 'P577', 'P585' },
	},
	datatypeToValueType = {
		['commonsMedia'] = 'string',
		['external-id'] = 'string',
		['geo-shape'] = 'string',
		['globe-coordinate'] = 'globecoordinate',
		['math'] = 'string',
		['monolingualtext'] = 'monolingualtext',
		['musical-notation'] = 'string',
		['quantity'] = 'quantity',
		['score'] = 'string',
		['string'] = 'string',
		['tabular-data'] = 'string',
		['time'] = 'time',
		['url'] = 'string',
		['wikibase-item'] = 'wikibase-entityid',
		['wikibase-property'] = 'wikibase-entityid',
		['wikibase-lexeme'] = 'wikibase-entityid',
		['wikibase-form'] = 'wikibase-entityid',
		['wikibase-sense'] = 'wikibase-entityid',
	},
}

local i18n = mw.loadData("Амодуль:Wikidata/i18n")

function p.addWdClass(str)
	return '<span class="wd">' .. str .. '</span>'
end

function p.category(key, ...)
	local Category = require 'Амодуль:Category'
	local title = mw.title.getCurrentTitle()
	if i18n.categories[key] ~= '-' then
		return Category.makeCategory(mw.ustring.format(i18n.categories[key], ...), '0,14', title.text)
	else
		return ''
	end
end

function p.getInterwikiPrefix(wiki)
	local prefixMap = {
		wiki = 'w:',
		wikibooks = 'b:',
		wikidata = 'd:',
		wikinews = 'n:',
		wikipedia = 'w:',
		wikiquote = 'q:',
		wikisource = 's:',
		wikiversity = 'v:',
		wikivoyage = 'voy:',
		wiktionary = 'wikt:',
	}
	if prefixMap[wiki] then
		return prefixMap[wiki]
	end
	local code, family = string.match(wiki, '^(.+)(wik.-)$')
	if prefixMap[code] and family == 'wiki' then
		return prefixMap[code]
	end
	if not code then
		code = wiki
	end
	local prefix = string.gsub(code, '_', '-') .. ':'
	return (prefixMap[family] or '') .. prefix
end

function p.augmentArgs(args, defaults, prefix)
	local out = {}
	prefix = prefix or ''
	setmetatable(out, {
		__index = function (t, key)
			if args[prefix .. key] ~= nil then
				return args[prefix .. key]
			end
			return defaults[key]
		end,
	})
	return out
end

function p.formatDateRange(snaks, options)
	local Formatters = require 'Амодуль:Wikidata/Formatters'
	local Y = require('Амодуль:Time').PRECISION.YEAR
	local defaults = { precision = Y }
	local options = p.augmentArgs(options, defaults)

	local begin_raw, ending_raw
	if snaks.begin then
		begin_raw = Formatters.getRawValue(snaks.begin, options)
	end
	if snaks.ending then
		ending_raw = Formatters.getRawValue(snaks.ending, options)
	end
	if not begin_raw or begin_raw == 'novalue' then
		if not ending_raw or ending_raw == 'novalue' then
			return ''
		end
		return mw.ustring.format(options['end-format'] or i18n.date['end'],
			Formatters.formatRawValue(ending_raw, 'time', options))
	end
	if not ending_raw or ending_raw == 'novalue' then
		return mw.ustring.format(options['begin-format'] or i18n.date['start'],
			Formatters.formatRawValue(begin_raw, 'time', options))
	end

	local begin, ending
	local connector = ' – '
	if begin_raw ~= 'somevalue' and ending_raw ~= 'somevalue' then
		local begin_precision = math.min(options.precision, begin_raw.precision)
		local ending_precision = math.min(options.precision, ending_raw.precision)
		local showera = {
			begin_raw.year <= 0 and ending_raw.year > 0,
			begin_raw.year <= 0 or ending_raw.year < 0,
		}
		while true do
			-- TODO: implement merging (1st - 2nd January 2020, 3rd January - 4th February 2020, etc.)
			defaults.precision = begin_precision
			begin = Formatters.formatRawValue(begin_raw, 'time', options)
			defaults.precision = ending_precision
			ending = Formatters.formatRawValue(ending_raw, 'time', options)
			if begin ~= ending then
				-- this must happen after equality test
				defaults.precision = begin_precision
				defaults.showera = showera[1]
				begin = Formatters.formatRawValue(begin_raw, 'time', options)
				defaults.precision = ending_precision
				defaults.showera = showera[2]
				ending = Formatters.formatRawValue(ending_raw, 'time', options)
				break
			end
			-- assumption: if the dates are same, they have the same precision
			if begin_precision == begin_raw.precision or ending_precision == ending_raw.precision then
				break
			end
			begin_precision = begin_precision + 1
			ending_precision = ending_precision + 1
		end
		if begin_precision == Y and ending_precision == Y and not showera[1] then
			connector = '–'
		end
	else
		begin = Formatters.formatRawValue(begin_raw, 'time', options)
		ending = Formatters.formatRawValue(ending_raw, 'time', options)
	end
	if begin == ending then
		return begin
	end
	return table.concat( { begin, ending }, connector )
end

function p.formatError(key, ...)
	return mw.ustring.format(i18n.errors[key], ...)
end

function p.formatFromPattern(str, pattern)
	local escaped = mw.ustring.gsub(str, '%%', '%%%%')
	return mw.ustring.gsub(pattern, '$1', escaped) .. '' --Hack to get only the first result of the function
end

function p.formatTextInLanguage(text, language)
	return mw.text.tag('span', { lang = language }, text)
end

-- @deprecated
function p.getEntityIdFromValue(value)
	local entityType = value['entity-type']
	if entityType == 'item' then
		return 'Q' .. value['numeric-id']
	elseif entityType == 'property' then
		return 'P' .. value['numeric-id']
	else
		return error(p.formatError('unknown-entity-type', entityType))
	end
end

function p.getItemIdFromURI(uri)
	return mw.ustring.match(uri, '(Q%d+)')
end

function p.getLabelInLanguage(entityId, langs)
	langs = p.textToTable(langs)
	local label, lang = mw.wikibase.getLabelWithLang(entityId)
	if label then
		for _, lg in ipairs(langs) do
			if lg == lang then
				return label, lang
			end
		end
	end
	return nil, nil
end

function p.getLinkWhenNonexistingLabel(entityId)
	local ImageFormatter = require 'Амодуль:ImageFormatter'
	return ImageFormatter.makeImage('Wikidata-edit.svg', {
		description = i18n['missing-label'],
		link = 'd:' .. entityId,
		size = '27x17px'
	}) .. '<code>[[d:' .. entityId .. '|' .. entityId .. ']]</code>' .. p.category('missing-label')
end

function p.IsOptionTrue(options, key)
	if options[key] then
		if tostring(options[key]) == 'true' or tostring(options[key]) == 'yes' or tostring(options[key]) == '1' then
			return true
		end
	end
	return false
end

function p.isPropertyId(value)
	return mw.ustring.match(value, '^[Pp][1-9]%d-$') and true
end

function p.IsSnakValue(snak)
	return snak.snaktype == 'value'
end

function p.raiseInvalidDatatype(method, allowed, provided)
	if type(allowed) ~= 'table' then
		allowed = { allowed }
	end
	return p.formatError('invalid-datatype2', method, mw.text.listToText(allowed, '“, „', '“ nebo „'), provided)
end

function p.simpleCompare(first, second)
	if first == second then
		return 0
	end
	if first < second then
		return -1
	else
		return 1
	end
end

function p.textToTable(something, options)
	if type(something) ~= "table" then
		local options = options or {}
		local split_pattern = options.split_pattern or "%s*,%s*"
		something = mw.text.split(something, split_pattern)
	end

	return p.common.cleanArgs(something)
end

return p