import { C as d, a9 as h, y as f } from "./index-162fd1aa.mjs";
const o = class extends d {
  constructor(r, t) {
    super(), this.sdk = r, this.cancelAll = async () => {
      const { pendingOrders: e } = this.state;
      if (e)
        try {
          const { userId: a } = this.props;
          await this.setState({ isCancelling: !0, cancellationError: null });
          const l = [], n = [];
          if (await Promise.all(
            e.map(async (s) => {
              const { orderNo: c } = s, i = await this.sdk.Platform.Order().cancelOrder({ userId: a, orderNo: c });
              return i.error ? n.push({ order: s, error: i.error }) : l.push(i.response.data);
            })
          ), n.length > 0 && e.length === n.length) {
            const s = n[0];
            if (s)
              throw s.error;
          }
        } catch (a) {
          console.error(a), await this.setState({ cancellationError: a });
        } finally {
          await this.setState({ isCancelling: !1 });
        }
    }, this.props = t, this.state = o.DEFAULT_STATE;
  }
  async init() {
    try {
      if (this.state.isFetchingOrders)
        return;
      await this.setState({
        isFetchingOrders: !0,
        pendingOrders: null,
        fetchOrdersError: null
      });
      const r = await this.fetchPendingOrders();
      if (await this.setState({
        pendingOrders: r
      }), r.length > 0 && r[0].paymentOrderNo) {
        const t = await this.fetchPaymentInfo(r[0].paymentOrderNo);
        await this.setState({
          paymentInfo: t
        });
      }
    } catch (r) {
      await this.setState({ fetchOrdersError: r });
    } finally {
      await this.setState({ isFetchingOrders: !1 });
    }
  }
  async fetchPendingOrders() {
    const { userId: r, itemId: t } = this.props;
    return await this.sdk.Platform.Order().getOrderList({ userId: r, queryParams: { itemId: t, status: h.enum.INIT } }).then((e) => {
      if (e.error)
        throw e.error;
      return e.response.data.data;
    });
  }
  async fetchPaymentInfo(r) {
    return await this.sdk.Platform.Payment().getPaymentInfo(r).then((t) => {
      if (f.isAxiosError(t.error) && t.error.response && t.error.response.status === 404)
        return null;
      if (t.error)
        throw t.error;
      return t.response.data;
    });
  }
};
let p = o;
p.DEFAULT_STATE = {
  isFetchingOrders: !1,
  pendingOrders: null,
  paymentInfo: null,
  fetchOrdersError: null,
  isCancelling: !1,
  cancellationError: null
};
export {
  p as P
};
