local export = {}

local m_ja = require("Module:ja")
local m_ja_parse = require("Module:ja-parse/sandbox")

local function link(lemma, key, bracket)
	local anchor = key and ('ja-' .. key) or 'Japanese'
	return '<span lang="ja" class="Jpan">' .. (bracket and '【' or '') .. '[[' .. lemma .. '#' .. anchor .. '|' .. lemma .. ']]' .. (bracket and '】' or '') .. '</span>'
end

local function ja(text)
	return '<span lang="ja" class="Jpan">' .. text .. '</span>'
end

local function spelling_type(source, target)
	local source_spelling_type = m_ja.script(source)
	local target_spelling_type = m_ja.script(target)
	if source_spelling_type:find('Hani') then
		return target_spelling_type:find('Hani') and 'an alternative kanji' or 'a kanji'
	elseif source_spelling_type == 'Hira' and target_spelling_type ~= 'Hira' then
		return 'a hiragana'
	elseif source_spelling_type == 'Kana' and target_spelling_type ~= 'Kana' then
		return 'a katakana'
	else
		return 'an alternative'
	end
end

local function format_definitions(defs, frame) -- format inspired by [[User:Wyang/zh-def]]
	local def_lines = {}
	local def_number = #defs
	for i = 1, #defs do
		local def = defs[i]
		local def_line = def[1]
		if def.pos ~= '' then
			def_line = def_line:gsub('^# ', '# <span style="padding-right:.6em;color:#5A5C5A;font-size:80%%">[' .. def.pos:lower() .. ']</span>')
		end
		def_line = def_line:gsub('^#', ':')
		table.insert(def_lines, def_line)
	end
	if def_number > 5 then
		local shorter = { unpack(def_lines, 1, 5) }
		def_lines = { unpack(def_lines, 6) }
		local result = { '<div class="vsSwitcher" data-toggle-category="glosses"><span class="vsToggleElement"></span><div class="vsShow" style="display: none;">' }
		for i, v in ipairs(shorter) do
			if i > 1 then
				table.insert(result, '\n')
			end
			table.insert(result, v)
		end
		table.insert(result, '</div><div class="vsHide">')
		for i, v in ipairs(def_lines) do
			if i > 1 then
				table.insert(result, '\n')
			end
			table.insert(result, v)
		end
		table.insert(result, '</div></div>')
		def_lines = table.concat(result, '\n')
	else
		def_lines = table.concat(def_lines, '\n')
	end
	def_lines = frame:preprocess(def_lines)
	local log
	def_lines, log = def_lines:gsub('<span class="ib%-brac">%(</span><span class="ib%-content">', '<span style="padding-right:.6em;color:#105E02;font-size:80%%"><span>[</span>')
	def_lines = def_lines:gsub('</span><span class="ib%-brac">%)</span>%s*', '<span>]</span></span>')
	return def_lines, def_number
end

local function get_definitions_and_categories(lemma, key, frame)
	local wikitext = m_ja_parse.get_etym_section(lemma, key, frame)
	local def, cat = m_ja_parse.extract_definitions_and_categories(wikitext, lemma, key, frame)
	local def_number
	def, def_number = format_definitions(def, frame)
	if mw.title.getCurrentTitle().nsText ~= '' then cat = '' end
	return def, cat, def_number
end

function export.show(frame)
	local args = frame:getParent().args
	local source = args.pagename or mw.title.getCurrentTitle().text
	local target = args[1]
	
	local source_link = ja(source)
	local source_spelling_type = spelling_type(source, target)
	local target_link = (source_spelling_type == 'a kanji' or source_spelling_type == 'an alternative kanji') and link(target, source) or link(target)
	local def, cat, def_number = get_definitions_and_categories(target, source, frame)
	
	local result = { [=[{| class="wikitable" style="min-width:70%"
|-
| '''For pronunciation and definitions of ]=] .. source_link .. " – see " }
	if def ~= '' then table.insert(result, 'the entry at ') end
	table.insert(result, "<span style='font-size:120%'>" .. target_link .. '</span>')
	
	if args.gloss then
		table.insert(result, "&nbsp;(“" .. args.gloss .. "”).'''<br>")
	elseif def == '' then
		table.insert(result, ".'''<br>")
	else
		table.insert(result, " with the following definition" .. (def_number == 1 and '' or 's') .. ".'''\n" .. def .. '\n')
	end
	table.insert(result, '(This term, ' .. source_link .. ", is ''" .. source_spelling_type .. "'' spelling of " .. target_link .. [=[.)
|}]=])
	table.insert(result, cat)
	return table.concat(result)
end

local kanji_grade_labels = {
	'<span class="explain" title="Grade 1 kanji" style="vertical-align: top;">1</span>',
	'<span class="explain" title="Grade 2 kanji" style="vertical-align: top;">2</span>',
	'<span class="explain" title="Grade 3 kanji" style="vertical-align: top;">3</span>',
	'<span class="explain" title="Grade 4 kanji" style="vertical-align: top;">4</span>',
	'<span class="explain" title="Grade 5 kanji" style="vertical-align: top;">5</span>',
	'<span class="explain" title="Grade 6 kanji" style="vertical-align: top;">6</span>',
	'<span class="explain" title="Jōyō kanji" style="vertical-align: top;">S</span>',
	'<span class="explain" title="Jinmeiyō kanji" style="vertical-align: top;">J</span>',
	'<span class="explain" title="Hyōgaiji kanji" style="vertical-align: top;">H</span>'
}

function export.show_kango(frame)
	local args = frame:getParent().args
	local source = args.pagename or mw.title.getCurrentTitle().text
	
	local kanji_table = { '{| style="width: 100%"' }
	local cats = {}
	
	local args_len = 0 -- too bad #args doesn't work
	for _, v in ipairs(args) do
		local def, cat = get_definitions_and_categories(v, source, frame)
		table.insert(kanji_table, '|-')
		
		local kanji_grade_label = ''
		if mw.ustring.len(v) == 1 then
			local kanji_grade = m_ja.kanji_grade(v)
			if kanji_grade then kanji_grade_label = kanji_grade_labels[kanji_grade] end
		end
		table.insert(kanji_table, '| style="vertical-align:top; ' .. (args_len == 0 and '' or 'border-top:1px solid lightgray') .. '" | <span style="font-size:x-large">' .. link(v, nil, true) .. '</span>' .. kanji_grade_label)
		
		table.insert(kanji_table, args_len == 0 and '|' or '| style="border-top:1px solid lightgray" |')
		table.insert(kanji_table, def)
		table.insert(cats, cat)
		args_len = args_len + 1
	end
	table.insert(kanji_table, '|}')
	kanji_table = table.concat(kanji_table, '\n')
	
	local source_link = ja(source)
	local result = { '{| class="wikitable" style="min-width:70%"' }
	table.insert(result, '|-')
	table.insert(result, "| '''For pronunciation and definitions of " .. source_link .. " – see the following " .. (args_len == 1 and "entry.'''" or "entries.'''"))
	table.insert(result, '|-')
	table.insert(result, '| style="background-color: white" |')
	table.insert(result, kanji_table)
	table.insert(result, '|-')
	table.insert(result, "| (This term, " .. source_link .. ", is ''the hiragana'' spelling of " .. (args_len == 1 and "the Sino-Japanese term.)" or "several Sino-Japanese terms.)"))
	table.insert(result, '|}')
	
	cats = table.concat(cats)
	
	return table.concat(result, '\n') .. cats
end

return export
"https://si.wiktionary.org/w/index.php?title=Module:ja-see/sandbox&oldid=43504" වෙතින් සම්ප්‍රවේශනය කෙරිණි