local export = {}
local m_ja = require("Modulo:ja")
local lang = require("Modulo:languages").getByCode("ja")
local sc = require("Modulo:scripts").getByCode("Jpan")
local m_links = require("Modulo:links")

function export.show(frame)
	local params = {
		[1] = { required = true },
		[2] = {},
		[3] = {},
		['gloss'] = { alias_of = 3 },
		['pos'] = {},
		['linkto'] = {},
		['caps'] = {},
		['rom'] = {},
	}
	local args = require("Modulo:parameters").process(frame:getParent().args, params)

	local lemma = args[1]
	local kana = args[2]
	-- if only one arg passed, it may be kana with hyphens
	if not kana then
		kana = lemma
		lemma = mw.ustring.gsub(lemma, '[%-^.]', '')
	end
	local gloss = args[3]
	local pos = args["pos"]
	local linkto = args["linkto"]
	local caps = args["caps"]
	local transliteration = args["rom"] or m_ja.kana_to_romaji(kana)

	if require("Modulo:scripts").getByCode('Hani'):countCharacters(transliteration) > 0 then
		error("The transliteration seems to have kanji in it. Was the kana reading properly provided?")
	end
	if caps then
		transliteration = mw.ustring.gsub(transliteration, "^%l", mw.ustring.upper)
		transliteration = mw.ustring.gsub(transliteration, " %l", mw.ustring.upper)
	end
	if transliteration ~= '-' then
		transliteration = "''[[" .. transliteration .. "]]''"
		--transliteration = m_links.remove_links(transliteration) serve se $lemma è linkato manualmente
	end

	local ruby = m_ja.add_ruby_backend(lemma, kana)
	lemma = mw.ustring.gsub(lemma, "[%% ]", "")

	if mw.ustring.match(ruby, '%[%[') then -- for if $lemma has manual wikilinks
		return m_links.full_link({lang = lang, term = nil, alt = ruby, gloss = gloss, sc = sc, tr = transliteration, pos = pos})
	elseif linkto then
		return m_links.full_link({lang = lang, term = linkto, alt = ruby, gloss = gloss, sc = sc, tr = transliteration, pos = pos})
	else
		return m_links.full_link({lang = lang, term = lemma, alt = ruby, gloss = gloss, sc = sc, tr = transliteration, pos = pos})
	end

end

return export