Documentation for this module may be created at Модуль:zh-see/doc

local export = {}
local sub = mw.ustring.sub
local match = mw.ustring.match

local function link(text, make_link)
	return '<span lang="zh" class="Hani">' .. (make_link and ('[[' .. text .. '#Кытай|' .. text .. ']]') or text) .. '</span>'
end

function export.show(frame)
	local non_lemma_type = {
		['s'] = 'the simplified', ['simp'] = 'the simplified', ['simplified'] = 'the simplified',
		['a'] = 'an ancient', ['ancient'] = 'an ancient',
		['o'] = 'an obsolete', ['obsolete'] = 'an obsolete',
		['v'] = 'a variant', ['var'] = 'a variant', ['variant'] = 'a variant',
		['av'] = 'an ancient variant',
		['sv'] = 'the simplified and variant traditional',
		['all'] = 'a simplified, obsolete or variant',
	}
	
	local args = frame:getParent().args
	local title = args[1]
	local curr_title = mw.title.getCurrentTitle().subpageText
	local content = mw.title.new(title):getContent() or ""
	local simp = args["simp"] or false
	local non_lemma_abbrev = args[2] or ""
	
	if title == curr_title then
		return error("The soft-directed item is the same as the page title.")
	end
	
	content = mw.ustring.gsub(content, "zh%-forms", "Ѭ")
	content = mw.ustring.gsub(content, "zh%-pron", "Ꙁ")
	if non_lemma_abbrev == "" and mw.title.new(title).exists then
		template = mw.ustring.match(content, "{{Ѭ[^}]*}}") or false
		if template then
			for forms_template in mw.ustring.gmatch(content, "{{Ѭ[^}]*}}") do
				if match(forms_template, curr_title) then
					if match(forms_template, "s[0-9]*=" .. curr_title) then
						non_lemma_abbrev = non_lemma_abbrev .. "s"
					end
					if match(forms_template, "t[0-9]*=" .. curr_title) then
						non_lemma_abbrev = non_lemma_abbrev .. "v"
					end
					break
				end
			end
		end
	end
	local non_lemma_cat = non_lemma_type[non_lemma_abbrev ~= "" and non_lemma_abbrev or 's'] or 'жөнөкөйлөтүлгөн'
	local note = ((mw.title.new(title) or {}).exists and mw.title.getCurrentTitle().nsText == "") and "" or '[[Category:Ыйык түрлөрү менен кытай шарттары]]'
	
	local box =
		'{| class="wikitable' .. (match(non_lemma_cat, 'simplified') and ' mw-collapsible mw-collapsed' or '') ..
			'" style="border:1px solid #797979; margin-left: 1px; text-align:left; min-width:70%"' ..
		
			'\n|-\n| style="background-color: #eeeeee; padding-left: 0.5em" | \'\'\'Айтылышы жана аныктамасын \'\'\'' .. link(mw.ustring.gsub(curr_title, '(.)', '[[%1]]'), false) .. 
			'\'\'\' – see <span style="font-size:120%">' .. link(title, true) .. '</span>' .. (args[3] and (' (“' .. args[3] .. '”)') or '') .. '.\'\'\'' ..
			
			'<br>(This ' .. (mw.ustring.len(title) == 1 and 'белги' or 'мөөнөт' ) .. ', ' .. link(curr_title) .. ', is ' .. ' \'\'' .. non_lemma_cat .. '\'\' form of ' .. link(title) .. '' ..
			
			(simp and
				('<small>:&nbsp; ' .. link(sub(simp, 1, 1), true) .. ' → ' .. link(sub(simp, 2, 2), true) .. '</small>')
			or
				'') .. '.)' .. 
			
			(match(non_lemma_cat, 'жөнөкөйлөтүлгөн') and [=[

|-
| class="mw-collapsible-content" style="background-color: #F5DEB3; font-size: smaller" | <b>Notes:</b>
* [[w:Simplified Chinese|Simplified Chinese]] is mainly used in Mainland China and Singapore.
* [[w:Traditional Chinese|Traditional Chinese]] is mainly used in Hong Kong, Macau and Taiwan.]=] or '') ..

			'\n|}' .. note
	
	local cat = { "вариант", "жөнөкөйлөтүлгөн", "эскирген" }
	local m_cat = require("Module:zh-cat")
	
	categories = ''
	for _, word in ipairs(cat) do
		if match(non_lemma_cat, word) then
			categories = categories .. m_cat.categorize(word)
		end
	end
	
	if content then
		match_group = mw.ustring.len(mw.ustring.gsub(content, "[^Ѭ]", "")) == 1
			and mw.ustring.gmatch(content, '{{Ꙁ[^Ꙁ]+%|cat%=[^\n]+\n?}}')
			or mw.ustring.gmatch(content, '{{Ѭ[^%}]+' .. curr_title .. '[^%}]*}}[^ѬꙀ]+({{Ꙁ.+\n%|cat%=[^\n]+\n?}})')
		
		for match_result in match_group do
			local function find_pron(variety)
				return sub(match(match_result, variety .. '=[^|}]+') or '', mw.ustring.len(variety) + 2, -1)
			end
		
			categories = categories .. mw.getCurrentFrame():expandTemplate{
				title = "Template:zh-pron",
				args = {
					['m'] = find_pron('m'),
					['c'] = find_pron('c'),
					['g'] = find_pron('g'),
					['h'] = find_pron('h'),
					['j'] = find_pron('j'),
					['md'] = find_pron('md'),
					['mn'] = find_pron('mn'),
					['mn-t'] = find_pron('mn-t'),
					['w'] = find_pron('w'),
					['x'] = find_pron('x'),
					['cat'] = find_pron('cat'),
					['only_cat'] = 'ооба',
					}
				}
		end
	end

	return box .. categories
end

return export