local export = {numbers = {}}

-- Automatically create new subtables of export.numbers through __index,
-- automatically add new fields of export.numbers
-- without overwriting old ones through __newindex.
local actual_numbers = export.numbers

local namespace = mw.title.getCurrentTitle().nsText
local function log(...)
	if namespace == "Module" then
		mw.log(...)
	end
end

local Georgian = require "Module:languages".getByCode "ka"
local proxy_number_metatable = {
	__newindex = function (self, k, v)
		local old = rawget(self.__actual, k)
		if old and old ~= v then
			local old, v = Georgian:transliterate(old), Georgian:transliterate(v)
			log("k: " .. k .. "; old " .. old .. "; new " .. v .. "; old == new: " .. tostring(old == v))
		end
		
		if type(old) == "table" then
			table.insert(old, v)
		elseif type(old) == "string" then
			if old ~= v then
				rawset(self.__actual, k, { old, v })
			end
		else
			rawset(self.__actual, k, v)
		end
	end,
	__index = function (self, k)
		return rawget(self.__actual, k)
	end,
}

local proxy_subtables = {}
local function get_proxy_number_table(k, actual_table)
	local t = proxy_subtables[k]
	if not t then
		t = setmetatable({ __actual = actual_table }, proxy_number_metatable)
		proxy_subtables[k] = t
	end
	return t
end

local function get_actual_number_table(k)
	local t = actual_numbers[k]
	if not t then
		t = {}
		actual_numbers[k] = t
	end
	return t
end

local proxy_numbers = setmetatable({}, {
	__newindex = function (self, k1, fields)
		local subtable = get_actual_number_table(k1)
		local proxy_subtable = get_proxy_number_table(k1, subtable)
		if not proxy_subtable then
			proxy_subtable = new_proxy_number_table(subtable)
			mw.log("new proxy_subtable for " .. k1)
			rawset(proxy_subtables, k1, proxy_subtable)
		end
		for k, v in pairs(fields) do
			proxy_subtable[k] = v
		end
	end,
	__index = function (self, k)
		local actual_table = get_actual_number_table(k)
		local proxy_table = get_proxy_number_table(k, actual_table)
		return proxy_table
	end,
})
local numbers = proxy_numbers

local adverbial_suffix = "ჯერ"
local multiplier_suffix = "მაგი"
local distributive_suffix = "მაგად"
local collective_suffix = "ვე"
local fractional_suffix = "დი"

numbers[0].cardinal = "ნული"

numbers[1] = {
	cardinal = "ერთი",
	ordinal = "პირველი",
	multiplier = "ერთმაგი",
	distributive = "ერთმაგად",
	adverbial = {"ერთხელ", "ერთჯერ" },
	collective = "ერთივე",
	fractional = { "მთლიანი", "სრული", "ერთიანი" },
}

numbers[2] = {
	cardinal = "ორი",
	fractional = "ნახევარი", -- regular fractional added below
}

numbers[3].cardinal = "სამი"
numbers[4].cardinal = "ოთხი"
numbers[5].cardinal = "ხუთი"
numbers[6].cardinal = "ექვსი"
numbers[7].cardinal = "შვიდი"
numbers[8].cardinal = "რვა"
numbers[9].cardinal = "ცხრა"
numbers[10].cardinal = "ათი"
numbers[11].cardinal = "თერთმეტი"
numbers[12].cardinal = "თორმეტი"
numbers[13].cardinal = "ცამეტი"
numbers[14].cardinal = "თოთხმეტი"
numbers[15].cardinal = "თხუთმეტი"
numbers[16].cardinal = "თექვსმეტი"
numbers[17].cardinal = "ჩვიდმეტი"
numbers[18].cardinal = "თვრამეტი"
numbers[19].cardinal = "ცხრამეტი"

local function remove_final_vowel(word)
	return (mw.ustring.gsub(word, "[აეიოუ]$", ""))
end

local function remove_final_i(word)
	return word:gsub("ი$", "")
end

local function get_cardinal(number)
	return numbers[number].cardinal
end

local function get(number, type)
	return numbers[number][type]
end

local function circumfix_ordinal(cardinal)
	return "მე" .. remove_final_vowel(cardinal) .. "ე"
end

for number = 2, 19 do
	numbers[number].ordinal = circumfix_ordinal(get_cardinal(number))
end

local twenty = "ოცი"
-- Add cardinals and ordinals for 20-99.
for i = 1, 4 do
	local twenties = i * 20
	local twenties_cardinal
	if i ~= 1 then
		twenties_cardinal = (remove_final_vowel(get_cardinal(i)) .. "მ" .. twenty)
			:gsub("მმ", "მ")
	else
		twenties_cardinal = twenty
	end
	numbers[twenties] = {
		cardinal = twenties_cardinal,
		ordinal = circumfix_ordinal(twenties_cardinal),
	}
	
	local twenties_and = remove_final_vowel(twenties_cardinal) .. "და"
	
	for ones = 1, 19 do
		numbers[twenties + ones] = {
			cardinal = twenties_and .. get_cardinal(ones),
			ordinal = twenties_and .. circumfix_ordinal(get_cardinal(ones)),
		}
	end
end

local function make_numerals(number, cardinal, ordinal)
	numbers[number].cardinal = cardinal
	
	ordinal = ordinal or circumfix_ordinal(cardinal)
	numbers[number].ordinal = ordinal
	
	numbers[number].adverbial = remove_final_i(cardinal) .. adverbial_suffix
	
	if number <= 1000 then
		numbers[number].multiplier = remove_final_i(cardinal) .. multiplier_suffix
	
		numbers[number].distributive = remove_final_i(cardinal) .. distributive_suffix
	end
	
	numbers[number].collective = cardinal .. collective_suffix
	
	numbers[number].fractional = remove_final_i(ordinal) .. fractional_suffix
end

for number = 2, 99 do
	make_numerals(number, get_cardinal(number), get(number, "ordinal"))
end

local hundred_cardinal = "ასი"
make_numerals(100, hundred_cardinal)

for i = 2, 10 do
	local cardinal = remove_final_vowel(get_cardinal(i)) .. hundred_cardinal
	make_numerals(i * 100, cardinal)
end

make_numerals(1000000, "მილიონი")

return export