}}


require("strict")

local p = {}

local getArgs

local function delinkReversePipeTrick(s)
	if s:match("^%[%[|.*[|\n]") then 
		return s
	end
	return s:match("%[%[|(.*)%]%]")

end

local function delinkPipeTrick(s)
	-- 
	if s:match(":") then
		s = s:match("%[%[.-:(.*)|%]%]")
	-- 
	else
		s = s:match("%[%[(.*)|%]%]")
	end
	
	-- 
	if s:match("%(.-%)$") then -- 
		s = s:match("(.-) ?%(.-%)$")
	elseif s:match(",") then -- 
		s = s:match("(.-),.*$")
	end
	return s
end

-- 
local function getDelinkedTarget(s)
	local result = s
	-- 
	if result:match("%[%[|") then
		return delinkReversePipeTrick(result)
	end
	
	result = mw.uri.decode(result, "PATH") -- 
	result = mw.text.decode(result, true) -- 
	
	-- 
	local target_area
	if result:match("|") then -- 
		target_area = result:match("^%[%[(.-)|.*%]%]")
	else
		target_area = result:match("^%[%[(.-)%]%]")
	end

	--
	if mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") and mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") ~= "?" then
		return s
	end
	
	return target_area
end

local function getDelinkedLabel(s)
	local result = s
	-- 
	if result:match("%[%[|") then
		return delinkReversePipeTrick(result)
	end

	result = mw.uri.decode(result, "PATH") -- 
	result = mw.text.decode(result, true) -- 

	-- 
	local target_area
	if result:match("|") then -- 
		target_area = result:match("^%[%[(.-)|.*%]%]")
	else
		target_area = result:match("^%[%[(.-)%]%]")
	end

	-- 
	if mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") and mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") ~= "?" then
		return s
	end

	-- 
	local colon_prefix = result:match("%[%[(.-):.*%]%]") or "" -- 
	local ns = mw.site.namespaces[colon_prefix] -- 
	if mw.language.isKnownLanguageTag(colon_prefix) or (ns and (ns.canonicalName == "File" or ns.canonicalName == "Category")) then
		return ""
	end

	--
	if result:match("%[%[:") then
		result = "[[" .. result:match("%[%[:(.*%]%])")
	end

	-- 
	if mw.ustring.match(result, "^%[%[[^|]*|%]%]") then
		return delinkPipeTrick(result)
	end
	
	-- 
	if result:match("|") then -- 
		result = result:match("^%[%[.-|(.+)%]%]")
		-- 
		result = result:gsub("\n", "")
	else
		result = result:match("^%[%[(.-)%]%]")
	end

	return result
end

local function delinkURL(s)
	
	if s:match("\n") then
		return s
	end
	
	local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"} 
	local url_prefix
	for _ ,v in ipairs(valid_url_prefixes) do
		if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
			url_prefix = v
			break
		end
	end
	
	-- Get display text
	if not url_prefix then
		return s
	end
	s = s:match("^%[" .. url_prefix .. "(.*)%]") 
	s = s:match('^.-(["<> ].*)') or "" 
	s = mw.ustring.match(s, "^%s*(%S.*)$") or "" 
	
	local s_decoded = mw.text.decode(s, true)
	if mw.ustring.match(s_decoded, "%c") then
		return s
	end

	return s_decoded

end

local function delinkLinkClass(text, pattern, delinkFunction)
	if type(text) ~= "string" then
		error("Attempt to de-link non-string input.", 2)
	end
	if type(pattern) ~= "string" or mw.ustring.sub(pattern, 1, 1) ~= "^" then
		error('Invalid pattern detected. Patterns must begin with "^".', 2)
	end
	
	local result = ""
	while text ~= "" do
	
		text = mw.ustring.gsub(text, pattern, delinkFunction, 1)
		
		result = result .. mw.ustring.sub(text, 1, 1)
		text = mw.ustring.sub(text, 2, -1)
	end
	return result
end

function p._delink(args)
	local text = args[1] or ""
	if args.refs == "yes" then
		
		text = mw.ustring.gsub(text, "UNIQ%w*%-ref%-%d*%-QINU", "")
	end
	if args.comments ~= "no" then
		text = text:gsub("<!%-%-.-%-%->", "") 
	end

	if args.wikilinks ~= "no" and args.wikilinks ~= "target" then
	
		text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedLabel)
	elseif args.wikilinks == "target" then
		
		text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedTarget)
	end
	if args.urls ~= "no" then
		text = delinkLinkClass(text, "^%[.-%]", delinkURL)  
	end
	if args.whitespace ~= "no" then
		
		text = mw.ustring.gsub(text, "([^\n \t][ \t]*)\n([ \t]*[^\n \t])", "%1 %2")
		text = text:gsub("[ \t]+", " ")  
	end
	return text
end

function p.delink(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	return p._delink(getArgs(frame, {wrappers = 'Andoza:Delink'}))
end

return p