وحدة:amazigh conjugation

local p = {}

-- قاموس كامل للنطق IPA (التيفيناغ → IPA) local ipa_map = {
    -- الحروف الأساسية
    ["ⴰ"] = "æ", ["ⴱ"] = "b", ["ⴳ"] = "ɡ", ["ⴷ"] = "d", ["ⴹ"] = "dˤ",
    ["ⴻ"] = "ə", ["ⴼ"] = "f", ["ⴽ"] = "k", ["ⵀ"] = "h", ["ⵂ"] = "h",
    ["ⵃ"] = "ħ", ["ⵄ"] = "ʕ", ["ⵅ"] = "χ", ["ⵇ"] = "q", ["ⵉ"] = "i",
    ["ⵊ"] = "ʒ", ["ⵍ"] = "l", ["ⵎ"] = "m", ["ⵏ"] = "n", ["ⵓ"] = "u",
    ["ⵔ"] = "r", ["ⵕ"] = "rˤ", ["ⵖ"] = "ʁ", ["ⵙ"] = "s", ["ⵚ"] = "sˤ",
    ["ⵛ"] = "ʃ", ["ⵜ"] = "t", ["ⵟ"] = "tˤ", ["ⵡ"] = "w", ["ⵢ"] = "j",
    ["ⵣ"] = "z", ["ⵥ"] = "zˤ",
    
    -- الحروف المزدوجة (للأصوات المشددة)
    ["ⴱⴱ"] = "bː", ["ⴳⴳ"] = "ɡː", ["ⴷⴷ"] = "dː", ["ⴹⴹ"] = "dˤː",
    ["ⴼⴼ"] = "fː", ["ⴽⴽ"] = "kː", ["ⵀⵀ"] = "hː", ["ⵂⵂ"] = "hː",
    ["ⵃⵃ"] = "ħː", ["ⵄⵄ"] = "ʕː", ["ⵅⵅ"] = "χː", ["ⵇⵇ"] = "qː",
    ["ⵊⵊ"] = "ʒː", ["ⵍⵍ"] = "lː", ["ⵎⵎ"] = "mː", ["ⵏⵏ"] = "nː",
    ["ⵔⵔ"] = "rː", ["ⵕⵕ"] = "rˤː", ["ⵖⵖ"] = "ʁː", ["ⵙⵙ"] = "sː",
    ["ⵚⵚ"] = "sˤː", ["ⵛⵛ"] = "ʃː", ["ⵜⵜ"] = "tː", ["ⵟⵟ"] = "tˤː",
    ["ⵡⵡ"] = "wː", ["ⵢⵢ"] = "jː", ["ⵣⵣ"] = "zː", ["ⵥⵥ"] = "zˤː"
}

-- دالة التحويل إلى IPA (للإستخدام الداخلي) local function to_ipa(text)
    if not text or text == "" then return "" end
    
    -- معالجة الحروف المزدوجة أولاً
    local result = text
    for double, ipa in pairs(ipa_map) do
        if mw.ustring.len(double) == 2 then
            result = mw.ustring.gsub(result, double, ipa)
        end
    end
    
    -- معالجة الحروف المفردة
    for single, ipa in pairs(ipa_map) do
        if mw.ustring.len(single) == 1 then
            result = mw.ustring.gsub(result, single, ipa)
        end
    end
    
    return result
end

-- دالة عامة للتحويل إلى IPA (للاستخدام الخارجي) function p.ipa(frame)
    local word = frame.args[1] or ""
    if word == "" then return "" end
    return to_ipa(word)
end

-- قائمة بأنماط الأفعال الشائعة local verb_patterns = {
    -- ثلاثي صحيح (مثل: ⴽⵔⵣ - حرث)
    C3 = {
        pattern = "^([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])$",
        imperfect = function(c1, c2, c3) return c1 .. c2 .. c2 .. "ⴰ" .. c3 end,
        negative_perfect = function(c1, c2, c3) return c1 .. "ⵉ" .. c2 .. c3 end
    },
    
    -- ثلاثي معتل الأول (مثل: ⵡⵍⵍ - أخبر)
    C3_weak1 = {
        pattern = "^([ⵡⵢ])([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])$",
        imperfect = function(c1, c2, c3) return "ⵜⵜ" .. c2 .. "ⴰ" .. c3 end,
        negative_perfect = function(c1, c2, c3) return "ⵓ" .. c2 .. c3 end
    },
    
    -- ثلاثي معتل الآخر (مثل: ⴱⵏⵓ - بنى)
    C3_vowel_end = {
        pattern = "^([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])([ⴰⵉⵓ])$",
        imperfect = function(c1, c2, c3) return "ⵜⵜ" .. c1 .. c2 .. "ⴰ" end,
        negative_perfect = function(c1, c2, c3) return c1 .. c2 .. "ⵉ" end
    },
    
    -- ثلاثي أجوف (مثل: ⵣⵓⵣ - باع)
    C3_hollow = {
        pattern = "^([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])([ⴰⵉⵓ])([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])$",
        imperfect = function(c1, c2, c3) return "ⵜⵜ" .. c1 .. c3 .. "ⴰ" .. c3 end,
        negative_perfect = function(c1, c2, c3) return c1 .. "ⵉ" .. c3 end
    },
    
    -- ثلاثي مضاعف (مثل: ⵙⵙⵏ - عرف)
    C3_geminated = {
        pattern = "^([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])([ⴱⴳⴷⴹⴼⴽⵀⵂⵃⵄⵅⵇⵊⵍⵎⵏⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥ])(ⴲ)%2$",
        imperfect = function(c1, c2, c3) return "ⵜⵜ" .. c1 .. c2 .. "ⴰ" .. c2 end,
        negative_perfect = function(c1, c2, c3) return c1 .. "ⵉ" .. c2 .. c2 end
    }
}

-- دالة التعرف على نمط الفعل function p.identify_pattern(word)
    for pattern_name, pattern_data in pairs(verb_patterns) do
        local c1, c2, c3 = mw.ustring.match(word, pattern_data.pattern)
        if c1 then
            return pattern_name, pattern_data, c1, c2, c3
        end
    end
    return nil
end

-- دالة التصريف الداخلية local function conjugate(word, task)
    if not word or word == "" then return "" end
    
    -- إذا كان المطلوب هو الصيغة الأساسية
    if task == "usmid" or task == "base" then
        return word
    end
    
    -- التعرف على نمط الفعل
    local pattern_name, pattern_data, c1, c2, c3 = p.identify_pattern(word)
    
    -- إذا لم نتعرف على النمط، نستخدم القواعد الافتراضية
    if not pattern_data then
        local len = mw.ustring.len(word)
        local last = mw.ustring.sub(word, len, len)
        
        if task == "anabaw" or task == "negative_perfect" then
            if last == "ⴰ" then
                return mw.ustring.sub(word, 1, len - 1) .. "ⵉ"
            end
            return mw.ustring.gsub(word, "ⴰ", "ⵉ")
        elseif task == "arusmid" or task == "imperfect" then
            -- التحقق من وجود ميم في الوسط
            local middle = mw.ustring.sub(word, 2, 2)
            if middle == "ⵎ" then
                local stem = mw.ustring.sub(word, 1, len - 1)
                return "ⵜⵜ" .. stem .. "ⴰ" .. last
            end
            return "ⵜⵜ" .. word
        end
    else
        -- استخدام النمط المحدد
        if task == "arusmid" or task == "imperfect" then
            return pattern_data.imperfect(c1, c2, c3)
        elseif task == "anabaw" or task == "negative_perfect" then
            return pattern_data.negative_perfect(c1, c2, c3)
        end
    end
    
    return word
end

-- الدالة الرئيسية للتصريف (للاستخدام الخارجي) function p.conjugate(frame)
    local args = frame:getParent().args or frame.args
    local word = args[1] or args.word or ""
    local task = args[2] or args.task or "usmid"
    
    if word == "" then
        word = mw.title.getCurrentTitle().text  -- استخدام اسم الصفحة إذا لم يُحدد
    end
    
    return conjugate(word, task)
end

-- دالة للتصريف مع عرض جميع الصيغ function p.all_forms(frame)
    local args = frame:getParent().args or frame.args
    local word = args[1] or args.word or mw.title.getCurrentTitle().text
    
    local forms = {
        usmid = conjugate(word, "usmid"),
        arusmid = conjugate(word, "arusmid"),
        anabaw = conjugate(word, "anabaw")
    }
    
    return forms
end

-- دالة للحصول على جذر الكلمة function p.extract_root(word)
    if not word or word == "" then
        word = mw.title.getCurrentTitle().text
    end
    
    -- إزالة السوابق واللواحق الشائعة
    word = mw.ustring.gsub(word, "^[ⵜⵜⴰⴷ]", "")
    word = mw.ustring.gsub(word, "[ⴰⵉⵓ]$", "")
    
    return word
end

-- دالة لتحليل الفعل وعرض معلومات مفصلة function p.analyze(frame)
    local args = frame:getParent().args or frame.args
    local word = args[1] or args.word or mw.title.getCurrentTitle().text
    
    local pattern_name, pattern_data = p.identify_pattern(word)
    local root = p.extract_root(word)
    local ipa = to_ipa(word)
    
    local result = {
        "=== ⵜⴰⵙⵏⴰⵥⵖⵓⵕⵜ ⵏ ⵓⵎⵢⴰⴳ ===",
        "* ⴰⵎⵢⴰⴳ: " .. word,
        "* ⴰⵥⵓⵕ: " .. root,
        "* ⴰⵏⴰⵡ: " .. (pattern_name or "ⵡⴰⵔ ⴰⵏⴰⵡ"),
        "* IPA: " .. ipa
    }
    
    if pattern_name then
        result[#result+1] = "* ⴰⵔⵓⵙⵎⵉⴷ: " .. pattern_data.imperfect(
            mw.ustring.match(word, pattern_data.pattern)
        )
        result[#result+1] = "* ⴰⵏⴰⴱⴰⵡ: " .. pattern_data.negative_perfect(
            mw.ustring.match(word, pattern_data.pattern)
        )
    end
    
    return table.concat(result, "\n")
end

-- دالة مساعدة للقوالب (للاستخدام في ) function p.format_for_template(frame)
    local word = frame.args[1] or mw.title.getCurrentTitle().text
    local task = frame.args[2] or "usmid"
    
    local conjugated = conjugate(word, task)
    local ipa = to_ipa(conjugated)
    
    return conjugated .. " (" .. ipa .. ")"
end

return p