require 'Modul:No globals'

local p = {}

local lib = require 'Modul:Wikidata/lib'

local function findPattern(property)
	local Statements = mw.wikibase.getBestStatements(property:upper(), 'P1630')
	for _, statement in ipairs(Statements) do
		if lib.IsSnakValue(statement.mainsnak) then
			return statement.mainsnak.datavalue.value
		end
	end
	return nil
end

function p.getRawValue(value, options)
	return value
end

function p.formatValue(value, options)
	if options.pattern then
		return lib.formatFromPattern(value, options.pattern)
	elseif lib.IsOptionTrue(options, 'autoformat') then
		local pattern = findPattern(options.property)
		if pattern then
			return lib.formatFromPattern(value, mw.ustring.format('[%s %s]', pattern, options.text or '$1'))
		end
	end
	return value
end

p.formatRawValue = p.formatValue

return p