This module implements the reference template {{R:map:Trussel2}}.


--[[
	This module handles {{R:map:ACD}} and {{R:poz-mic:MCD}}.
--]]

local export = {};

local gsub = mw.ustring.gsub;
local find = mw.ustring.find;

local function format_nums(str)
	return gsub(str, "[1-9]$", {
		["1"] = "₁", ["2"] = "₂", ["3"] = "₃",
		["4"] = "₄", ["5"] = "₅", ["6"] = "₆",
		["7"] = "₇", ["8"] = "₈", ["9"] = "₉",
	});
end

local function get_initial(entry, add_one, replace_token, replace, replace_table)
	entry = format_nums(entry);
	local initial = gsub(entry, ("-?(.%s).+"):format(replace_token), "%1");
	if (find(initial, ("^[%s]%s$"):format(add_one, replace_token))) then
		initial = initial .. "1";
	elseif (find(initial, ("^[%s]%s$"):format(replace, replace_token))) then
		initial = replace_table[initial];
	end
	return entry, initial;
end

function export.ACD(frame)

	local args = require("Module:parameters").process(frame:getParent().args, {
		entry = { }, [1] = { alias_of = "entry" },
		display = { }, [2] = { alias_of = "display" },
		type = { },
		id = { },
		passage = { },
	});

	local entry = args.entry;

	-- Retrieves the entry name from Proto-Austronesian entries if |1= is not provided.
	if (not entry) then
		local title, isPAN = gsub(mw.title.getCurrentTitle().text, "^Proto-Austronesian/", "");
		if (isPAN == 1) then
			entry = title;
		end
	end

	local cite_args = {
		title = "The Austronesian Comparative Dictionary",
		first = "Robert", last = "Blust",
		first2 = "Stephen", last2 = "Trussel",
		year = "2010–",
	}

	-- Finds out the index under which the site has the entry.
	if (entry) then
		local initial, display_text;
		local display = args.display;
		if (args.type == "n" or args.type == "lo") then
			display_text = entry;
			entry = gsub(entry, " ", "");
			initial = mw.ustring.sub(entry, 1, 1);
		else
			entry, initial = get_initial(entry, "aceinrsu", "", "áíúCRSNñŋ", {
				["á"] = "a1", ["í"] = "i1", ["ú"] = "u1",
				["C"] = "c2", ["R"] = "r2", ["S"] = "s2",
				["N"] = "n2", ["ñ"] = "n3", ["ŋ"] = "n4",
			});
			display_text = "*" .. entry;
			if (display) then
				display = format_nums(display);
				display_text = ("%s § *%s"):format(display_text, display);
			end
		end
		cite_args.entry = display_text;
		cite_args.entryurl = ("http://www.trussel2.com/ACD/acd-%s_%s.htm#%s"):format(
			args.type or "s",
			initial,
			args.id or display or entry
		);
	else
		cite_args.url = "http://www.trussel2.com/ACD/";
	end

	return frame:expandTemplate {
		title = "cite-book",
		args = cite_args,
	};

end

function export.MCD(frame)

	local args = require("Module:parameters").process(frame:getParent().args, {
		entry = { }, [1] = { alias_of = "entry" },
		id = { },
		lang = { }, l = { alias_of = "lang" },
		passage = { },
		page = { }, pages = { },
	});

	local cite_args = {
		title = "Proto-Micronesian Reconstructions: I",
		first = "Bryon W.", last = "Bender",
		first2 = "Ward H.", last2 = "Goodenough",
		author3 = "et al.",
		year = "2003", journal = "Oceanic Linguistics", volume = "42", issue = "1",
		jstor = "3623449",
		page = args.page, pages = args.pages,
		passage = args.passage,
	};

	if (not args.page and not args.pages) then
		cite_args.page = frame:expandTemplate {
			title = "maintenance line",
			args = { "please specify page" },
		};
	end

	if (entry) then
		local initial;
		entry, initial = get_initial(entry, "mnpst", "ʷ?", "mñŋpST", {
			["mʷ"] = "m2", ["pʷ"] = "p2",
			["ñ"] = "n2", ["ŋ"] = "n3",
			["S"] = "s2", ["T"] = "t2",
		});
		local language = "";
		if (args.lang) then
			if (find(args.lang, "^[CW]$")) then language = args.lang;
			else error(("Language <%s> not supported!"):format(args.lang)); end
		end
		cite_args.entry = ("P%sMc *%s"):format(language, entry);
		cite_args.entryurl = ("http://www.trussel2.com/MCD/pmc-sets-%s.htm#%s"):format(initial, entry or args.id);
	else
		cite_args.url = "http://www.trussel2.com/MCD/";
	end

	return frame:expandTemplate {
		title = "cite-journal",
		args = cite_args,
	};

end

return export;