local export = {}

function export.exists(frame)
	local args = frame.args
	local sc = args[1] or error("Script code has not been specified. Please pass parameter 1 to the module invocation.")
	
	sc = require("Module:scripts").getByCode(sc)
	
	if sc then
		return "1"
	else
		return ""
	end
end

function export.getByCode(frame)
	local args = frame.args
	local sc = require("Module:scripts").getByCode(args[1], 1, "disallow nil")

	return require("Module:language-like").templateGetByCode(sc, args,
		function(itemname)
			if itemname == "countCharacters" then
				local text = args[3] or ""
				return sc:countCharacters(text)
			end
		end
	)
end

function export.getByCanonicalName(frame)
	local args = frame.args
	local sc = args[1] or error("Script name (parameter 1) has not been specified.")
	
	sc = require("Module:scripts").getByCanonicalName(sc)
	
	if sc then
		return sc:getCode()
	else
		return "None"
	end
end

function export.findBestScript(frame)
	local args = frame.args
	local text = args[1] or error("Text to analyse (parameter 1) has not been specified.")
	local lang = args[2] or error("Language code (parameter 2) has not been specified.")
	local force_detect = args.force_detect; if force_detect == "" then force_detect = nil end
	local getCanonicalName = args[3] == "getCanonicalName"
	
	local sc = require("Module:languages").getByCode(lang, true):findBestScript(text, force_detect)
	
	if getCanonicalName then
		return sc:getCanonicalName()
	else
		return sc:getCode()
	end
end

return export