function pluralise(rt) {
    let m = rt.match(/([aeioáéíó])(?:[^aeioáéíó]*$)/)
    return (rt.substr(0, m.index + 1) + rt.substr(m.index, rt.length)
            .replace('á','a')
            .replace('é','e')
            .replace('í','i')
            .replace('ó','o'))
}


module.exports = {
    /**
     * Declines a noun
     */
    noun: (rt, num, decl) => {
        if (num == "PL")
            rt = pluralise(rt)
        switch (decl) {
            case "TOP": return rt;
            case "OBL": {
                if (rt.endsWith("cz") || rt.endsWith("tz") || rt.endsWith("ts") || rt.endsWith("cs")) {
                    return rt.substr(0, rt.length - 2) + "t";
                }
                if (rt.endsWith("z") || rt.endsWith("s")) {
                    return rt.substr(0, rt.length - 1) + "t"
                }
                return rt + "t"
            }
            case "GEN": {
                if (rt.endsWith("cz") || rt.endsWith("tz") || rt.endsWith("ts") || rt.endsWith("cs")) {
                    return rt.substr(0, rt.length - 2) + "c";
                }
                if (rt.endsWith("z") || rt.endsWith("s")) {
                    return rt.substr(0, rt.length - 1) + "c"
                }
                return rt + "c"
            }
            case "DAT": {
                if (rt.endsWith("s") || rt.endsWith("z")) {
                    return rt + "i"
                }
                return rt + "si"
            }
            case "ABL": {
                if (rt.endsWith("cz") || rt.endsWith("cs")) {
                    return rt.substr(0, rt.length - 2) + "cvia";
                }
                if (rt.endsWith("tz") || rt.endsWith("ts")) {
                    return rt.substr(0, rt.length - 2) + "tvia";
                }
                if (rt.endsWith("z") || rt.endsWith("s")) {
                    return rt.substr(0, rt.length - 1) + "via";
                }
                return rt + "via"
            }
        }
    },
    verb: (rt, t, p) => {
        rt = rt.substr(0, rt.length - 1);
        var tense;
        if (t == "PST") tense = "i";
        if (t == "PRS") tense = "e";
        if (t == "FUT") tense = "o";
        if (t == "GNO") tense = "a";
        var pers;
        if (p == "1S") pers = "f";
        if (p == "2S") pers = "n";
        if (p == "3SI") pers = "s";
        if (p == "3SA") pers = "h";
        if (p == "1PE") pers = "́v";
        if (p == "1PI") pers = "́m";
        if (p == "2P") pers = "́n";
        if (p == "3PI") pers = "́z";
        if (p == "3PA") pers = "́r";
        if (p == "INF") pers = "";
        if (p == "SPCP") pers = "p";
        if (p == "PPCP") pers = "́p";
        return rt + tense + pers;
    }
}
