وحدة:استشهاد مختصر

local mCitation = {}

function mCitation.main(frame)
    -- الحصول على المتغيرات تلقائيًا
    local lang = frame.args["لغة"] or "ar"  -- جعل اللغة العربية هي الافتراضية
    local volume = frame.args["ج"] or ""
    local page = frame.args["ص"] or frame.args["صفحة"] or ""
    local plate = frame.args["لوحة"] or ""
    local chapter = frame.args["فصل"] or ""
    local section = frame.args["قسم"] or ""
    local text = frame.args["نص"] or ""
    local author1 = frame.args[1] or ""
    local author2 = frame.args[2] or ""

    -- التحقق من وجود مؤلفين
    if author1 == "" then
        return "خطأ: يجب تحديد المؤلف الأول."
    end

    -- بناء النص باستخدام قالب تقليم وCITEREF
    local result = "[[#" .. "CITEREF" .. frame:expandTemplate{ title = "تقليم", args = { author1 } }
    if author2 ~= "" then
        result = result .. frame:expandTemplate{ title = "تقليم", args = { author2 } }
    end
    result = result .. "|" .. frame:expandTemplate{ title = "تقليم", args = { author1 } }
    if author2 ~= "" then
        result = result .. " (" .. frame:expandTemplate{ title = "تقليم", args = { author2 } } .. ")"
    end
    result = result .. "]]"

    -- تعيين التسميات حسب اللغة
    local volume_label, page_label, chapter_label, section_label, plate_label, separator

    if lang == "en" then
        volume_label = "vol."
        page_label = "p."
        chapter_label = "chapt."
        section_label = "sect."
        plate_label = "plate"
        separator = ", "
    elseif lang == "es" then
        volume_label = "vol."
        page_label = "pág."
        chapter_label = "cap."
        section_label = "secc."
        plate_label = "lámina"
        separator = ", "
    elseif lang == "fr" then
        volume_label = "vol."
        page_label = "p."
        chapter_label = "chap."
        section_label = "sect."
        plate_label = "planche"
        separator = ", "
    elseif lang == "pt" then
        volume_label = "vol."
        page_label = "p."
        chapter_label = "cap."
        section_label = "seç."
        plate_label = "prancha"
        separator = ", "
    elseif lang == "de" then
        volume_label = "Bd."
        page_label = "s."
        chapter_label = "Kap."
        section_label = "Abschnitt"
        plate_label = "Tafel"
        separator = ", "
    else
        -- اللغة الافتراضية هي العربية
        volume_label = "ج."
        page_label = "ص."
        chapter_label = "الفصل"
        section_label = "القسم"
        plate_label = "لوحة"
        separator = "، "
    end

    -- بناء النص حسب وجود لوحة أو صفحة
    if volume ~= "" then result = result .. separator .. volume_label .. " " .. volume end
    
    -- إذا كان وسيط اللوحة موجودًا، استخدمه وإلا استخدم وسيط الصفحة
    if plate ~= "" then
        result = result .. separator .. plate_label .. " " .. plate
    elseif page ~= "" then
        result = result .. separator .. page_label .. " " .. page
    end

    if chapter ~= "" then
        result = result .. separator .. chapter_label .. " " .. chapter
        if section ~= "" then result = result .. separator .. section_label .. " " .. section end
    end

    if text ~= "" then result = result .. separator .. text end

    result = result .. "."
    return result
end

return mCitation