from typing import Literal, Set, cast

OrderCancelReason = Literal[
    "ACCOUNT_LOCKED",
    "ACCOUNT_NEW_POSITIONS_LOCKED",
    "ACCOUNT_ORDER_CREATION_LOCKED",
    "ACCOUNT_ORDER_FILL_LOCKED",
    "ACCOUNT_POSITION_VALUE_LIMIT_EXCEEDED",
    "BOUNDS_VIOLATION",
    "CLIENT_REQUEST",
    "CLIENT_REQUEST_REPLACED",
    "CLIENT_TRADE_ID_ALREADY_EXISTS",
    "FIFO_VIOLATION",
    "HEDGING_GSLO_VIOLATION",
    "INSTRUMENT_ASK_HALTED",
    "INSTRUMENT_ASK_REDUCE_ONLY",
    "INSTRUMENT_BID_HALTED",
    "INSTRUMENT_BID_REDUCE_ONLY",
    "INSUFFICIENT_LIQUIDITY",
    "INSUFFICIENT_MARGIN",
    "INTERNAL_SERVER_ERROR",
    "LINKED_TRADE_CLOSED",
    "LOSING_TAKE_PROFIT",
    "MARKET_HALTED",
    "MIGRATION",
    "OPEN_TRADES_ALLOWED_EXCEEDED",
    "PENDING_ORDERS_ALLOWED_EXCEEDED",
    "POSITION_CLOSEOUT_FAILED",
    "POSITION_SIZE_EXCEEDED",
    "STOP_LOSS_ON_FILL_CLIENT_ORDER_ID_ALREADY_EXISTS",
    "STOP_LOSS_ON_FILL_GTD_TIMESTAMP_IN_PAST",
    "STOP_LOSS_ON_FILL_GUARANTEED_ASK_HALTED",
    "STOP_LOSS_ON_FILL_GUARANTEED_BID_HALTED",
    "STOP_LOSS_ON_FILL_GUARANTEED_HEDGING_NOT_ALLOWED",
    "STOP_LOSS_ON_FILL_GUARANTEED_LEVEL_RESTRICTION_EXCEEDED",
    "STOP_LOSS_ON_FILL_GUARANTEED_MINIMUM_DISTANCE_NOT_MET",
    "STOP_LOSS_ON_FILL_GUARANTEED_NOT_ALLOWED",
    "STOP_LOSS_ON_FILL_GUARANTEED_REQUIRED",
    "STOP_LOSS_ON_FILL_LOSS",
    "STOP_LOSS_ON_FILL_PRICE_DISTANCE_MAXIMUM_EXCEEDED",
    "STOP_LOSS_ON_FILL_REQUIRED",
    "STOP_LOSS_ON_FILL_TIME_IN_FORCE_INVALID",
    "STOP_LOSS_ON_FILL_TRIGGER_CONDITION_INVALID",
    "TAKE_PROFIT_ON_FILL_CLIENT_ORDER_ID_ALREADY_EXISTS",
    "TAKE_PROFIT_ON_FILL_GTD_TIMESTAMP_IN_PAST",
    "TAKE_PROFIT_ON_FILL_LOSS",
    "TAKE_PROFIT_ON_FILL_PRICE_DISTANCE_MAXIMUM_EXCEEDED",
    "TIME_IN_FORCE_EXPIRED",
    "TRAILING_STOP_LOSS_ON_FILL_CLIENT_ORDER_ID_ALREADY_EXISTS",
    "TRAILING_STOP_LOSS_ON_FILL_GTD_TIMESTAMP_IN_PAST",
]

ORDER_CANCEL_REASON_VALUES: Set[OrderCancelReason] = {
    "ACCOUNT_LOCKED",
    "ACCOUNT_NEW_POSITIONS_LOCKED",
    "ACCOUNT_ORDER_CREATION_LOCKED",
    "ACCOUNT_ORDER_FILL_LOCKED",
    "ACCOUNT_POSITION_VALUE_LIMIT_EXCEEDED",
    "BOUNDS_VIOLATION",
    "CLIENT_REQUEST",
    "CLIENT_REQUEST_REPLACED",
    "CLIENT_TRADE_ID_ALREADY_EXISTS",
    "FIFO_VIOLATION",
    "HEDGING_GSLO_VIOLATION",
    "INSTRUMENT_ASK_HALTED",
    "INSTRUMENT_ASK_REDUCE_ONLY",
    "INSTRUMENT_BID_HALTED",
    "INSTRUMENT_BID_REDUCE_ONLY",
    "INSUFFICIENT_LIQUIDITY",
    "INSUFFICIENT_MARGIN",
    "INTERNAL_SERVER_ERROR",
    "LINKED_TRADE_CLOSED",
    "LOSING_TAKE_PROFIT",
    "MARKET_HALTED",
    "MIGRATION",
    "OPEN_TRADES_ALLOWED_EXCEEDED",
    "PENDING_ORDERS_ALLOWED_EXCEEDED",
    "POSITION_CLOSEOUT_FAILED",
    "POSITION_SIZE_EXCEEDED",
    "STOP_LOSS_ON_FILL_CLIENT_ORDER_ID_ALREADY_EXISTS",
    "STOP_LOSS_ON_FILL_GTD_TIMESTAMP_IN_PAST",
    "STOP_LOSS_ON_FILL_GUARANTEED_ASK_HALTED",
    "STOP_LOSS_ON_FILL_GUARANTEED_BID_HALTED",
    "STOP_LOSS_ON_FILL_GUARANTEED_HEDGING_NOT_ALLOWED",
    "STOP_LOSS_ON_FILL_GUARANTEED_LEVEL_RESTRICTION_EXCEEDED",
    "STOP_LOSS_ON_FILL_GUARANTEED_MINIMUM_DISTANCE_NOT_MET",
    "STOP_LOSS_ON_FILL_GUARANTEED_NOT_ALLOWED",
    "STOP_LOSS_ON_FILL_GUARANTEED_REQUIRED",
    "STOP_LOSS_ON_FILL_LOSS",
    "STOP_LOSS_ON_FILL_PRICE_DISTANCE_MAXIMUM_EXCEEDED",
    "STOP_LOSS_ON_FILL_REQUIRED",
    "STOP_LOSS_ON_FILL_TIME_IN_FORCE_INVALID",
    "STOP_LOSS_ON_FILL_TRIGGER_CONDITION_INVALID",
    "TAKE_PROFIT_ON_FILL_CLIENT_ORDER_ID_ALREADY_EXISTS",
    "TAKE_PROFIT_ON_FILL_GTD_TIMESTAMP_IN_PAST",
    "TAKE_PROFIT_ON_FILL_LOSS",
    "TAKE_PROFIT_ON_FILL_PRICE_DISTANCE_MAXIMUM_EXCEEDED",
    "TIME_IN_FORCE_EXPIRED",
    "TRAILING_STOP_LOSS_ON_FILL_CLIENT_ORDER_ID_ALREADY_EXISTS",
    "TRAILING_STOP_LOSS_ON_FILL_GTD_TIMESTAMP_IN_PAST",
}


def check_order_cancel_reason(value: str) -> OrderCancelReason:
    if value in ORDER_CANCEL_REASON_VALUES:
        return cast(OrderCancelReason, value)
    raise TypeError(
        f"Unexpected value {value!r}. Expected one of {ORDER_CANCEL_REASON_VALUES!r}"
    )
