-- Sample Module demonstrating how to access arguments.
-- For more about the Frame object, see http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object
-- Unit tests at Module:BananasArgs/testcases

local p = {}

-- No arguments, used like: {{#invoke:PisangArgs|hello_world}}
function p.hai_semua()
	return "Hai, semua!"
end

-- One argument, used like: {{#invoke:PisangArgs|hello|Fred}} 
function p.hai(frame)
	local name = frame.args[1] -- in this example, args[1] is the word Fred 
	return "Hai, " .. name .. "!" -- .. name .. replace by the word Fred
end

-- Two arguments, used like: {{#invoke:PisangArgs|tambah|5|3}}
function p.tambah(frame)
	local num1 = tonumber(frame.args[1])
	local num2 = tonumber(frame.args[2])
	return num1 + num2
end

-- Named arguments, used like: {{#invoke:PisangArgs|count_fruit|bananas=5|apples=3}}
function p.hitung_semua(frame)
	local num_pisang = frame.args.pisang
	local num_donat = frame.args.donat
	return 'Aku punya ' .. num_pisang .. ' pisang dan ' .. num_donat .. ' donat'
end

-- Mixing regular args with named args and optional named args
-- Used like: {{#invoke:PisangArgs|has_fruit|Fred|bananas=5|cherries=7}}
function p.punya_semua(frame)
	local name = frame.args[1]
	local num_pisang = frame.args.pisang
	local num_donat = frame.args.donat
	local num_pesawat = frame.args.pesawat
	
	local result = name .. ' punya:'
	if num_pisang then result = result .. ' ' .. num_pisang .. ' pisang' end
	if num_donat then result = result .. ' ' .. num_donat .. ' donat' end
	if num_pesawat then result = result .. ' ' .. num_pesawat .. ' pesawat' end
	return result
end

-- Iterating over args, used like: {{#invoke:PisangArgs|custom_fruit|pineapples=10|kiwis=5}}
function p.racik_semua_bebas(frame)
	local result = 'Aku punya:'
	for name, value in pairs(frame.args) do
		result = result .. ' ' .. value .. ' ' .. name
	end
	return result
end

-- Iterating over args with separate mandatory args
-- Used like: {{#invoke:BananasArgs|custom_fruit_2|Fred|pineapples=10|kiwis=5}}
function p.racik_semua_bebas_2(frame)
	local name = frame.args[1]
	local result = name .. ' punya:'
	for name, value in pairs(frame.args) do
		if name ~= 1 then
			result = result .. ' ' .. value .. ' ' .. name
		end
	end
	return result
end

return p