from sysactions import (
    action,
    get_model,
    get_podtype,
    get_posot,
)

from actions.kiosk import do_void_kiosk_order
from actions.void.do_void_order import do_void_order
from utilfunctions import is_stored_order


@action
def handle_abandon_order(pos_id):
    try:
        model = get_model(pos_id)
        pod_type = get_podtype(model)
        is_totem_order = pod_type == "TT"
        if is_totem_order:
            return do_void_kiosk_order(pos_id)

        order_was_saved = is_stored_order(model=model)
        if order_was_saved:
            pos_ot = get_posot(model)
            pos_ot.storeOrder(pos_id)
            return True

        return do_void_order(
            pos_id,
            show_cancel_message=False,
            received_void_reason="TIMEOUT_CANCELLATION",
            from_timeout=True,
        )
    except BaseException as _:
        return False
