local p = {}
local configfile = 'Module:Authority control/config' -- default configuation module
local arg = mw.getCurrentFrame.args.config
if arg and arg~='' then
	configfile = 'Module:Authority control/config/' .. arg
end
local config
if mw.title.new(configfile).exists then
	config = mw.loadData(configfile)
else
	return error
end
local title = mw.title.getCurrentTitle
local namespace = title.namespace
local testcases = title.subpageText == config.i18n.testcases
local makelink = require(config.i18n.ACmodule).makelink

local function needsAttention(sortkey)
	return '' .. sortkey .. title.text .. ''
end

local function addCat
	if cat and cat ~= '' and (namespace == 0 or namespace == 14 or testcases) then
		local redlinkcat = ''
		if testcases == false and mw.title.new.exists == false then
			redlinkcat = needsAttention('N')
		end
		if sortkey then
			cat = '' .. sortkey .. title.text .. ''
		else
			cat = '' .. config.i18n.category .. ':'..cat..''
		end
		cat = cat .. redlinkcat
		return cat
	else
		return ''
	end
end

-- Creates a human-readable standalone wikitable version of conf, and tracking categories with page counts, for use in the documentation
function p.docConfTable(frame)
	local function checkcat
		local ret=''..label..''
		--if mw.title.new.exists == false then
			--ret = ret..' '
		--end
		return ret
	end
	local doc = ''
	local a, P, f = 0, 0, 0 --cumulative sums
	local count = frame.args.count or false
	local columns = 3 + (count and 1 or 0)
	local lang = mw.getContentLanguage
	local row = function(conf)
		local category = conf.category or conf[1]
		local articleCat = category..' '..config.i18n.Articles_with
		local articleCount = mw.site.stats.pagesInCategory
		local errorCat = conf.errorcat or config.i18n.faulty..' '..(conf.category or conf[1])..' '..config.i18n.Articles_with
		local errorCount = mw.site.stats.pagesInCategory
		P = P + 1 --property count
		a = a + articleCount
		f = f + errorCount
		local getstatement = function
			local statement = mw.wikibase.getBestStatements
			local value
			if statement and statement[1] and statement[1].mainsnak.datavalue.value.id then
				value = statement[1].mainsnak.datavalue.value.id
				local qual
				if qualid and statement[1].qualifiers['P' .. qualid][1].datavalue.value then
					qual = statement[1].qualifiers['P' .. qualid][1].datavalue.value
				end
				return value, qual
			end
		end
		local _, example = getstatement
		if not example then example = '' end
		example = frame:expandTemplate{title = "Hlist", args = {'\n' .. makelink}} .. '\n'
		local getname = function(nameprop)
			local name = getstatement
			if name then
				return frame:expandTemplate{title='Wikidata fallback link',args={name}}
			end
		end
		local name = getname(1629) or getname(10726) or ''
		local out = ''
		if conf.remark then
			out = out .. ''
		else
			out = out .. '' ..
			'' ..
			'' ..
			''
		if count then
			out = out .. ''
		if conf.remark then
			out = out .. ""
		end
		return out
	end
	local doc = '' ..
	 '' ..
	 '' ..
	 '' ..
	 ''
	if count then
	 	doc = doc .. ''
	for _, conf in pairs(config.config) do
		doc = doc .. row(conf)
	end
	if count then
		doc = doc .. '' ..
			'' ..
			''
	return doc
end

function p.errorTable(frame)
	local Table = ''..
	 ''..
	 ''..
	 ''
	local f, P = 0, 0, 0 --cumulative sums
	for _, conf in pairs(config.config) do
		local category = conf.errorcat or '잘못된 '..(conf.category or conf[1])..' 식별자를 포함한 위키백과 문서'
		local count = mw.site.stats.pagesInCategory
		if count > 0 then
			P = P + 1
			f = f + count
			Table = Table..''..
				''..
				''
		end
	end
	Table = Table..''..
		''..
		''
	return Table
end

function p.whitelisttable(frame)
	local Table = ''..
	 ''..
	 ''..
	 ''
	for code, wlist in pairs(config.whitelists) do
		Table = Table .. ''..
			''
		local plist = {}
		for _, property in pairs(wlist.properties) do
			table.insert
		end
		Table = Table .. ''
	return Table
end

function p.sectiontable(frame)
	local Table = ''..
	 '' ..
	 ''..
	 ''
	for number,section in ipairs(config.sections) do
		Table = Table .. ''
		local plist = {}
		for _,id in pairs(config.config) do
			if id.section == number then
				table.insert
			end
		end
		Table = Table .. ''
	return Table
end

-- Main/External Call for Pages with authority control identifiers
function p.autoDetect(frame)
	local function whichTOC( frame ) -- standardize TOC behavior via 
		return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } }
	end
	local function wpa
		for _,conf in pairs(config.config) do
			-- if conf.category == id or conf[1] == id then
			if conf.category == id or mw.ustring.lower(conf[1]) == mw.ustring.lower(id) then
				local getname = function(nameprop)
					local name = mw.wikibase.getBestStatements
					if name and name[1] and name[1].mainsnak.datavalue.value.id then
						return frame:expandTemplate{title='Wikidata fallback link',args={name[1].mainsnak.datavalue.value.id}}
					end
				end
				local name = getname('P1629') or getname('P10726') or ''
				local outString = frame:expandTemplate{ title = 'Category explanation', args = {name..' 식별자를 포함한 문서(하위 분류를 추가하지 말아 주십시오. 이 분류에 속하는 문서는 모듈:Authority control을 통해서만 추가되어야 합니다.)'}}
					.. frame:expandTemplate{ title = '분류 설명', args = {'위키백과:전거 통제', ':d:Property:P'..conf.property} }
					.. frame:expandTemplate{ title = '빈 분류' }
					.. frame:expandTemplate{ title = '위키백과 분류', args = { hidden = 'yes', tracking = 'yes' } }
					.. frame:expandTemplate{ title = 'Polluted category' }
					.. whichTOC( frame )
				if faulty then
					outString = outString .. addCat .. addCat(id..' 식별자를 포함한 위키백과 문서')
				else
					outString = outString .. addCat
				end
				return outString
			end
		end
	end
	if namespace == 14 then --cat space
		local wpfaultyID = mw.ustring.match(title.text, '잘못된 (.+) 식별자를 포함한 위키백과 문서')
		local wpID = mw.ustring.match(title.text, '(.+) 식별자를 포함한 위키백과 문서')
		if wpfaultyID then
			return wpa-- must be before wpID check, in case they both match
		elseif wpID then
			return wpa
		else
			return needsAttention('U')
		end
	end
end

function p.idproposal(frame)
	local property = frame.args.property or ''
	local out = ''
	out = out .. '링크: '
	local link = frame.args.link or ''
	if link=='' then
		link = mw.wikibase.getBestStatements or ''
		link = link[1].mainsnak.datavalue.value
	end	
	if link~='' then
		out = out .. '레이블: '
	local label = frame.args.label or ''
	if label~='' then
		out = out .. label
	else
		out = out .. '미지정'
	end
	out = out .. ''
	return out
end