//! Generated from czech.sbl by Snowball 3.0.0 - https://snowballstem.org/

#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(unused_mut)]
#![allow(unused_parens)]
#![allow(unused_variables)]
use crate::snowball::Among;
use crate::snowball::SnowballEnv;

#[derive(Clone)]
struct Context {
    i_p1: i32,
}

static A_0: &'static [Among<Context>; 9] = &[
    Among("c", -1, 1, None),
    Among("čt", -1, 2, None),
    Among("št", -1, 3, None),
    Among("dešt", 2, -1, None),
    Among("lešt", 2, -1, None),
    Among("išt", 2, -1, None),
    Among("poušt", 2, -1, None),
    Among("ášt", 2, -1, None),
    Among("íšt", 2, -1, None),
];

static A_1: &'static [Among<Context>; 3] = &[
    Among("in", -1, 2, None),
    Among("ov", -1, 1, None),
    Among("ův", -1, 1, None),
];

static A_2: &'static [Among<Context>; 3] = &[
    Among("obl", -1, -1, None),
    Among("sn", -1, -1, None),
    Among("dot", -1, -1, None),
];

static A_3: &'static [Among<Context>; 5] = &[
    Among("uc", -1, -1, None),
    Among("h", -1, -1, None),
    Among("ok", -1, -1, None),
    Among("kar", -1, -1, None),
    Among("č", -1, -1, None),
];

static A_4: &'static [Among<Context>; 57] = &[
    Among("a", -1, 1, None),
    Among("ama", 0, 1, None),
    Among("ata", 0, 1, None),
    Among("eb", -1, 3, None),
    Among("ec", -1, 4, None),
    Among("e", -1, 2, None),
    Among("ete", 5, 2, None),
    Among("ěte", 5, 1, None),
    Among("ech", -1, 2, None),
    Among("atech", 8, 1, None),
    Among("ách", -1, 1, None),
    Among("ích", -1, 11, None),
    Among("ých", -1, 1, None),
    Among("i", -1, 11, None),
    Among("mi", 13, 1, None),
    Among("ami", 14, 1, None),
    Among("emi", 14, 2, None),
    Among("ěmi", 14, 1, None),
    Among("ťmi", 14, 10, None),
    Among("ími", 14, 11, None),
    Among("ými", 14, 1, None),
    Among("ěti", 13, 1, None),
    Among("ovi", 13, 1, None),
    Among("ek", -1, 5, None),
    Among("ěk", -1, 6, None),
    Among("em", -1, 2, None),
    Among("etem", 25, 2, None),
    Among("ětem", 25, 1, None),
    Among("ěm", -1, 1, None),
    Among("ám", -1, 1, None),
    Among("ém", -1, 1, None),
    Among("ím", -1, 11, None),
    Among("ům", -1, 1, None),
    Among("atům", 32, 1, None),
    Among("ým", -1, 1, None),
    Among("o", -1, 1, None),
    Among("ého", 35, 1, None),
    Among("ího", 35, 11, None),
    Among("us", -1, 1, None),
    Among("at", -1, 1, None),
    Among("et", -1, 8, None),
    Among("u", -1, 1, None),
    Among("ému", 41, 1, None),
    Among("ímu", 41, 11, None),
    Among("ou", 41, 1, None),
    Among("ev", -1, 9, None),
    Among("y", -1, 1, None),
    Among("aty", 46, 1, None),
    Among("eň", -1, 7, None),
    Among("ě", -1, 1, None),
    Among("á", -1, 1, None),
    Among("ť", -1, 10, None),
    Among("é", -1, 1, None),
    Among("ové", 52, 1, None),
    Among("í", -1, 11, None),
    Among("ů", -1, 1, None),
    Among("ý", -1, 1, None),
];

static G_v: &'static [u8; 34] = &[
    17, 65, 16, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17, 4, 18, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 64,
];

static G_v_or_syllabic_c: &'static [u8; 34] = &[
    17, 73, 18, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17, 4, 18, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 64,
];

static G_ev_ending: &'static [u8; 3] = &[73, 20, 4];

static G_env_ending: &'static [u8; 36] = &[
    71, 66, 23, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    128, 0, 0, 0, 16,
];

fn r_mark_regions(env: &mut SnowballEnv, context: &mut Context) -> bool {
    let mut i_x: i32;
    let v_1 = env.cursor;
    if !env.hop(3) {
        return false;
    }
    i_x = env.cursor;
    env.cursor = v_1;
    context.i_p1 = env.limit;
    let v_2 = env.cursor;
    'lab0: loop {
        'lab1: loop {
            let v_3 = env.cursor;
            'lab2: loop {
                if !env.in_grouping(G_v, 97, 367) {
                    break 'lab2;
                }
                break 'lab1;
            }
            env.cursor = v_3;
            if env.cursor >= env.limit {
                break 'lab0;
            }
            env.next_char();
            if !env.go_out_grouping(G_v_or_syllabic_c, 97, 367) {
                break 'lab0;
            }
            env.next_char();
            break 'lab1;
        }
        if !env.go_in_grouping(G_v, 97, 367) {
            break 'lab0;
        }
        env.next_char();
        context.i_p1 = env.cursor;
        'lab3: loop {
            if context.i_p1 >= i_x {
                break 'lab3;
            }
            context.i_p1 = i_x;
            break 'lab3;
        }
        break 'lab0;
    }
    env.cursor = v_2;
    return true;
}

fn r_R1(env: &mut SnowballEnv, context: &mut Context) -> bool {
    return context.i_p1 <= env.cursor;
}

fn r_palatalise_e(env: &mut SnowballEnv, context: &mut Context) -> bool {
    env.ket = env.cursor;
    if !env.eq_s_b(&"c") {
        return false;
    }
    env.bra = env.cursor;
    env.slice_from("k");
    return true;
}

fn r_palatalise_i(env: &mut SnowballEnv, context: &mut Context) -> bool {
    let mut among_var;
    env.ket = env.cursor;
    if (env.cursor <= env.limit_backward
        || (env.current.as_bytes()[(env.cursor - 1) as usize] as u8 != 99 as u8
            && env.current.as_bytes()[(env.cursor - 1) as usize] as u8 != 116 as u8))
    {
        return false;
    }

    among_var = env.find_among_b(A_0, context);
    if among_var == 0 {
        return false;
    }
    env.bra = env.cursor;
    match among_var {
        1 => {
            env.slice_from("k");
        }
        2 => {
            env.slice_from("ck");
        }
        3 => {
            env.slice_from("sk");
        }
        _ => (),
    }
    return true;
}

fn r_possessive_suffix(env: &mut SnowballEnv, context: &mut Context) -> bool {
    let mut among_var;
    env.ket = env.cursor;
    if (env.cursor - 1 <= env.limit_backward
        || (env.current.as_bytes()[(env.cursor - 1) as usize] as u8 != 110 as u8
            && env.current.as_bytes()[(env.cursor - 1) as usize] as u8 != 118 as u8))
    {
        return false;
    }

    among_var = env.find_among_b(A_1, context);
    if among_var == 0 {
        return false;
    }
    env.bra = env.cursor;
    if !r_R1(env, context) {
        return false;
    }
    match among_var {
        1 => {
            env.slice_del();
        }
        2 => {
            env.slice_del();
            let v_1 = env.limit - env.cursor;
            'lab0: loop {
                if !r_palatalise_i(env, context) {
                    env.cursor = env.limit - v_1;
                    break 'lab0;
                }
                break 'lab0;
            }
        }
        _ => (),
    }
    return true;
}

fn r_case_suffix(env: &mut SnowballEnv, context: &mut Context) -> bool {
    let mut among_var;
    if env.cursor < context.i_p1 {
        return false;
    }
    let v_1 = env.limit_backward;
    env.limit_backward = context.i_p1;
    env.ket = env.cursor;
    among_var = env.find_among_b(A_4, context);
    if among_var == 0 {
        env.limit_backward = v_1;
        return false;
    }
    env.bra = env.cursor;
    env.limit_backward = v_1;
    match among_var {
        1 => {
            env.slice_del();
        }
        2 => {
            env.slice_del();
            let v_2 = env.limit - env.cursor;
            'lab0: loop {
                if !r_palatalise_e(env, context) {
                    env.cursor = env.limit - v_2;
                    break 'lab0;
                }
                break 'lab0;
            }
        }
        3 => {
            let v_3 = env.limit - env.cursor;
            if !env.out_grouping_b(G_v, 97, 367) {
                return false;
            }
            env.cursor = env.limit - v_3;
            let v_4 = env.limit - env.cursor;
            'lab1: loop {
                if !env.eq_s_b(&"tř") {
                    break 'lab1;
                }
                return false;
            }
            env.cursor = env.limit - v_4;
            env.slice_from("b");
        }
        4 => {
            let v_5 = env.limit - env.cursor;
            if !env.out_grouping_b(G_v, 97, 367) {
                return false;
            }
            env.cursor = env.limit - v_5;
            env.slice_del();
            let (bra, ket) = (env.cursor, env.cursor);
            env.insert(bra, ket, "c");
            let v_6 = env.limit - env.cursor;
            'lab2: loop {
                if !r_palatalise_e(env, context) {
                    env.cursor = env.limit - v_6;
                    break 'lab2;
                }
                break 'lab2;
            }
        }
        5 => {
            let v_7 = env.limit - env.cursor;
            if !env.out_grouping_b(G_v, 97, 367) {
                return false;
            }
            env.cursor = env.limit - v_7;
            let v_8 = env.limit - env.cursor;
            'lab3: loop {
                if (env.cursor - 1 <= env.limit_backward
                    || env.current.as_bytes()[(env.cursor - 1) as usize] as u8 >> 5 != 3 as u8
                    || ((1069056 as i32
                        >> (env.current.as_bytes()[(env.cursor - 1) as usize] as u8 & 0x1f))
                        & 1)
                        == 0)
                {
                    break 'lab3;
                }

                if env.find_among_b(A_2, context) == 0 {
                    break 'lab3;
                }
                return false;
            }
            env.cursor = env.limit - v_8;
            env.slice_from("k");
        }
        6 => {
            if !env.eq_s_b(&"n") {
                return false;
            }
            env.bra = env.cursor;
            env.slice_from("ňk");
        }
        7 => {
            let v_9 = env.limit - env.cursor;
            if !env.in_grouping_b(G_env_ending, 98, 382) {
                return false;
            }
            env.cursor = env.limit - v_9;
            env.slice_from("n");
        }
        8 => {
            if env.find_among_b(A_3, context) == 0 {
                return false;
            }
            env.slice_from("t");
        }
        9 => {
            if !env.in_grouping_b(G_ev_ending, 104, 122) {
                return false;
            }
            env.slice_from("v");
        }
        10 => {
            env.slice_from("t");
        }
        11 => {
            env.slice_del();
            let v_10 = env.limit - env.cursor;
            'lab4: loop {
                if !r_palatalise_i(env, context) {
                    env.cursor = env.limit - v_10;
                    break 'lab4;
                }
                break 'lab4;
            }
        }
        _ => (),
    }
    return true;
}

pub fn stem(env: &mut SnowballEnv) -> bool {
    let mut context = &mut Context { i_p1: 0 };
    if !r_mark_regions(env, context) {
        return false;
    }
    env.limit_backward = env.cursor;
    env.cursor = env.limit;
    let v_1 = env.limit - env.cursor;
    r_case_suffix(env, context);
    env.cursor = env.limit - v_1;
    let v_2 = env.limit - env.cursor;
    r_possessive_suffix(env, context);
    env.cursor = env.limit - v_2;
    env.cursor = env.limit_backward;
    return true;
}
