require('strict')

local newBuffer = require('Module:OutputBuffer')
local mt = {}

function mt.__index(t, title)
	return function(frame)
		local getBuffer, print, printf = newBuffer()
		printf('{{%s', title)
		local ipairsArgs = {}
		for k,v in ipairs(frame.args) do
			if string.find(v, '=', 1, true) then
				break
			end
			ipairsArgs[k] = true
			printf('|%s', v)
		end
		for k,v in pairs(frame.args) do
			if not ipairsArgs[k] then
				printf('|%s=%s', string.gsub(k, '=', '{{=}}'), v)
			end
		end
		print('}}')
		local buffer = getBuffer()
		-- rather than calling expandTemplate with the title and args we have, call preprocess, so that our code example will always match our output, even in the cases of pipes or other things we should have escaped but didn't
		return '{| style="table-layout:fixed; width:100%; margin-top:0;margin-left:0; border-width:medium; padding:0; "\n'
			.. '!scope="col" style="width:50%;"| توصيف ||scope="col" style="width:50%;"| يظهر\n|-\n'
			..'|style="width:50%; background:#f9f9f9; border-width:1px;border-style:solid none none solid;border-color:#ddd;padding:9px 5px 5px; vertical-align:top;"|'
			..'<pre style="margin:0;border:none;padding:0; word-wrap:break-word;'
			..'white-space:-moz-pre-wrap;white-space:-o-pre-wrap;white-space:-pre-wrap;white-space:pre-wrap; ">'
			--.. '<syntaxhighlight lang="json">' .. mw.text.nowiki(buffer) ..'</syntaxhighlight></pre>'
			.. '' .. mw.text.nowiki(buffer) ..'</pre>'
			.. '\n|style="width:50%; background:#f9f9f9;border-width:1px;border-style:solid solid none solid;'
			..' border-color:#ddd; padding:5px; vertical-align:top;"| '
			.. '<span style="">' .. frame:preprocess(buffer) ..'</span>\n'
			..'|-\n|style="width:50%; border-width:1px;border-style:solid none none none; border-color:#ddd; padding:5px; vertical-align:top;"| \n'
			..'|style="width:50%; border-width:1px;border-style:solid none none none;border-color:#ddd; padding:5px; vertical-align:top;"| \n|}'
	end
end

return setmetatable({}, mt)