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 = 'ordinal to word', 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 = 'Suku kata $script $syllable. Yang setaraf dengannya dalam $other_script ialah $other_syllable.'
	local text_two = 'Ia ialah suku kata $order dalam susunan [[gojūon]]'
	local text_three = '; kedudukannya ialah $position.'

	if m_ja.script(syllable) == 'Hira' then
		text = replace(text, '$script', '[[Lampiran:Skrip hiragana|hiragana]]')
		text = replace(text, '$other_script', '[[Lampiran:Skrip katakana|katakana]]')
		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', '[[Lampiran:Skrip katakana|katakana]]')
		text = replace(text, '$other_script', '[[Lampiran:Skrip hiragana|hiragana]]')
		text = replace(text, '$syllable', link(syllable, manual_translit))
		text = replace(text, '$other_syllable', link(k2h_syllable, manual_translit))
	else
		error(syllable .. ' bukan askara kana atau urutan kana.')
	end

	if syllable == 'ん' then
		text_two = replace(text_two, '$order', to_ordinal(kana_data[syllable][1]))
		return text .. ' ' .. text_two .. '.'
	elseif syllable == 'ン' then
		text_two = replace(text_two, '$order',  to_ordinal(kana_data[k2h_syllable][1]))
		return text .. ' ' .. text_two .. '.'
	elseif (syllable == 'ヷ') or (syllable == 'ヸ') or (syllable == 'ヹ') or (syllable == 'ヺ') or (syllable == 'ヸャ') or (syllable == 'ヸュ') or (syllable == 'ヸョ') then
		-- XXX
		text = replace(text, ' Its equivalent.+', '')
		return text
	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', to_ordinal(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',
					"baris ''" .. m_ja.kana_to_romaji(kana_data[syllable][2]) .. "'', bahagian ''" .. m_ja.kana_to_romaji(kana_data[syllable][3]) .. "''"
			)
		)
		return text .. ' ' .. text_two .. text_three
	else
		return text
	end
end

return export