Documentation for this module may be created at Module:Location/ga/doc

local p = {}

-- locations where any referent is redundant or innecessary
function p.namingExceptions(id)
	local exc_id = {
		['Q61'] = true, -- Washington, D.C., USA
	}
	return exc_id[id]
end

---- Cases by state

-- Australia, by state/territory
local function namingLocationAUS(qid, label, c_date, main)
	local adm = main.fetchAdministrativeEntity(qid, 'en')
	local adm_entity = main.getAdmByID(adm, 'Q5852411', label) -- state of Australia
	if adm_entity == nil then
		adm_entity = main.getAdmByID(adm, 'Q14192234', label) -- mainland territory of Australia
		if adm_entity == nil then
			adm_entity = main.getAdmByID(adm, 'Q11687019', label) -- external territory of Australia
		end
	end
	return main.addReferent(label, adm_entity or 'Astráil')
end

-- Canada, by province
local function namingLocationCA(qid, label, c_date, main)
	local adm = main.fetchAdministrativeEntity(qid, 'en')
	local adm_entity = main.getAdmByID(adm, 'Q11828004', label) -- province of Canada
	return main.addReferent(label, adm_entity or 'Ceanada')
end

-- UK, by country/territory
local function namingLocationUK(qid, label, c_date, main)
	if c_date < 1000 then
		return label
	end
	local adm = main.fetchAdministrativeEntity(qid, 'en')
	local adm_entity = main.getAdmByID(adm, 'Q3336843', label) -- constituent part of the United Kingdom
	if adm_entity == nil then
		if c_date < 1700 then
			return label
		else
			adm_entity = main.getAdmByID(adm, 'Q46395', label) -- British Overseas Territories
		end
	end
	return main.addReferent(label, adm_entity or 'Ríocht Aontaithe')
end

-- USA, by state/territory
local function namingLocationUSA(qid, label, c_date, main)
	local adm = main.fetchAdministrativeEntity(qid, 'en')
	local adm_entity = main.getAdmByID(adm, 'Q35657', label) -- state of the United States
	if adm_entity == nil then
		adm_entity = main.getAdmByID(adm, 'Q783733', label) -- unincorporated territory
	end
	return main.addReferent(label, adm_entity or 'SAM')
end

function p.namingLocation(state_id, loc_id, loc_label, context_date, main)
	if state_id == 'Q16' then -- Canada
		return namingLocationCA(loc_id, loc_label, context_date, main)
	elseif state_id == 'Q30' then -- USA
		return namingLocationUSA(loc_id, loc_label, context_date, main)
	elseif state_id == 'Q145' then -- United Kingdom
		return namingLocationUK(loc_id, loc_label, context_date, main)
	elseif state_id == 'Q408' then -- Australia
		return namingLocationAUS(loc_id, loc_label, context_date, main)
	end
	
	return
end

return p