import { C as Container, a as Ctx, $ as OrderStatus } from "./index.js";
import { P as PendingOrdersAppManager } from "./PendingOrdersAppManager.js";
class OrderLogic extends Container {
  constructor(props) {
    super();
    this.orderExpiredTimeout = async () => {
      const { order } = this.state;
      if (!order || !order.expireTime)
        return;
      const TODAY = new Date().getTime();
      const orderExpiredTime = new Date(order.expireTime).getTime();
      const orderExpiredTimeout = orderExpiredTime - TODAY;
      setTimeout(() => {
        this.setState({ orderIsExpired: true });
      }, orderExpiredTimeout);
    };
    this.fetchOrder = async (orderNo) => {
      const { userId } = this.props;
      try {
        if (!userId || !orderNo)
          return;
        const order = await this.sdk.Platform.Order().getOrderByOrderNo({ userId, orderNo }).then((result) => {
          if (result.error)
            throw result.error;
          return result.response.data;
        });
        this.setState({ order });
      } catch (error) {
        this.setState({ error });
      }
    };
    this.cancelAllOrders = () => {
      var _a;
      return (_a = this.pendingOrdersAppManager) == null ? void 0 : _a.cancelAll();
    };
    this.props = props;
    this.state = {
      isFetching: false,
      error: null,
      order: null,
      orderIsExpired: false,
      item: null,
      ...PendingOrdersAppManager.DEFAULT_STATE
    };
    this.sdk = Ctx.getAppState().getSdk();
  }
  async init() {
    const { userId, orderNo } = this.props;
    try {
      if (this.state.isFetching)
        return;
      this.setState({
        isFetching: true,
        error: null,
        order: null
      });
      const order = await this.sdk.Platform.Order().getOrderByOrderNo({ userId, orderNo }).then((result) => {
        if (result.error)
          throw result.error;
        return result.response.data;
      });
      const item = await this.sdk.Platform.Item().getItemsByItemIds({
        queryParams: { itemIds: order == null ? void 0 : order.itemId }
      }).then((result) => {
        if (result.error)
          throw result.error;
        const [item2] = result.response.data;
        return item2;
      });
      this.pendingOrdersAppManager = new PendingOrdersAppManager(this.sdk, {
        userId: this.props.userId,
        itemId: order.itemId
      });
      this.pendingOrdersAppManager.subscribe(() => {
        const pendingOrderState = Object.keys(PendingOrdersAppManager.DEFAULT_STATE).reduce((acc, key) => {
          var _a;
          acc[key] = (_a = this.pendingOrdersAppManager) == null ? void 0 : _a.state[key];
          return acc;
        }, {});
        this.setState(pendingOrderState);
      });
      await this.pendingOrdersAppManager.init();
      const { fetchOrdersError } = this.pendingOrdersAppManager.state;
      if (fetchOrdersError)
        throw fetchOrdersError;
      await this.setState({ order, item });
    } catch (error) {
      this.setState({ error });
    } finally {
      await this.setState({ isFetching: false });
      if (this.state.order && this.state.order.status === OrderStatus.enum.INIT) {
        this.orderExpiredTimeout();
      }
    }
  }
}
export { OrderLogic as O };
