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

Module:PositionHandler

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

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

local p = {}
local wb = mw.wikibase

local function getLinkableValue(snak)
    if not snak then return nil end
    if snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then
        local qid = snak.datavalue.value.id
        local label = wb.getLabel(qid) or qid
        local sitelink = wb.getSitelink(qid)
        if sitelink then return "[[" .. sitelink .. "|" .. label .. "]]" else return label end
    end
    return wb.renderSnak(snak)
end

local function getLinkableQualifier(statement, qualId)
    if not (statement.qualifiers and statement.qualifiers[qualId]) then return nil end
    return getLinkableValue(statement.qualifiers[qualId][1])
end

function p.renderPositions(frame)
    local args = frame.args
    local parentArgs = frame:getParent().args
    
    local itemId = args.qid or parentArgs.qid
    if not itemId or itemId == "" then itemId = mw.wikibase.getEntityIdForCurrentPage() end
    if not itemId then return "" end

    local noIcon = (args.noicon or parentArgs.noicon or ""):lower()
    local hideIcon = (noIcon == "yes" or noIcon == "y" or noIcon == "true")

    local statements = wb.getBestStatements(itemId, 'P39')
    if #statements == 0 then return "" end

    local container = mw.html.create('div'):css('width', '100%'):attr('dir', 'rtl')
    local visibleItems = 0

    for _, statement in ipairs(statements) do
        local quals = statement.qualifiers or {}
        
        -- THE 4 QUALIFIER FILTERS
        local hasDates = (quals['P580'] or quals['P582'])
        local hasConstituency = quals['P768']
        local hasNavigation = (quals['P1365'] or quals['P1366'])
        
        -- ONLY SHOW IF: has at least one of the 4 qualifiers
        if (hasDates or hasConstituency or hasNavigation) then
            visibleItems = visibleItems + 1
            
            local positionLabel = getLinkableValue(statement.mainsnak)
            
            if quals['P1545'] then
                positionLabel = positionLabel .. " (" .. wb.renderSnak(quals['P1545'][1]) .. ")"
            end

            local editIcon = ""
            if not hideIcon then
                editIcon = " <span class='noprint' style='font-size:85%; margin-right:5px;'>[[File:OOjs UI icon edit-ltr-progressive.svg|10px|link=https://www.wikidata.org/wiki/" .. itemId .. "?uselang=ks#P39|عَدل کَرُن]]</span>"
            end

            local card = container:tag('div'):css('margin-bottom', '5px')
            
            -- Title (Lavender)
            card:tag('div'):css('background', '#E6E6FA'):css('padding', '5px')
                :css('font-weight', 'bold'):css('text-align', 'center')
                :css('border-top', '1px solid #c8ccd1'):css('border-bottom', '1px solid #c8ccd1')
                :wikitext(positionLabel .. editIcon)

            -- Status Row
            card:tag('div'):css('background', '#f8f9fa'):css('text-align', 'center')
                :css('font-weight', 'bold'):css('padding', '2px')
                :css('border-bottom', '1px solid #c8ccd1')
                :wikitext("عُہدَس مَنٛز")

            -- Dates Row
            local startDate = quals['P580'] and wb.renderSnak(quals['P580'][1])
            local endDate = quals['P582'] and wb.renderSnak(quals['P582'][1])
            
            if startDate or endDate then
                card:tag('div'):css('text-align', 'center'):css('padding', '5px'):css('background', '#f8f9fa')
                    :css('border-bottom', '1px solid #c8ccd1')
                    :wikitext((startDate or "") .. " – " .. (endDate or "ازتام"))
            end

            -- Constituency Row
            local constituency = getLinkableQualifier(statement, 'P768')
            if constituency then
                local row = card:tag('div'):css('display', 'flex'):css('justify-content', 'space-between')
                    :css('padding', '4px 8px'):css('background', '#f8f9fa')
                    :css('border-bottom', '1px solid #eeeeee')
                row:tag('span'):css('font-weight','bold'):wikitext("چُناو حَلقہٕ"):done()
                row:tag('span'):wikitext(constituency):done()
            end

            -- Preceder Row
            local prec = getLinkableQualifier(statement, 'P1365')
            if prec then
                local precRow = card:tag('div'):css('display', 'flex'):css('justify-content', 'space-between')
                    :css('padding', '4px 8px'):css('background', '#f8f9fa')
                    :css('border-bottom', '1px solid #eeeeee')
                precRow:tag('span'):css('font-weight','bold'):wikitext("برٛونٛہہ حقدار"):done()
                precRow:tag('span'):wikitext(prec):done()
            end

            -- Successor Row
            local succ = getLinkableQualifier(statement, 'P1366')
            if succ then
                local succRow = card:tag('div'):css('display', 'flex'):css('justify-content', 'space-between')
                    :css('padding', '4px 8px'):css('background', '#f8f9fa')
                    :css('border-bottom', '1px solid #c8ccd1')
                succRow:tag('span'):css('font-weight','bold'):wikitext("پتہٕ حقدار"):done()
                succRow:tag('span'):wikitext(succ):done()
            end
        end
    end

    if visibleItems == 0 then return "" end
    return tostring(container)
end

return p