require('strict')

--[=[
Implements PD templates
]=]
local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local error_function = require('Module:Error')['error']
local yesno = require('Module:Yesno')

-- utility definitions
local licenseScopeModule = require('Module:License_scope')
p.license_scope = licenseScopeModule._license_scope
p.license_grammar = licenseScopeModule._license_grammar

p.license = require('Module:License')._license

local licenseWikidataModule = require('Module:License Wikidata')
function p.getAuthorDeathYear(years)
	return licenseWikidataModule.getWorkCreatorOrAuthorDeathYear({['deathyears'] = years})
end
function p.getPublicationYear(years)
	return licenseWikidataModule.getWorkOrAuthorPublicationYear({['pubyears'] = years})
end

p.currentyear = tonumber(os.date("%Y"))
p.currentmonth = tonumber(os.date("%m"))
p.currentday = tonumber(os.date("%d"))
p.PD_US_cutoff = math.min(p.currentyear - 95, 1978)

p.namespace = mw.title.getCurrentTitle().nsText
p.is_author_namespace = p.namespace == 'Author' or p.namespace == 'Author talk'

p.PD_image = 'PD-icon.svg'
p.US_flag_image = 'Flag of the United States.svg'

--[=[
Generates error license
]=]
function p.error_text(text, basecat)
	local category
	if basecat then
		category = basecat .. "-possible-copyright-violations"
	else
		category = "Possible copyright violations"
	end
	return p.license({
		['image'] = 'PDmaybe-icon.svg',
		['category'] = category,
		['text'] = error_function({text})
	})
end

--[=[
Handle year bucketing
]=]
function p.year_floor(cutoffs, year)
	if year then
		table.sort(cutoffs, function(a, b) return a > b end)
		for k, cutoff in pairs(cutoffs) do
			if p.currentyear - year > cutoff then
				return cutoff
			end
		end
	end
	return nil
end

--[=[
[category]-[year bucket] or [category]
]=]
function p.category_with_year_floor(category, cutoffs, year)
	local year_floor = p.year_floor(cutoffs, year)
	if year_floor then
		return category .. "-" .. year_floor
	else
		return category
	end
end

--[=[
[category]-old-[year bucket] or [category]
]=]
function p.category_with_deathyear_floor(category, year)
	local year_floor = p.year_floor({100, 99, 96, 95, 80, 75, 70, 60, 50, 30, 25}, year)
	if year_floor then
		return category .. "-old-" .. year_floor
	else
		return category
	end
end

function p.frame_category_with_deathyear_floor(frame)
	local args = getArgs(frame)
	return p.category_with_deathyear_floor(args[1] or args.category, args[2] or args.year or args.deathyear)
end

--[=[
Text about where else a work is PD
]=]

function p.shorter_term_text(deathyear, film)
	deathyear = p.getAuthorDeathYear({deathyear})
	film = yesno(film or 'no')
	
	local text = "\n----\n"
	if deathyear and deathyear <= p.currentyear then
		local deathyear_display = deathyear
		if deathyear <= 0 then
			deathyear_display = (-deathyear + 1) .. " BCE"
		end
		
		if p.is_author_namespace then
			text = text .. "This author"
		elseif film then
			text = text .. "Copyright law abroad tends to consider the following people authors of a film:\n* The principal director\n* The screenwriter, and/or other writers of dialogue\n* The composer/lyricist (if the film is accompanied by sound)\n*The cinematographer\n* By extension, the authors of any works that may serve as the basis for a film's plot\n\nThe longest-living of these authors"
		else
			text = text .. "The longest-living author of " .. p.license_grammar({"this work", "these works"})
		end
		
		text = text .. " died in " .. deathyear_display .. ", so "
		
		if p.is_author_namespace then
			text = text .. 'works by this author are'
		else
			text = text .. p.license_grammar({"this work is", "these works are"})
		end
		
		text = text .. " in the '''public domain''' in countries and areas where the copyright term is the author's '''life plus " .. p.currentyear - deathyear - 1 .. " "
		if p.currentyear - deathyear - 1 == 1 then
			text = text .. "year"
		else
			text = text .. "years"
		end
		text = text .. " or less'''. "
	end
	text = text .. p.license_grammar({"This work", "These works"}) .. " may be in the '''public domain''' in countries and areas with longer native copyright terms that apply the '''[[w:Rule of the shorter term|rule of the shorter term]]''' to ''foreign works''."
	return text
end

return p