h = require("Module:HtmlEntities");

local p = {};

local function convertBaybayin(latin)
	str = latin.args[1];
	str = mw.ustring.gsub(str, "=", "="); -- escape equal signs
	str = mw.text.killMarkers(str); --needed, or else strip markers will be visible, but all <ref> tags and others will be removed
	
	-- ESCAPE ~ AND ~ ENCODE
	str = mw.ustring.gsub(str, "%b~~", function (a)	
			return "~"..h.encode(mw.ustring.gsub(a, "[~~]", "")).."~";
		end
	);
	
	-- HTML CHARACTERS ENCODE
	str = mw.ustring.gsub(str, "%b<>", function (a)	
			return "<"..h.encode(mw.ustring.gsub(a, "[<>]", ""))..">";
		end
	);

	-- WIKI LINKS ENCODE
	str = mw.ustring.gsub(str, "%b[]", function (a)	
			-- return "[["..h.encode(mw.ustring.gsub(a, "[%[%]]", "")).."]]";   -- bugs gallore, temporary fix to just remove wiki links
			return "";
		end
	);

	--SPECIAL CASES GO FIRST
	str = mw.ustring.gsub(str, "[Mm][Gg][Aa]", "ᜋᜅ");
	str = mw.ustring.gsub(str, "[Cc][Aa][Ff][Ee]", "ᜃᜉᜒ");
	
	str = mw.ustring.gsub(str, "[Cc][Rr][Aa]", "ᜃ᜔ᜇ");
	str = mw.ustring.gsub(str, "[Cc][Rr][EeIi]", "ᜃ᜔ᜇᜒ");
	str = mw.ustring.gsub(str, "[Cc][Rr][OoUu]", "ᜃ᜔ᜇᜓ");
	str = mw.ustring.gsub(str, "[Cc][Rr]", "ᜃ᜔ᜇ᜔");
	
	str = mw.ustring.gsub(str, "[Cc][Ll][Aa]", "ᜃ᜔ᜎ");
	str = mw.ustring.gsub(str, "[Cc][Ll][EeIi]", "ᜃ᜔ᜎᜒ");
	str = mw.ustring.gsub(str, "[Cc][Ll][OoUu]", "ᜃ᜔ᜎᜓ");
	str = mw.ustring.gsub(str, "[Cc][Ll]", "ᜃ᜔ᜎ᜔");
	
	str = mw.ustring.gsub(str, "[Ññ][Aa]", "ᜈᜒᜌ");
	str = mw.ustring.gsub(str, "[Ññ][EeIi]", "ᜈᜒᜌᜒ");
	str = mw.ustring.gsub(str, "[Ññ][OoUu]", "ᜈᜒᜌᜓ");
	str = mw.ustring.gsub(str, "[Ññ]", "ᜈᜒᜌ᜔");
	
	
	-- STANDARD CONVERSION
	str = mw.ustring.gsub(str, "[Kk][Aa]", "ᜃ");
	str = mw.ustring.gsub(str, "[Kk][EeIi]", "ᜃᜒ");
	str = mw.ustring.gsub(str, "[Kk][OoUu]", "ᜃᜓ");
	str = mw.ustring.gsub(str, "[Kk]", "ᜃ᜔");

	str = mw.ustring.gsub(str, "[Nn][Gg][Aa]", "ᜅ");
	str = mw.ustring.gsub(str, "[Nn][Gg][EeIi]", "ᜅᜒ");
	str = mw.ustring.gsub(str, "[Nn][Gg][OoUu]", "ᜅᜓ");
	str = mw.ustring.gsub(str, "[Nn][Gg]", "ᜅ᜔");

	str = mw.ustring.gsub(str, "[Gg][Aa]", "ᜄ");
	str = mw.ustring.gsub(str, "[Gg][EeIi]", "ᜄᜒ");
	str = mw.ustring.gsub(str, "[Gg][OoUu]", "ᜄᜓ");
	str = mw.ustring.gsub(str, "[Gg]", "ᜄ᜔");

	str = mw.ustring.gsub(str, "[Tt][Aa]", "ᜆ");
	str = mw.ustring.gsub(str, "[Tt][EeIi]", "ᜆᜒ");
	str = mw.ustring.gsub(str, "[Tt][OoUu]", "ᜆᜓ");
	str = mw.ustring.gsub(str, "[Tt]", "ᜆ᜔");

	str = mw.ustring.gsub(str, "[Dd][Aa]", "ᜇ");
	str = mw.ustring.gsub(str, "[Dd][EeIi]", "ᜇᜒ");
	str = mw.ustring.gsub(str, "[Dd][OoUu]", "ᜇᜓ");
	str = mw.ustring.gsub(str, "[Rr][Rr]?[Aa]", "ᜇ");
	str = mw.ustring.gsub(str, "[Rr][Rr]?[EeIi]", "ᜇᜒ");
	str = mw.ustring.gsub(str, "[Rr][Rr]?[OoUu]", "ᜇᜓ");
	str = mw.ustring.gsub(str, "[DdRr][Rr]?", "ᜇ᜔");

	str = mw.ustring.gsub(str, "[Nn][Aa]", "ᜈ");
	str = mw.ustring.gsub(str, "[Nn][EeIi]", "ᜈᜒ");
	str = mw.ustring.gsub(str, "[Nn][OoUu]", "ᜈᜓ");
	str = mw.ustring.gsub(str, "[Nn]", "ᜈ᜔");
	
	str = mw.ustring.gsub(str, "[PpFf][Aa]", "ᜉ");
	str = mw.ustring.gsub(str, "[PpFf][EeIi]", "ᜉᜒ");
	str = mw.ustring.gsub(str, "[PpFf][OoUu]", "ᜉᜓ");
	str = mw.ustring.gsub(str, "[PpFf]", "ᜉ᜔");
	
	str = mw.ustring.gsub(str, "[BbVv][Aa]", "ᜊ");
	str = mw.ustring.gsub(str, "[BbVv][EeIi]", "ᜊᜒ");
	str = mw.ustring.gsub(str, "[BbVv][OoUu]", "ᜊᜓ");
	str = mw.ustring.gsub(str, "[BbVv]", "ᜊ᜔");

	str = mw.ustring.gsub(str, "[Mm][Aa]", "ᜋ");
	str = mw.ustring.gsub(str, "[Mm][EeIi]", "ᜋᜒ");
	str = mw.ustring.gsub(str, "[Mm][OoUu]", "ᜋᜓ");
	str = mw.ustring.gsub(str, "[Mm]", "ᜋ᜔");

	str = mw.ustring.gsub(str, "[Yy][Aa]", "ᜌ");
	str = mw.ustring.gsub(str, "[Yy][EeIi]", "ᜌᜒ");
	str = mw.ustring.gsub(str, "[Yy][OoUu]", "ᜌᜓ");
	str = mw.ustring.gsub(str, "[Yy]", "ᜌ᜔");

	str = mw.ustring.gsub(str, "[Ll][Aa]", "ᜎ");
	str = mw.ustring.gsub(str, "[Ll][EeIi]", "ᜎᜒ");
	str = mw.ustring.gsub(str, "[Ll][OoUu]", "ᜎᜓ");
	str = mw.ustring.gsub(str, "[Ll]", "ᜎ᜔");

	str = mw.ustring.gsub(str, "[Ww][Aa]", "ᜏ");
	str = mw.ustring.gsub(str, "[Ww][EeIi]", "ᜏᜒ");
	str = mw.ustring.gsub(str, "[Ww][OoUu]", "ᜏᜓ");
	str = mw.ustring.gsub(str, "[Ww]", "ᜏ᜔");

	str = mw.ustring.gsub(str, "[SsZz][Aa]", "ᜐ");
	str = mw.ustring.gsub(str, "[SsZz][EeIi]", "ᜐᜒ");
	str = mw.ustring.gsub(str, "[SsZz][OoUu]", "ᜐᜓ");
	str = mw.ustring.gsub(str, "[SsZz]", "ᜐ᜔");

	str = mw.ustring.gsub(str, "[HhJj][Aa]", "ᜑ");
	str = mw.ustring.gsub(str, "[HhJj][EeIi]", "ᜑᜒ");
	str = mw.ustring.gsub(str, "[HhJj][OoUu]", "ᜑᜓ");
	str = mw.ustring.gsub(str, "[HhJj]", "ᜑ᜔");

	-- STANDALONE VOWELS LAST
	str = mw.ustring.gsub(str, "[Aa]", "ᜀ");
	str = mw.ustring.gsub(str, "[EeIi]", "ᜁ");
	str = mw.ustring.gsub(str, "[OoUu]", "ᜂ");
	
	-- punctuation mark replacement, may be controversial so temporarily removed
	-- str = mw.ustring.gsub(str, "[%.%!%?]", "᜶");
	-- str = mw.ustring.gsub(str, "[%,]", "᜵");

	-- ESCAPE ~ AND ~ DECODE
	str = mw.ustring.gsub(str, "%b~~", function (a)	
			return h.decode(mw.ustring.gsub(a, "[~~]", ""));
		end
	);

	-- HTML CHARACTERS DECODE
	str = mw.ustring.gsub(str, "%b<>", function (a)	
			return "<"..h.decode(mw.ustring.gsub(a, "[<>]", ""))..">";
		end
	);
	
	-- WIKI LINKS DECODE
	-- str = mw.ustring.gsub(str, "%b[]", function (a)	
	-- 		return "[["..h.decode(mw.ustring.gsub(a, "[%[%]]", "")).."]]";
	-- 	end
	-- );

	return mw.getCurrentFrame():extensionTag (
		'templatestyles', '', {src='Module:Baybayin/styles.css'}) .. "\n" .. str; 
end

p.convertBaybayin = convertBaybayin;

function p.removeEscape(text)
	return mw.ustring.gsub(text.args[1], "%b~~", function (a)	
			return mw.ustring.gsub(a, "~", "");
		end
	).."";
end

return p;