local export = {}

local rm_spaces_hyphens = require("Module:ja").rm_spaces_hyphens
local kana_to_romaji = require("Module:Hrkt-translit").tr

function export.japanese_classical_conjugation(frame)
	local args = frame:getParent().args
	

	local function _i(t, i)
		if type(t) == 'table' then return t[i]
		else return t end
	end

	local function behead(s)
		return mw.ustring.gsub(s, '[ぁ-ゖァ-ヶ]', '', 1)
	end

	local function _l(s)
		return '<span class="Jpan" lang="ja-Jpan">[[' .. _i(s, 1) .. '#ภาษาญี่ปุ่น|' .. _i(s, 2) ..']]</span>'
	end

	local t = {
		['4-k'] = {
			'か', 'き', 'く', 'く', 'け', 'け',
			['t_percon'] = { 'けり', 'きたり' }
		},
		['4-g'] = {
			'が', 'ぎ', 'ぐ', 'ぐ', 'げ', 'げ',
			['t_percon'] = { 'げり', 'ぎたり' }
		},
		['4-s'] = {
			'さ', 'し', 'す', 'す', 'せ', 'せ',
			['t_percon'] = { 'せり', 'したり' }
		},
		['4-t'] = {
			'た', 'ち', 'つ', 'つ', 'て', 'て',
			['t_percon'] = { 'てり', 'ちたり' }
		},
		['4-p'] = {
			'は', 'ひ', 'ふ', 'ふ', 'へ', 'へ',
			['t_percon'] = { 'へり', 'ひたり' }
		},
		['4-b'] = {
			'ば', 'び', 'ぶ', 'ぶ', 'べ', 'べ',
			['t_percon'] = { 'べり', 'びたり' }
		},
		['4-m'] = {
			'ま', 'み', 'む', 'む', 'め', 'め',
			['t_percon'] = { 'めり', 'みたり' }
		},
		['4-r'] = {
			'ら', 'り', 'る', 'る', 'れ', 'れ',
			['t_percon'] = { 'れり', 'りたり' }
		},
		
		['2u-k'] = { 'き', 'き', 'く', 'くる', 'くれ', 'き.よ' },
		['2u-g'] = { 'ぎ', 'ぎ', 'ぐ', 'ぐる', 'ぐれ', 'ぎ.よ' },
		['2u-z'] = { 'じ', 'じ', 'ず', 'ずる', 'ずれ', 'じ.よ' },
		['2u-t'] = { 'ち', 'ち', 'つ', 'つる', 'つれ', 'ち.よ' },
		['2u-d'] = { 'ぢ', 'ぢ', 'づ', 'づる', 'づれ', 'ぢ.よ' },
		['2u-p'] = { 'ひ', 'ひ', 'ふ', 'ふる', 'ふれ', 'ひ.よ' },
		['2u-b'] = { 'び', 'び', 'ぶ', 'ぶる', 'ぶれ', 'び.よ' },
		['2u-m'] = { 'み', 'み', 'む', 'むる', 'むれ', 'み.よ' },
		['2u-y'] = { '.い', '.い', '.ゆ', '.ゆる', '.ゆれ', '.い.よ' },
		['2u-r'] = { 'り', 'り', 'る', 'るる', 'るれ', 'り.よ' },
		
		['2d-a'] = { '.え', '.え', '.う', '.うる', '.うれ', '.えよ' },
		['2d-k'] = { 'け', 'け', 'く', 'くる', 'くれ', 'けよ' },
		['2d-g'] = { 'げ', 'げ', 'ぐ', 'ぐる', 'ぐれ', 'げよ' },
		['2d-s'] = { 'せ', 'せ', 'す', 'する', 'すれ', 'せよ' },
		['2d-z'] = { 'ぜ', 'ぜ', 'ず', 'ずる', 'ずれ', 'ぜよ' },
		['2d-t'] = { 'て', 'て', 'つ', 'つる', 'つれ', 'てよ' },
		['2d-d'] = { 'で', 'で', 'づ', 'づる', 'づれ', 'でよ' },
		['2d-n'] = { 'ね', 'ね', 'ぬ', 'ぬる', 'ぬれ', 'ねよ' },
		['2d-p'] = { 'へ', 'へ', 'ふ', 'ふる', 'ふれ', 'へよ' },
		['2d-b'] = { 'べ', 'べ', 'ぶ', 'ぶる', 'ぶれ', 'べよ' },
		['2d-m'] = { 'め', 'め', 'む', 'むる', 'むれ', 'めよ' },
		['2d-y'] = { '.え', '.え', '.ゆ', '.ゆる', '.ゆれ', '.えよ' },
		['2d-y-ye'] = { '.𛀁', '.𛀁', '.ゆ', '.ゆる', '.ゆれ', '.𛀁よ' },
		['2d-r'] = { 'れ', 'れ', 'る', 'るる', 'るれ', 'れよ' },
		['2d-w'] = { 'ゑ', 'ゑ', '.う', '.うる', '.うれ', 'ゑよ' },
		
		['1u-k'] = { 'き', 'き', 'きる', 'きる', 'きれ', 'き.よ' },
		['1u-n'] = { 'に', 'に', 'にる', 'にる', 'にれ', 'に.よ' },
		['1u-p'] = { 'ひ', 'ひ', 'ひる', 'ひる', 'ひれ', 'ひ.よ' },
		['1u-m'] = { 'み', 'み', 'みる', 'みる', 'みれ', 'み.よ' },
		['1u-y'] = { '.い', '.い', '.いる', '.いる', '.いれ', '.い.よ' },
		['1u-w'] = { 'ゐ', 'ゐ', 'ゐる', 'ゐる', 'ゐれ', 'ゐ.よ' },
		
		['1d-k'] = { 'け', 'け', 'ける', 'ける', 'けれ', 'けよ' },
		['1d-kw'] = { 'くゑ', 'くゑ', 'くゑる', 'くゑる', 'くゑれ', 'くゑよ' },
		
		['irreg-k'] = {
			'こ', 'き', 'く', 'くる', 'くれ', { 'こ', 'こよ' },
			['t_pas_ki'] = { 'こし', 'きし' }
		},
		
		['irreg-s'] = {
			'せ', 'し', 'す', 'する', 'すれ', 'せよ',
			['t_percon'] = { 'せり', 'したり' }
		},
		['irreg-s-z'] = {
			'ぜ', 'じ', 'ず', 'ずる', 'ずれ', 'ぜよ',
			['t_percon'] = { 'ぜり', 'じたり' }
		},
		
		['irreg-n'] = { 'な', 'に', 'ぬ', 'ぬる', 'ぬれ', 'ね' },
		
		['irreg-r'] = { 'ら', 'り', 'り', 'る', 'れ', 'れ' },

		['adj-ku'] = {
			{ 'く', 'から' }, { 'く', 'かり' }, 'し', { 'き', 'かる' }, 'けれ', 'かれ',
			['irr_1'] = 'く', ['irr_2'] = 'から', ['adv_1'] = 'く', ['adv_2'] = 'かり', 
		},
		['adj-shiku'] = {
			{ 'しく', 'しから' }, { 'しく', 'しかり' }, 'し', { 'しき', 'しかる' }, 'しけれ', 'しかれ',
			['irr_1'] = 'しく', ['irr_2'] = 'しから', ['adv_1'] = 'しく', ['adv_2'] = 'しかり', 
		},
		['adj-shiku-z'] = {
			{ 'じく', 'じから' }, { 'じく', 'じかり' }, 'じ', { 'じき', 'じかる' }, 'じけれ', 'じかれ',
			['irr_1'] = 'じく', ['irr_2'] = 'じから', ['adv_1'] = 'じく', ['adv_2'] = 'じかり', 
		},
		['adj-nar'] = {
			'なら', { 'に', 'なり' }, 'なり', 'なる', 'なれ', 'なれ',
			['adv_1'] = 'に', ['adv_2'] = 'なり'
		},
		['adj-tar'] = {
			'たら', { 'と', 'たり' }, 'たり', 'たる', 'たれ', 'たれ',
			['adv_1'] = 'と', ['adv_2'] = 'たり',
		},
	}

	local t_note = {
		['irreg-k'] = { ['t_pas_ki'] = 'Attributive form, as the terminal form (lemma) does not exist for this construction in this conjugation.' },

		['irreg-r'] = { ['t_percon'] = 'Often proscribed due to a redundancy of having 2 '.._l('あり')..'.' },

		['adj-ku'] = { { 'Without auxiliary verb.', 'With auxiliary verb.' }, { 'Without auxiliary verb.', 'With auxiliary verb.' } },
		['adj-shiku'] = { { 'Without auxiliary verb.', 'With auxiliary verb.' }, { 'Without auxiliary verb.', 'With auxiliary verb.' } },
		['adj-shiku-z'] = { { 'Without auxiliary verb.', 'With auxiliary verb.' }, { 'Without auxiliary verb.', 'With auxiliary verb.' } },
		['adj-nar'] = { nil, { 'Without auxiliary verb.', 'With auxiliary verb.' } },
		['adj-tar'] = { nil, { 'Without auxiliary verb.', 'With auxiliary verb.' } },
	}

	local conj_name = {
		['4-k'] = { '四段活用', 'カ行四段活用' },
		['4-g'] = { '四段活用', 'ガ行四段活用' },
		['4-s'] = { '四段活用', 'サ行四段活用' },
		['4-t'] = { '四段活用', 'タ行四段活用' },
		['4-p'] = { '四段活用', 'ハ行四段活用' },
		['4-b'] = { '四段活用', 'バ行四段活用' },
		['4-m'] = { '四段活用', 'マ行四段活用' },
		['4-r'] = { '四段活用', 'ラ行四段活用' },
		
		['2u-k'] = { '上二段活用', 'カ行上二段活用' },
		['2u-g'] = { '上二段活用', 'ガ行上二段活用' },
		['2u-z'] = { '上二段活用', 'ザ行上二段活用' },
		['2u-t'] = { '上二段活用', 'タ行上二段活用' },
		['2u-d'] = { '上二段活用', 'ダ行上二段活用' },
		['2u-p'] = { '上二段活用', 'ハ行上二段活用' },
		['2u-b'] = { '上二段活用', 'バ行上二段活用' },
		['2u-m'] = { '上二段活用', 'マ行上二段活用' },
		['2u-y'] = { '上二段活用', 'ヤ行上二段活用' },
		['2u-r'] = { '上二段活用', 'ラ行上二段活用' },
		
		['2d-a'] = { '下二段活用', 'ア行下二段活用' },
		['2d-k'] = { '下二段活用', 'カ行下二段活用' },
		['2d-g'] = { '下二段活用', 'ガ行下二段活用' },
		['2d-s'] = { '下二段活用', 'サ行下二段活用' },
		['2d-z'] = { '下二段活用', 'ザ行下二段活用' },
		['2d-t'] = { '下二段活用', 'タ行下二段活用' },
		['2d-d'] = { '下二段活用', 'ダ行下二段活用' },
		['2d-n'] = { '下二段活用', 'ナ行下二段活用' },
		['2d-p'] = { '下二段活用', 'ハ行下二段活用' },
		['2d-b'] = { '下二段活用', 'バ行下二段活用' },
		['2d-m'] = { '下二段活用', 'マ行下二段活用' },
		['2d-y'] = { '下二段活用', 'ヤ行下二段活用' },
		['2d-y-ye'] = { '下二段活用', 'ヤ行下二段活用' },
		['2d-r'] = { '下二段活用', 'ラ行下二段活用' },
		['2d-w'] = { '下二段活用', 'ワ行下二段活用' },
		
		['1u-k'] = { '上一段活用', 'カ行上一段活用' },
		['1u-n'] = { '上一段活用', 'ナ行上一段活用' },
		['1u-p'] = { '上一段活用', 'ハ行上一段活用' },
		['1u-m'] = { '上一段活用', 'マ行上一段活用' },
		['1u-y'] = { '上一段活用', 'ヤ行上一段活用' },
		['1u-w'] = { '上一段活用', 'ワ行上一段活用' },
		
		['1d-k'] = { '下一段活用', 'カ行下一段活用' },
		['1d-kw'] = { '下一段活用', 'カ行下一段活用' },
		
		['irreg-k'] = 'カ行変格活用',
		
		['irreg-s'] = 'サ行変格活用',
		['irreg-s-z'] = 'サ行変格活用',
		
		['irreg-n'] = 'ナ行変格活用',
		
		['irreg-r'] = 'ラ行変格活用',

		['adj-ku'] = 'ク活用',
		['adj-shiku'] = 'シク活用',
		['adj-shiku-z'] = 'シク活用',
		['adj-nar'] = 'ナリ活用',
		['adj-tar'] = 'タリ活用',
	}

	local t_assumed_pagename_ending = {
		['4-k'] = 'く',
		['4-g'] = 'ぐ',
		['4-s'] = 'す',
		['4-t'] = 'つ',
		['4-p'] = { 'ふ', 'う' },
		['4-b'] = 'ぶ',
		['4-m'] = 'む',
		['4-r'] = 'る',
		
		['2u-k'] = { 'く', 'きる' },
		['2u-g'] = { 'ぐ', 'ぎる' },
		['2u-z'] = { 'ず', 'じる' },
		['2u-t'] = { 'つ', 'ちる' },
		['2u-d'] = { 'づ', 'ぢる', 'じる' },
		['2u-p'] = { 'ふ', 'いる', 'ひる' },
		['2u-b'] = { 'ぶ', 'びる' },
		['2u-m'] = { 'む', 'みる' },
		['2u-y'] = { 'ゆ', 'いる' },
		['2u-r'] = { 'りる', 'る' },
		
		['2d-a'] = { 'う', 'える' },
		['2d-k'] = { 'く', 'ける' },
		['2d-g'] = { 'ぐ', 'げる' },
		['2d-s'] = { 'す', 'せる' },
		['2d-z'] = { 'ず', 'ぜる' },
		['2d-t'] = { 'つ', 'てる' },
		['2d-d'] = { 'づ', 'でる' },
		['2d-n'] = { 'ぬ', 'ねる' },
		['2d-p'] = { 'ふ', 'える', 'へる' },
		['2d-b'] = { 'ぶ', 'べる' },
		['2d-m'] = { 'む', 'める' },
		['2d-y'] = { 'ゆ', 'える' },
		['2d-y-ye'] = { 'ゆ', 'える', '𛀁る'},
		['2d-r'] = { 'れる', 'る' },
		['2d-w'] = { 'う', 'える', 'ゑる' },
		
		['1u-k'] = 'きる',
		['1u-n'] = 'にる',
		['1u-p'] = { 'ひる', 'いる' },
		['1u-m'] = 'みる',
		['1u-y'] = 'いる',
		['1u-w'] = { 'ゐる', 'いる' },
		
		['1d-k'] = 'ける',
		['1d-kw'] = 'くゑる',
		
		['irreg-k'] = { 'く', 'くる' },
		
		['irreg-s'] = { 'す', 'する' },
		['irreg-s-z'] = { 'ず', 'ずる', 'じる' },
		
		['irreg-n'] = 'ぬ',
		
		['irreg-r'] = { 'り', 'る' },

		['adj-ku'] = { 'し', 'い' },
		['adj-shiku'] = { 'し', 'しい' },
		['adj-shiku-z'] = { 'じ', 'じい' },
		['adj-nar'] = { 'なり', 'な' },
		['adj-tar'] = { 'たり', 'と' },
	}

	local pagename = args['pagename'] or mw.title.getCurrentTitle().text

	local stem = args['stem'] or args[1] or args['s'] or ''
	local ctype = args['ctype'] or args[2] or args['c'] or ''

	local kanji = args['kanji'] or args[3] or args['k']
	local suffix_in_kanji = args['suffix_in_kanji'] or args['sik']
	local lemma = args['lemma'] or args['l']

	local function guess_kanji_from_pagename()
		
		local suf
		if type(t_assumed_pagename_ending[ctype]) == 'table' then
			suf = t_assumed_pagename_ending[ctype]
		else
			suf = { t_assumed_pagename_ending[ctype] }
		end
		local lemma_ending

		if suffix_in_kanji then
			for _, v in ipairs(suf) do
				lemma_ending = behead(v)
				if lemma_ending ~= '' and string.sub(pagename, - string.len(lemma_ending)) == lemma_ending then
					kanji = kanji or string.sub(pagename, 1, - (string.len(lemma_ending) + 1))
					break
				end
			end
			kanji = kanji or pagename
			lemma = lemma or kanji .. behead(_i(t[ctype][3], 1))
		else
			for _, v in ipairs(suf) do
				lemma_ending = v
				if string.sub(pagename, - string.len(lemma_ending)) == lemma_ending then
					kanji = kanji or string.sub(pagename, 1, - (string.len(lemma_ending) + 1))
					break
				end
			end
			kanji = kanji or pagename
			lemma = lemma or kanji .. _i(t[ctype][3], 1)
		end

		return

	end

	if conj_name[ctype] == nil then
		kanji = kanji or pagename
		lemma = lemma or pagename
	else
		guess_kanji_from_pagename()
	end

	local f_irr = args['f_irr'] or args['f1']
	local f_adv = args['f_adv'] or args['f2']
	local f_ter = args['f_ter'] or args['f3']
	local f_adn = args['f_adn'] or args['f4']
	local f_rea = args['f_rea'] or args['f5']
	local f_imp = args['f_imp'] or args['f6']

	local f_neg = args['f_neg'] or args['f7']
	local f_conj_contr = args['f_conj_contr'] or args['f8']
	local f_conj_caus = args['f_conj_caus'] or args['f9']
	local f_conj_cond = args['f_conj_cond'] or args['f10']
	local f_t_pas_ki = args['f_t_pas_ki'] or args['f11']
	local f_t_pas_ker = args['f_t_pas_ker'] or args['f12']
	local f_t_per_t = args['f_t_per_t'] or args['f13']
	local f_t_per_n = args['f_t_per_n'] or args['f14']
	local f_t_percon = args['f_t_percon'] or args['f15']
	local f_adverb = args['f_adverb'] or args['f16']
	local f_volit = args['f_volit'] or args['f17']

	local f_irr__p = args['f_irr+']
	local f_adv__p = args['f_adv+']
	local f_ter__p = args['f_ter+']
	local f_adn__p = args['f_adn+']
	local f_rea__p = args['f_rea+']
	local f_imp__p = args['f_imp+']

	local f_neg__p = args['f_neg+']
	local f_conj_contr__p = args['f_conj_contr+']
	local f_conj_caus__p = args['f_conj_caus+']
	local f_conj_cond__p = args['f_conj_cond+']
	local f_t_pas_ki__p = args['f_t_pas_ki+']
	local f_t_pas_ker__p = args['f_t_pas_ker+']
	local f_t_per_t__p = args['f_t_per_t+']
	local f_t_per_n__p = args['f_t_per_n+']
	local f_t_percon__p = args['f_t_percon+']
	local f_adverb__p = args['f_adverb+']
	local f_volit__p = args['f_volit+']

	local hide_key_constructions = args['hide_key_constructions'] or args['hkc']

	local notes = {}
	local notes_rev ={}
	local note_num = 0

	local function insert_note(note)
		local note_id
		note_id = notes_rev[note]
		if note_id == nil then
			note_num = note_num + 1
			notes[note_num] = note
			notes_rev[note] = note_num
			note_id = note_num
		end
		return note_id
	end

	local function japanese_conj_row_classical_manual(form)

		local r1, r2, r3 = {}, {}, {}
		local form_no_note, kanji, kana, note

		local forms = {}

		local p1, p2
		local identical_kanji_kana = true

		string.gsub(form, '[^;]+', function(w) table.insert(forms, w) end)

		for _, form_single in ipairs(forms) do
			p1 = string.find(form_single, '?')
			if p1 then
				note = string.sub(form_single, p1 + 1)
				form_no_note = string.sub(form_single, 1, p1 - 1)
			else
				note = nil
				form_no_note = form_single
			end
	
			p2 = string.find(form_no_note, ':')
			if p2 then
				kanji = string.sub(form_no_note, 1, p2 - 1)
				kana = string.sub(form_no_note, p2 + 1)
			else
				kanji = form_no_note
				kana = form_no_note
			end

			identical_kanji_kana = identical_kanji_kana and kanji == kana

			if note then
				table.insert(r1,
					'<span class="Jpan" lang="ja-Jpan">'
					.. rm_spaces_hyphens(kanji)
					.. '</span><small><sup>['
					.. insert_note(note)
					.. ']</sup></small>')
			else
				table.insert(r1,
					'<span class="Jpan" lang="ja-Jpan">'
					.. rm_spaces_hyphens(kanji)
					.. '</span>')
			end
			table.insert(r2,
				'<span class="Jpan" lang="ja-Jpan">'
				.. rm_spaces_hyphens(kana)
				.. '</span>')
			table.insert(r3,
				'<span class="Latn" lang="ja-Latn">'
				.. kana_to_romaji(kana, "ja", nil, {hist = true})
				.. '</span>')
		end

		if identical_kanji_kana then
			return '| colspan ="2" |'
					.. table.concat(r1, '<br/>')
					.. '||'
					.. table.concat(r3, '<br/>')
		else
			return '||'
					.. table.concat(r1, '<br/>')
					.. '||'
					.. table.concat(r2, '<br/>')
					.. '||'
					.. table.concat(r3, '<br/>')
		end

	end

	local function japanese_conj_row_classical_auto(_stem, _kanji, _suffix, _suffix_in_kanji, _note, _tail)
		local result = {}
		
		if type(_suffix) ~= 'table' then
			if type(_suffix) == 'string' then
				_suffix = { _suffix }
			else
				return '-'
			end
		end
		_tail = _tail or ''

		if _suffix_in_kanji then
			if type(_note) == 'table' then
				for i, suf in ipairs(_suffix) do
					if _note[i] then
						table.insert(result, _kanji .. behead(suf) .. _tail .. ':' .. _stem .. suf .. _tail .. '?' .. _note[i])
					else
						table.insert(result, _kanji .. behead(suf) .. _tail .. ':' .. _stem .. suf .. _tail)
					end
				end 
			elseif type(_note) == 'string' then
				for _, suf in ipairs(_suffix) do
					table.insert(result, _kanji .. behead(suf) .. _tail .. ':' .. _stem .. suf .. _tail .. '?' .. _note)
				end
			else
				for _, suf in ipairs(_suffix) do
					table.insert(result, _kanji .. behead(suf) .. _tail .. ':' .. _stem .. suf .. _tail)
				end
			end
		else
			if type(_note) == 'table' then
				for i, suf in ipairs(_suffix) do
					if _note[i] then
						table.insert(result, _kanji .. suf .. _tail .. ':' .. _stem .. suf .. _tail .. '?' .. _note[i])
					else
						table.insert(result, _kanji .. suf .. _tail .. ':' .. _stem .. suf .. _tail)
					end
				end 
			elseif type(_note) == 'string' then
				for _, suf in ipairs(_suffix) do
					table.insert(result, _kanji .. suf .. _tail .. ':' .. _stem .. suf .. _tail .. '?' .. _note)
				end
			else
				for _, suf in ipairs(_suffix) do
					table.insert(result, _kanji .. suf .. _tail .. ':' .. _stem .. suf .. _tail)
				end
			end
		end

		return table.concat(result, ';')
	end

	f_irr = f_irr or japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 1), suffix_in_kanji, _i(t_note[ctype], 1))
	f_adv = f_adv or japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 2), suffix_in_kanji, _i(t_note[ctype], 2))
	f_ter = f_ter or japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 3), suffix_in_kanji, _i(t_note[ctype], 3))
	f_adn = f_adn or japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 4), suffix_in_kanji, _i(t_note[ctype], 4))
	f_rea = f_rea or japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 5), suffix_in_kanji, _i(t_note[ctype], 5))
	f_imp = f_imp or japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 6), suffix_in_kanji, _i(t_note[ctype], 6))

	if not hide_key_constructions and t[ctype] then
		f_neg = f_neg or
			_i(t[ctype], 'neg') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'neg'), suffix_in_kanji, _i(t_note[ctype], 'neg')) or
			japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'irr_2') or _i(t[ctype], 1), suffix_in_kanji, _i(t_note[ctype], 'neg'), 'ず')
		f_conj_contr = f_conj_contr or
			_i(t[ctype], 'conj_contr') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'conj_contr'), suffix_in_kanji, _i(t_note[ctype], 'conj_contr')) or
			japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 5), suffix_in_kanji, _i(t_note[ctype], 'conj_contr'), 'ど')
		f_conj_caus = f_conj_caus or
			_i(t[ctype], 'conj_caus') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'conj_caus'), suffix_in_kanji, _i(t_note[ctype], 'conj_caus')) or
			japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 5), suffix_in_kanji, _i(t_note[ctype], 'conj_caus'), 'ば')
		f_conj_cond = f_conj_cond or
			_i(t[ctype], 'conj_cond') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'conj_cond'), suffix_in_kanji, _i(t_note[ctype], 'conj_cond')) or
			japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'irr_1') or _i(t[ctype], 1), suffix_in_kanji, _i(t_note[ctype], 'conj_cond'), 'ば')
		f_t_pas_ki = f_t_pas_ki or
			_i(t[ctype], 't_pas_ki') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 't_pas_ki'), suffix_in_kanji, _i(t_note[ctype], 't_pas_ki')) or
			japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'adv_2') or _i(t[ctype], 2), suffix_in_kanji, _i(t_note[ctype], 't_pas_ki'), 'き')
		f_t_pas_ker = f_t_pas_ker or
			_i(t[ctype], 't_pas_ker') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 't_pas_ker'), suffix_in_kanji, _i(t_note[ctype], 't_pas_ker')) or
			japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'adv_2') or _i(t[ctype], 2), suffix_in_kanji, _i(t_note[ctype], 't_pas_ker'), 'けり')
		if string.sub(ctype, 1, 3) == 'adj' then
			f_adverb = f_adverb or
				_i(t[ctype], 'adverb') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'adverb'), suffix_in_kanji, _i(t_note[ctype], 'adverb')) or
				japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'adv_1') or _i(t[ctype], 2), suffix_in_kanji, _i(t_note[ctype], 'adverb'))
		else
			f_t_per_t = f_t_per_t or
				_i(t[ctype], 't_per_t') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 't_per_t'), suffix_in_kanji, _i(t_note[ctype], 't_per_t')) or
				japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'adv_2') or _i(t[ctype], 2), suffix_in_kanji, _i(t_note[ctype], 't_per_t'), 'つ')
			f_t_per_n = f_t_per_n or
				_i(t[ctype], 't_per_n') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 't_per_n'), suffix_in_kanji, _i(t_note[ctype], 't_per_n')) or
				japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'adv_2') or _i(t[ctype], 2), suffix_in_kanji, _i(t_note[ctype], 't_per_n'), 'ぬ')
			f_t_percon = f_t_percon or
				_i(t[ctype], 't_percon') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 't_percon'), suffix_in_kanji, _i(t_note[ctype], 't_percon')) or
				japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'adv_2') or _i(t[ctype], 2), suffix_in_kanji, _i(t_note[ctype], 't_percon'), 'たり')
			f_volit = f_volit or
				_i(t[ctype], 'volit') and japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'volit'), suffix_in_kanji, _i(t_note[ctype], 'volit')) or
				japanese_conj_row_classical_auto(stem, kanji, _i(t[ctype], 'irr_1') or _i(t[ctype], 1), suffix_in_kanji, _i(t_note[ctype], 'volit'), 'む')
		end
	end

	f_irr = f_irr and f_irr__p and f_irr ..';'.. f_irr__p or f_irr or f_irr__p
	f_adv = f_adv and f_adv__p and f_adv ..';'.. f_adv__p or f_adv or f_adv__p
	f_ter = f_ter and f_ter__p and f_ter ..';'.. f_ter__p or f_ter or f_ter__p
	f_adn = f_adn and f_adn__p and f_adn ..';'.. f_adn__p or f_adn or f_adn__p
	f_rea = f_rea and f_rea__p and f_rea ..';'.. f_rea__p or f_rea or f_rea__p
	f_imp = f_imp and f_imp__p and f_imp ..';'.. f_imp__p or f_imp or f_imp__p
	f_neg = f_neg and f_neg__p and f_neg ..';'.. f_neg__p or f_neg or f_neg__p
	f_conj_contr = f_conj_contr and f_conj_contr__p and f_conj_contr ..';'.. f_conj_contr__p or f_conj_contr or f_conj_contr__p
	f_conj_caus = f_conj_caus and f_conj_caus__p and f_conj_caus ..';'.. f_conj_caus__p or f_conj_caus or f_conj_caus__p
	f_conj_cond = f_conj_cond and f_conj_cond__p and f_conj_cond ..';'.. f_conj_cond__p or f_conj_cond or f_conj_cond__p
	f_t_pas_ki = f_t_pas_ki and f_t_pas_ki__p and f_t_pas_ki ..';'.. f_t_pas_ki__p or f_t_pas_ki or f_t_pas_ki__p
	f_t_pas_ker = f_t_pas_ker and f_t_pas_ker__p and f_t_pas_ker ..';'.. f_t_pas_ker__p or f_t_pas_ker or f_t_pas_ker__p
	f_t_per_t = f_t_per_t and f_t_per_t__p and f_t_per_t ..';'.. f_t_per_t__p or f_t_per_t or f_t_per_t__p
	f_t_per_n = f_t_per_n and f_t_per_n__p and f_t_per_n ..';'.. f_t_per_n__p or f_t_per_n or f_t_per_n__p
	f_t_percon = f_t_percon and f_t_percon__p and f_t_percon ..';'.. f_t_percon__p or f_t_percon or f_t_percon__p

	local result = '<div class="NavFrame" style="border:0px;">'
		.. '<div class="NavHead" align="left" style="font-size:105%; border:1px solid #aaaaaa; background-color:#CCCCFF;" cellpadding="3">'
		.. 'การผันรูปแบบคลาสสิกของ "<span class="Jpan" lang="ja-Jpan">'..rm_spaces_hyphens(lemma)..'</span>" '
		.. (conj_name[ctype] and '('.._l(conj_name[ctype])..', ดูเพิ่ม [[ภาคผนวก:คำกริยาภาษาญี่ปุ่น]])</div>' or '(ไม่ปรกติ)</div>')
		.. '<div class="NavContent" style="font-size:105%;">\n'
		.. '{| class="wikitable" style="margin-top: 0; text-align: center; width: 100%" rules="all"\n|-\n! colspan="4" | Stem forms\n|-\n! style="width:20%" |'
		.. 'Irrealis ('.._l('未然形')..')\n'
		.. japanese_conj_row_classical_manual(f_irr)
		.. '\n|-\n!Continuative ('.._l('連用形')..')\n'
		.. japanese_conj_row_classical_manual(f_adv)
		.. '\n|-\n!Terminal ('.._l('終止形')..')\n'
		.. japanese_conj_row_classical_manual(f_ter)
		.. '\n|-\n!Attributive ('.._l('連体形')..')\n'
		.. japanese_conj_row_classical_manual(f_adn)
		.. '\n|-\n!Realis ('.._l('已然形')..')\n'
		.. japanese_conj_row_classical_manual(f_rea)
		.. '\n|-\n!Imperative ('.._l('命令形')..')\n'
		.. japanese_conj_row_classical_manual(f_imp)
	
		.. (not hide_key_constructions and '\n|-\n! colspan="4" | Key constructions' or '')

		.. (f_neg and '\n|-\n! Negative\n' .. japanese_conj_row_classical_manual(f_neg) or '')
		.. (f_conj_contr and '\n|-\n! Contrasting conjunction\n' .. japanese_conj_row_classical_manual(f_conj_contr) or '')
		.. (f_conj_caus and '\n|-\n! Causal conjunction\n' .. japanese_conj_row_classical_manual(f_conj_caus) or '')
		.. (f_conj_cond and '\n|-\n! Conditional conjunction\n' .. japanese_conj_row_classical_manual(f_conj_cond) or '')
		.. (f_t_pas_ki and '\n|-\n! Past tense (firsthand knowledge)\n' .. japanese_conj_row_classical_manual(f_t_pas_ki) or '')
		.. (f_t_pas_ker and '\n|-\n! Past tense (secondhand knowledge)\n' .. japanese_conj_row_classical_manual(f_t_pas_ker) or '')
		.. (f_t_per_t and '\n|-\n! Perfect tense (conscious action)\n' .. japanese_conj_row_classical_manual(f_t_per_t) or '')
		.. (f_t_per_n and '\n|-\n! Perfect tense (natural event)\n' .. japanese_conj_row_classical_manual(f_t_per_n) or '')
		.. (f_t_percon and '\n|-\n! Perfect-continuative tense\n' .. japanese_conj_row_classical_manual(f_t_percon) or '')
		.. (f_adverb and '\n|-\n! Adverbial\n' .. japanese_conj_row_classical_manual(f_adverb) or '')
		.. (f_volit and '\n|-\n! Volitional\n' .. japanese_conj_row_classical_manual(f_volit) or '')

	if note_num > 0 then
		result = result .. '\n|-\n| style="text-align:left;" bgcolor="#FFFFFF" colspan="5" |<small>'
		for i, v in ipairs(notes) do
			result = result .. '<sup>[' .. i .. ']</sup>' .. v .. '<br/>'
		end
		result = result .. '</small>'
	end

	result = result .. '\n|}</div></div>'
	
	return result
end

return export