local export = {}

local links = require("Module:links")

local lang = require("Module:languages").getByCode("th")
local script = require("Module:scripts").getByCode("Thai")
local PAGENAME = mw.title.getCurrentTitle().text

local function contains(table, val)

	for i = 1, #table do
		if table[i] == val then 
			return true
		end
	end

	return false

end

local function get_alphagram(word)

	word = mw.ustring.upper(word) -- it may apply
	word = mw.ustring.gsub(word, "[%p%c%s…]", "")

	local charTbl = mw.text.split(word, "")
	table.sort(charTbl)
	return table.concat(charTbl, "")

end

function export.show(frame)

	local args = frame:getParent().args
	local head = ((frame.args[1] == "" or frame.args[1] == nil) and PAGENAME or frame.args[1])
	local alphagram = get_alphagram(head)

	local output = {}
	local list = require("Module:th-anagram/processed data")[alphagram]

	if list then
		for _, term in ipairs(list) do
			if term ~= head then
				table.insert(output, links.full_link({lang = lang, term = term, sc = script}))
			end
		end
	end

	return "<ul><li>" .. table.concat(output, ", ") .. "</li></ul>"

end

return export