import { C as Container, $ as OrderStatus, E as ErrorHelper } from "./index.js";
const _PendingOrdersAppManager = class extends Container {
  constructor(sdk, props) {
    super();
    this.sdk = sdk;
    this.cancelAll = async () => {
      const { pendingOrders } = this.state;
      if (!pendingOrders)
        return;
      try {
        const { userId } = this.props;
        await this.setState({ isCancelling: true, cancellationError: null });
        const succededOrderCancellation = [];
        const failedOrderCancellationWithError = [];
        await Promise.all(pendingOrders.map(async (order) => {
          const { orderNo } = order;
          const result = await this.sdk.Platform.Order().cancelOrder({ userId, orderNo });
          if (result.error)
            return failedOrderCancellationWithError.push({ order, error: result.error });
          return succededOrderCancellation.push(result.response.data);
        }));
        if (failedOrderCancellationWithError.length > 0 && pendingOrders.length === failedOrderCancellationWithError.length) {
          const firstFailed = failedOrderCancellationWithError[0];
          if (firstFailed)
            throw firstFailed.error;
        }
      } catch (cancellationError) {
        console.error(cancellationError);
        await this.setState({ cancellationError });
      } finally {
        await this.setState({ isCancelling: false });
      }
    };
    this.props = props;
    this.state = _PendingOrdersAppManager.DEFAULT_STATE;
  }
  async init() {
    try {
      if (this.state.isFetchingOrders)
        return;
      await this.setState({
        isFetchingOrders: true,
        pendingOrders: null,
        fetchOrdersError: null
      });
      const pendingOrders = await this.fetchPendingOrders();
      await this.setState({
        pendingOrders
      });
      if (pendingOrders.length > 0 && pendingOrders[0].paymentOrderNo) {
        const paymentInfo = await this.fetchPaymentInfo(pendingOrders[0].paymentOrderNo);
        await this.setState({
          paymentInfo
        });
      }
    } catch (error) {
      await this.setState({ fetchOrdersError: error });
    } finally {
      await this.setState({ isFetchingOrders: false });
    }
  }
  async fetchPendingOrders() {
    const { userId, itemId } = this.props;
    return await this.sdk.Platform.Order().getOrderList({ userId, queryParams: { itemId, status: OrderStatus.enum.INIT } }).then((result) => {
      if (result.error)
        throw result.error;
      return result.response.data.data;
    });
  }
  async fetchPaymentInfo(paymentOrderNo) {
    return await this.sdk.Platform.Payment().getPaymentInfo(paymentOrderNo).then((result) => {
      if (ErrorHelper.isAxiosError(result.error) && result.error.response && result.error.response.status === 404) {
        return null;
      }
      if (result.error) {
        throw result.error;
      }
      return result.response.data;
    });
  }
};
let PendingOrdersAppManager = _PendingOrdersAppManager;
PendingOrdersAppManager.DEFAULT_STATE = {
  isFetchingOrders: false,
  pendingOrders: null,
  paymentInfo: null,
  fetchOrdersError: null,
  isCancelling: false,
  cancellationError: null
};
export { PendingOrdersAppManager as P };
