local export = {}
local replace = mw.ustring.gsub
local m_ja = require('Module:ja')

local kana_data = {
	-- ['名']={'順','行','段'},
	['あ']={ '1','あ','あ'}, ['い']={ '2','あ','い'}, ['う']={ '3','あ','う'}, ['え']={ '4','あ','え'}, ['お']={ '5','あ','お'},
	['か']={ '6','か','あ'}, ['き']={ '7','か','い'}, ['く']={ '8','か','う'}, ['け']={ '9','か','え'}, ['こ']={'10','か','お'},
	['さ']={'11','さ','あ'}, ['し']={'12','さ','い'}, ['す']={'13','さ','う'}, ['せ']={'14','さ','え'}, ['そ']={'15','さ','お'},
	['た']={'16','た','あ'}, ['ち']={'17','た','い'}, ['つ']={'18','た','う'}, ['て']={'19','た','え'}, ['と']={'20','た','お'},
	['な']={'21','な','あ'}, ['に']={'22','な','い'}, ['ぬ']={'23','な','う'}, ['ね']={'24','な','え'}, ['の']={'25','な','お'},
	['は']={'26','は','あ'}, ['ひ']={'27','は','い'}, ['ふ']={'28','は','う'}, ['へ']={'29','は','え'}, ['ほ']={'30','は','お'},
	['ま']={'31','ま','あ'}, ['み']={'32','ま','い'}, ['む']={'33','ま','う'}, ['め']={'34','ま','え'}, ['も']={'35','ま','お'},
	['や']={'36','や','あ'},                          ['ゆ']={'37','や','う'},                          ['よ']={'38','や','お'},
	['ら']={'39','ら','あ'}, ['り']={'40','ら','い'}, ['る']={'41','ら','う'}, ['れ']={'42','ら','え'}, ['ろ']={'43','ら','お'},
	['わ']={'44','わ','あ'}, ['ゐ']={'45','わ','い'},                          ['ゑ']={'46','わ','え'}, ['を']={'47','わ','お'},
	['ん']={'48', nil, nil},
}

local function link(term, tr, gloss)
	if not tr then tr = m_ja.kana_to_romaji(term) end
	return require('Module:links').full_link({lang = require('Module:languages').getByCode('ja'), term = term, tr = tr, gloss = gloss}, 'term')
end

--[[ ไม่ใช้
local function to_ordinal(text)
	return mw.getCurrentFrame():expandTemplate{ title = 'theordinal', args = { text } }
end
--]]

function export.show(frame)
	local args = frame:getParent().args
	local syllable = mw.title.getCurrentTitle().text
	local k2h_syllable = m_ja.kata_to_hira(syllable)
	local manual_translit = args['tr'] or nil

	local text = 'พยางค์ $syllable ใน$script เทียบเท่ากับพยางค์ $other_syllable ใน$other_script'
	local text_two = 'เป็นพยางค์ที่ $order ในลำดับ[[โกจูอง]]'
	local text_three = '; ตำแหน่ง $position'

	if m_ja.script(syllable) == 'Hira' then
		text = replace(text, '$script', '[[ภาคผนวก:อักษรฮิรางานะ|อักษรฮิรางานะ]]')
		text = replace(text, '$other_script', '[[ภาคผนวก:อักษรคาตากานะ|อักษรคาตากานะ]]')
		text = replace(text, '$syllable', link(syllable, manual_translit))
		text = replace(text, '$other_syllable', link(m_ja.hira_to_kata(syllable), manual_translit))
	elseif m_ja.script(syllable) == 'Kana' then
		text = replace(text, '$script', '[[ภาคผนวก:อักษรคาตากานะ|อักษรคาตากานะ]]')
		text = replace(text, '$other_script', '[[ภาคผนวก:อักษรฮิรางานะ|อักษรฮิรางานะ]]')
		text = replace(text, '$syllable', link(syllable, manual_translit))
		text = replace(text, '$other_syllable', link(k2h_syllable, manual_translit))
	else
		error(syllable .. ' is not a kana character or sequence.')
	end

	if syllable == 'ん' then
		text_two = replace(text_two, '$order', kana_data[syllable][1])
		return text .. ' ' .. text_two
	elseif syllable == 'ン' then
		text_two = replace(text_two, '$order', kana_data[k2h_syllable][1])
		return text .. ' ' .. text_two
	elseif kana_data[syllable] or kana_data[k2h_syllable] then
		if m_ja.script(syllable) == 'Kana' then
			kana_data[syllable] = kana_data[k2h_syllable] -- kana_data['ア'] = kana_data['あ']
			kana_data[syllable][2] = m_ja.hira_to_kata(kana_data[k2h_syllable][2]) -- kana_data['ア'][2] = k2h(kana_data['あ'][2])
			kana_data[syllable][3] = m_ja.hira_to_kata(kana_data[k2h_syllable][3]) -- kana_data['ア'][3] = k2h(kana_data['あ'][3])
		end
		text_two = replace(text_two, '$order', kana_data[syllable][1])
		text_three = replace(text_three, '$position',
			link(
					kana_data[syllable][2] .. '[[行]]' .. kana_data[syllable][3] .. '[[段]]',
					m_ja.kana_to_romaji(kana_data[syllable][2]) .. '-gyō ' .. m_ja.kana_to_romaji(kana_data[syllable][3]) .. '-dan',
					"แถว ''" .. m_ja.kana_to_romaji(kana_data[syllable][2]) .. "'', หลัก ''" .. m_ja.kana_to_romaji(kana_data[syllable][3]) .. "''"
			)
		)
		return text .. ' ' .. text_two .. text_three
	else
		return text
	end
end

return export