use crate::enums::lua_type::lua_Type;
use crate::macros::lvalue::lvalue;
use crate::macros::setlvalue::setlvalue;
use crate::macros::ttisinteger::ttisinteger;
use crate::type_aliases::lua_state::lua_State;
use crate::type_aliases::stk_id::StkId;
use crate::type_aliases::t_value::TValue;

#[allow(non_snake_case)]
pub unsafe fn luau_f_integerarshift(
    _L: *mut lua_State,
    res: StkId,
    arg0: *mut TValue,
    nresults: core::ffi::c_int,
    args: StkId,
    nparams: core::ffi::c_int,
) -> core::ffi::c_int {
    if nparams >= 2 && nresults <= 1 && ttisinteger!(arg0) && ttisinteger!(args) {
        let n: i64 = lvalue!(arg0);
        let i: i64 = lvalue!(args);

        if i >= -63 && i <= 63 {
            setlvalue!(
                res,
                if i < 0 {
                    ((n as u64) << (-i)) as i64
                } else {
                    n >> i
                }
            );
        } else if i < -63 {
            setlvalue!(res, 0);
        } else {
            setlvalue!(res, if n < 0 { -1 } else { 0 });
        }

        1
    } else {
        -1
    }
}
