local p = {}

local function makeData(args)
	local data = {}

	-- Main table classes
	data.classes = {}
	if yesno(args.metadata) ~= false then
		table.insert
	end
	if args.position and args.position:lower == 'left' then
		table.insert
	elseif args['위치'] and args['위치']:lower == 'left' then
		table.insert
	else
		table.insert
	end
	
	if args.collapsible then
		table.insert
		if args.collapsible == "collapsed" then
			table.insert
		end
		data.collapsible = true
	end

	table.insert
	
	-- Image
	if args.image and args.image ~= 'none' then
		data.image = args.image
	elseif args['그림'] and args['그림'] ~= 'none' then
		data.image = args['그림']
	end
	
	-- we have to check to see if a downstream use has plainlist like
	-- Template:Sister_project. also it's the default. wikitext is :(
	if args.textclass == 'plainlist' or not args.textclass then
		data.textclass = 'plainlist'
		data.plainlist_templatestyles = 'Plainlist/styles.css'
	else
		data.textclass = args.textclass
	end

	-- Copy over data that does not need adjusting
	local argsToCopy = {
		-- aria qualities
		'role',
		'labelledby',

		-- Styles
		'style',
		'textstyle',
		'양식',
		'내용양식',
		'templatestyles',

		-- Above row
		'above',
		'abovestyle',
		'윗글',
		'윗글양식',

		-- Body row
		'text',
		'imageright',
		'내용',
		'오른쪽그림',

		-- Below row
		'below',
		'아랫글',
	for i, key in ipairs(argsToCopy) do
		data[key] = args[key]
	end

	return data
end

local function renderSidebox(data)
	-- Renders the sidebox HTML.

	-- Table root
	local root = mw.html.create('div')
	root:attr
	for i, class in ipairs(data.classes or {}) do
		root:addClass(class)
	end
	if (data.style or data['양식']) then
		root:cssText(data.style or data['양식'])
	end
	
	local frame = mw.getCurrentFrame
	if data.plainlist_templatestyles then
		root:wikitext(frame:extensionTag{
			name = 'templatestyles', args = { src = data.plainlist_templatestyles }
	end

	-- The "above" row
	if (data.above or data['윗글']) then
		local above = root:newline:tag('div')
		above:addClass('side-box-abovebelow')
		if (data.textstyle or data['내용양식']) then
			above:cssText(data.textstyle or data['내용양식'])
		end
		if (data.abovestyle or data['윗글양식']) then
			above:cssText(data.abovestyle or data['윗글양식'])
		end
	end

	-- The body row
	local body = root:newline:tag('div')
		body:addClass('side-box-flex')
	if (data.image or data['그림']) then
		body:tag('div')
	end
	local text = body:newline:tag('div')
	text:addClass('side-box-text')
	if (data.textstyle or data['내용양식']) then
		text:cssText(data.textstyle or data['내용양식'])
	end
	text:wikitext(data.text or data['내용'])
	if (data.imageright or data['오른쪽그림']) then
		body:newline:tag('div')
	end

	-- The below row
	if (data.below or data['아랫글']) then
		local below = root:newline:tag('div')
		below
		if (data.textstyle or data['내용양식']) then
			below:cssText(data.textstyle or data['내용양식'])
		end
	end

	root:newline
	local templatestyles = ''
	if data.templatestyles then
		templatestyles = frame:extensionTag{
			name = 'templatestyles', args = { src = data.templatestyles }
	end
	return frame:extensionTag{
		name = 'templatestyles', args = { src = 'Module:Side box/styles.css' }
end

function p._main(args)
	local data = makeData(args)
	return renderSidebox(data)
end

function p.main(frame)
	local origArgs = frame:getParent.args
	local args = {}
	for k, v in pairs(origArgs) do
		v = v:match('%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end