مَوادَس کُن گٔژھِو

Module:Historical populations/Wikidata

وِکیٖپیٖڈیا پؠٹھٕ، اَکھ آزاد اِنسایکلوپیٖڈیا

"یَمہٕ ماڈیوٗلُک دَستاویز ییٚہِ Module:Historical populations/Wikidata/دَستاویز جاے بَناونہٕ"

local M = {}
local wikidataIB = require("Module:WikidataIB")

local function getQID()
    return wikidataIB.getQid()
end

function M.formatPopulationData(frame)

    local qid = frame.args[1] or frame.args["qid"] or getQID()

    if not qid then
        return "No Wikidata QID"
    end

    local wdibResult = frame:expandTemplate{
        title = "Wdib",
        args = { "P1082", qual = "P585", qid = qid, osd = "no", fwd = "ALL", noicon = "yes" }
    }

    if not wdibResult or wdibResult == "" then
        return "No population data"
    end

    local entries = mw.text.split(wdibResult, ", ")

    local data = {}

    for _, entry in ipairs(entries) do
        local parts = mw.text.split(entry, " ")
        local population = parts[1] or ""
        local year = mw.ustring.gsub(parts[2] or "", "[()]", "")

        if year ~= "" and population ~= "" then
            table.insert(data, {
                year = tonumber(year),
                pop = population
            })
        end
    end

    -- ✅ Sort by year
    table.sort(data, function(a, b)
        return a.year < b.year
    end)

    local rows = ""

    for _, item in ipairs(data) do
        rows = rows .. "\n|-\n|" .. item.year .. " || " .. item.pop
    end

    -- ✅ Lavender header only
    local tableText = [[
{| class="wikitable"
|-
! style="background-color: lavender;" | ؤری
! style="background-color: lavender;" | آبٲدی
]] .. rows .. "\n|}"

    return frame:expandTemplate{
        title = "تٲریخی آبٲدی",
        args = { table = tableText }
    }
end

return M