--Expects a PAGENAME of the form "Some sequential 2000 example cat", where
-- firstpart = Some sequential
-- decade = 2000
-- lastpart = example cat
-- mindecade = 1800 ('min' decade parameter; optional; defaults to -9999)
-- maxdecade = 2020 ('max' decade parameter; optional; defaults to 9999)  
--sterilize dec
local dec = sterilizedec(decade)
if dec == nil then
errors = p.errorclass('Function nav_decade was sent "'..(decade or '')..'" as its 2nd parameter, '..
'but expects a 1 to 4-digit year ending in "0".')
return p.failedcat(errors, 'D')
end
local ndec = tonumber(dec)  
--sterilize mindecade & determine AD/BC
local mindefault = '-9999'
local mindec = sterilizedec(mindecade) --returns a tostring(unsigned int), or nil
if mindec then
if string.match(mindecade, '-%d') or
string.match(mindecade, 'கிமு')
then
mindec = '-'..mindec --better +/-0 behavior with strings (0-initialized int == "-0" string...)
end
elseif mindec == nil and mindecade and mindecade ~= '' then
errors = p.errorclass('Function nav_decade was sent "'..(mindecade or '')..'" as its 4th parameter, '..
'but expects a 1 to 4-digit year ending in "0", the earliest decade to be shown.')
return p.failedcat(errors, 'E')
else --mindec == nil
mindec = mindefault --tonumber later, after error checks
end  
--sterilize maxdecade & determine AD/BC
local maxdefault = '9999'
local maxdec = sterilizedec(maxdecade) --returns a tostring(unsigned int), or nil + error
if maxdec then
if string.match(maxdecade, '-%d') or
string.match(maxdecade, 'கிமு')
then --better +/-0 behavior with strings (0-initialized int == "-0" string...),
maxdec = '-'..maxdec --but a "-0" string -> tonumber -> tostring = "-0",
end --and a "0" string -> tonumber -> tostring = "0"
elseif maxdec == nil and maxdecade and maxdecade ~= '' then
errors = p.errorclass('Function nav_decade was sent "'..(maxdecade or '')..'" as its 5th parameter, '..
'but expects a 1 to 4-digit year ending in "0", the highest decade to be shown.')
return p.failedcat(errors, 'F')
else --maxdec == nil
maxdec = maxdefault
end  
local tspace = ' ' --assume trailing space for "1950s in X"-type cats
if string.match(lastpart, '^-') then tspace = '' end --DNE for "1970s-related"-type cats  
--AD/BC switches & vars  
local parentBC = string.match(lastpart, '^கிமு') --following the "0s BC" convention for all years BC
lastpart = mw.ustring.gsub(lastpart, '^கிமு%s*', '') --handle BC separately; AD never used
--TODO?: handle BCE, but only if it exists in the wild  
local dec0to40AD = (ndec >= 0 and ndec <= 40 and not parentBC) --special behavior in this range
local switchADBC = 1 -- 1=AD parent
if parentBC then switchADBC = -1 end -- -1=BC parent; possibly adjusted later
local BCdisp = ''
local D = -math.huge --secondary switch & iterator for AD/BC transition  
--check non-default min/max more carefully
if mindec ~= mindefault then
if tonumber(mindec) > ndec*switchADBC then
mindec = tostring(ndec*switchADBC) --input error; mindec should be <= parent
end
end
if maxdec ~= maxdefault then
if tonumber(maxdec) < ndec*switchADBC then
maxdec = tostring(ndec*switchADBC) --input error; maxdec should be >= parent
end
end
local nmindec = tonumber(mindec) --similar behavior to nav_year & nav_nordinal
local nmaxdec = tonumber(maxdec) --similar behavior to nav_nordinal  
--begin navdecade
local bnb = '' --/no
if navborder == false then --for Navseasoncats year and decade
bnb = ' -style: ; background-color: transparent;'
end
local navd = '{|\n'..'|\n'  
local i = -50 --nav position x 10
while i <= 50 do
local d = ndec + i*switchADBC  
local BC = ''
BCdisp = ''
if dec0to40AD then
if D < -10 then
d = math.abs(d + 10) --b/c 2 "0கள்" decades exist: "கிமு 0கள்" & "0கள்" (கிபி)
BC = 'கிமு '
if d == 0 then
D = -10 --track 1st d = 0 use (கிமு)
end
elseif D >= -10 then
D = D + 10 --now iterate from கிபி 0கள்
d = D --2nd d = 0 use
end
elseif parentBC then
if switchADBC == -1 then --parentBC looking at the BC side (the common case)
BC = 'கிமு '
if d == 0 then --prepare to switch to the AD side on the next iteration
switchADBC = 1 --1st d = 0 use (BC)
D = -10 --prep
end
elseif switchADBC == 1 then --switched to the AD side
D = D + 10 --now iterate from 0s AD
d = D --2nd d = 0 use (on first use)
end
end
if BC ~= '' and ndec <= 50 then
BCdisp = 'கிமு ' --show BC for all BC decades whenever a "0s" is displayed on the nav
end  
--determine target cat
local disp = BCdisp..d..'கள்'
local catlink = catlinkfollowr( , firstpart..' '..d..'கள்'..tspace..BC..lastpart, disp )
if catlink.rtarget then --a was followed
trackcat(18, 'Navseasoncats decade redirected')
end  
--populate / navd
local shown = navcenter(i, catlink)
local hidden = '<span>'..disp..'</span>'
local dsign = d --use d for display & dsign for logic
if BC ~= '' then dsign = -dsign end
if (nmindec <= dsign) and (dsign <= nmaxdec) then
if dsign == 0 and (nmindec == 0 or nmaxdec == 0) then --distinguish b/w -0 (BC) & 0 (AD)
--"zoom in" on +/- 0 and turn dsign/min/max temporarily into +/- 1 for easier processing
local zsign, zmin, zmax = 1, nmindec, nmaxdec
if BC ~= '' then zsign = -1 end
if mindec == '-0' then zmin = -1
elseif mindec == '0' then zmin = 1 end
if maxdec == '-0' then zmax = -1
elseif maxdec == '0' then zmax = 1 end  
if (zmin <= zsign) and (zsign <= zmax) then
navd = navd..shown
hidden = nil
else
navd = navd..'*'..hidden..'\n'
end
else
navd = navd..shown --the common case
hidden = nil
end
else
navd = navd..'*'..hidden..'\n'
end
if listall and hidden then
tlistall[#tlistall] = tlistall[#tlistall]..' ('..hidden..')'
end  
i = i + 10
end  
isolatedcat
if listall then
return listalllinks
else
return navd..'|}'
end
end  
--========================================================  
local function nav_decade_ta1( , firstpart, decade, lastpart, mindecade, maxdecade )
--Expects a PAGENAME of the form "Some sequential 2000 example cat", where
-- firstpart = Some sequential
-- decade = 2000
-- lastpart = example cat
-- mindecade = 1800 ('min' decade parameter; optional; defaults to -9999)
-- maxdecade = 2020 ('max' decade parameter; optional; defaults to 9999)  
--sterilize dec
local dec = sterilizedec(decade)
if dec == nil then
errors = p.errorclass('Function nav_decade was sent "'..(decade or '')..'" as its 2nd parameter, '..
'but expects a 1 to 4-digit year ending in "0".')
return p.failedcat(errors, 'D')
end
local ndec = tonumber(dec)  
--sterilize mindecade & determine AD/BC
local mindefault = '-9999'
local mindec = sterilizedec(mindecade) --returns a tostring(unsigned int), or nil
if mindec then
if string.match(mindecade, '-%d') or
string.match(mindecade, 'கிமு')
then
mindec = '-'..mindec --better +/-0 behavior with strings (0-initialized int == "-0" string...)
end
elseif mindec == nil and mindecade and mindecade ~= '' then
errors = p.errorclass('Function nav_decade was sent "'..(mindecade or '')..'" as its 4th parameter, '..
'but expects a 1 to 4-digit year ending in "0", the earliest decade to be shown.')
return p.failedcat(errors, 'E')
else --mindec == nil
mindec = mindefault --tonumber later, after error checks
end  
--sterilize maxdecade & determine AD/BC
local maxdefault = '9999'
local maxdec = sterilizedec(maxdecade) --returns a tostring(unsigned int), or nil + error
if maxdec then
if string.match(maxdecade, '-%d') or
string.match(maxdecade, 'கிமு')
then --better +/-0 behavior with strings (0-initialized int == "-0" string...),
maxdec = '-'..maxdec --but a "-0" string -> tonumber -> tostring = "-0",
end --and a "0" string -> tonumber -> tostring = "0"
elseif maxdec == nil and maxdecade and maxdecade ~= '' then
errors = p.errorclass('Function nav_decade was sent "'..(maxdecade or '')..'" as its 5th parameter, '..
'but expects a 1 to 4-digit year ending in "0", the highest decade to be shown.')
return p.failedcat(errors, 'F')
else --maxdec == nil
maxdec = maxdefault
end  
local tspace = ' ' --assume trailing space for "1950s in X"-type cats
if string.match(lastpart, '^-') then tspace = '' end --DNE for "1970s-related"-type cats