local export = {}

local len = mw.ustring.len
local loadData = mw.loadData

local function format_Tangut_text(text) return '<span class="Tang" lang="txg">' .. text .. '</span>' end

function export.pron(frame)
	local args = frame:getParent().args
	local title = args["1"] or mw.title.getCurrentTitle().text
	local prons = {
		loadData("Module:txg/data/LFW2008"),
		loadData("Module:txg/data/Miyake"),
		loadData("Module:txg/data/Kychanov2006"),
		loadData("Module:txg/data/Sofronov1968"),
		loadData("Module:txg/data/Nishida1966")
	}
	
	local pron = {{}, {}, {}, {}, {}}
	
	local sources = {
		"[[w:Lý Phạm Văn|Lý Phạm Văn]] 2008",
		"[[w:Marc Miyake|Miyake]] 2019",
		"[[w:Yevgeny Kychanov|Kychanov]] 2006",
		"Sofronov 1968",
		"[[w:Tatsuo Nishida|Nishida]] 1966"
	}
	
	local text = {'<div class="toccolours zhpron" style="width: max-content; max-width: 100%; padding-right: 1em; font-size:100%; overflow: hidden">'}
	
	for char in title:gmatch("[%z\1-\127\194-\244][\128-\191]*") do
		if char ~= "-" then
			for i = 1, 5 do
				table.insert(pron[i], prons[i][char] or "?")
			end
		end
	end
	
	for i = 1, 5 do
		for _, v in ipairs(pron[i]) do
			if v ~= "?" then
				table.insert(text, "\n* <small>(<i>" .. sources[i] .. "</i>)</small>: ")
				table.insert(text, "*" .. table.concat(pron[i], " "))
			break end
		end
	end
	
	table.insert(text, "</div>")
	
	return table.concat(text)
	
end

function export.newDer(frame)
	local title = mw.title.getCurrentTitle().subpageText
	if 101119 > mw.ustring.codepoint(title) and mw.ustring.codepoint(title) >= 100352 then
		idslist = require("Module:txg/data/idslist").list
	else
		wordlist = require("Module:txg/data/wordlist").list
	end
	local args = frame:getParent().args
	
	local result = {}
	
	for _, arg in ipairs(args) do
		table.insert(result, arg)
	end
	
	if wordlist then
		for _, word in ipairs(wordlist) do
			if word:match(title) and word ~= title then
				table.insert(result, word)
			end
		end
	elseif idslist then
		for char, seq in pairs(idslist) do
			if seq:match(title) and seq ~= title then
				table.insert(result, char)
			end
		end
	end
	
	return "{{col-auto|txg|" .. table.concat(result, "|") .. "}}"
end

function export.see(frame)
	local params = {
		[1] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local title = args[1]
	local curr_title = mw.title.getCurrentTitle().subpageText
	local content = mw.title.new(title):getContent()
	local senses = {}
	local sense_id = 0
	local j, pos, s, section
	
	if title == curr_title then
		return error("The soft-directed item is the same as the page title.")
	end
	
	if content then
		if not content:match("==Tangut==") then
			categories = categories .. "[[Category:Tangut redlinks/txg-see]]"
		elseif not content:match("txg%-pron") and not content:match("txg%-see") then
			require("Module:debug").track("txg-see/unidirectional reference to variant")
		elseif not content:match(curr_title) then
			require("Module:debug").track("txg-see/unidirectional reference variant→orthodox")
		end
	end
	
	while true do
		_, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.-)\n==%f[^=]", pos)
		
		if j == nil then
			i, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.+)", pos)
		end
		
		if j == nil then
			break
		else
			pos = j - 1
		end
		
		if language_name == "Tangut" then
			section = s
		end
	end
		
	if not section then
		return ""
	end
	
	section = section:gsub("\n===+Etymology.-(\n==)", "%1")
	section = section:gsub("\n===+Glyph origin.-(\n==)", "%1")
	
	local text = {'<div class="toccolours zhpron" style="width: max-content; padding-left: 1em; padding-right: 1.5em; padding-bottom: 1em; font-size:100%; overflow: hidden">'}
	
	table.insert(text, 'This character is a variant form of <span style="font-size:150%;">' .. frame:expandTemplate{ title = "l", args = { "txg", title} } .. "</span>")
	
	for sense in section:gmatch("\n# ([^\n]+)") do
		if not sense:match("rfdef") and not sense:match("defn") then
			sense_id = sense_id + 1
			table.insert(senses, sense)
		end
	end
	if sense_id == 0 then
		table.insert(text, ".")
	else
		table.insert(text, ":\n# " .. table.concat(senses, "\n# "))
	end
	
	table.insert(text, "</div>")
	
	return frame:preprocess( table.concat(text) )
		
end

return export