var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/Sales/Checkout/index.tsx
var Checkout_exports = {};
__export(Checkout_exports, {
  CheckoutContext: () => CheckoutContext,
  CheckoutProvider: () => CheckoutProvider,
  default: () => Checkout_default,
  useCheckout: () => useCheckout
});
module.exports = __toCommonJS(Checkout_exports);
var import_react = __toESM(require("react"));
var import_SalesModel = require("../../../businessModel/SalesModel");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_ahooks = require("ahooks");
var import_serve = require("./serve");
var import_Cart = require("../Cart");
var import_utils = require("../../appointmentBooking/utils");
var defaultState = {};
var CheckoutContext = (0, import_react.createContext)(
  void 0
);
var CheckoutProvider = ({
  children
}) => {
  const context = (0, import_useEngineContext.default)();
  const utils = context.appHelper.utils || {};
  const { data, setData } = (0, import_SalesModel.useSales)();
  const { updateDisabledCartItem } = (0, import_Cart.useCart)();
  const checkoutCallback = (0, import_react.useRef)(null);
  const checkRef = (0, import_react.useRef)(null);
  const payRef = (0, import_react.useRef)(null);
  const checkBeforeBooking = (0, import_ahooks.useRequest)(import_serve.checkBeforeBookingApi, {
    manual: true,
    onSuccess: (res) => {
      var _a, _b;
      if ((_a = res.data) == null ? void 0 : _a.length) {
        updateDisabledCartItem(res.data);
        checkRef.current({ result: false, num: (_b = res.data) == null ? void 0 : _b.length });
      } else {
        checkRef.current({ result: true });
      }
    }
  });
  const createBooking = (0, import_ahooks.useRequest)(import_serve.createBookingApi, {
    manual: true,
    onSuccess: (res) => {
      onPay(res.data);
      checkoutCallback.current({ success: true });
    },
    onError: (err) => {
      checkoutCallback.current({ success: false, err });
    }
  });
  const freePay = (0, import_ahooks.useRequest)(import_serve.freePayApi, {
    manual: true,
    onSuccess: (res) => {
      var _a, _b;
      if (res == null ? void 0 : res.status) {
        goPaymentFinish((_a = payRef.current) == null ? void 0 : _a.order_id, (_b = payRef.current) == null ? void 0 : _b.total_amount);
      } else {
        (0, import_utils.Toast)(res == null ? void 0 : res.message);
      }
    }
  });
  const onCheckBeforeBooking = (data2) => {
    return new Promise((resolve) => {
      checkRef.current = (res) => {
        resolve(res);
        checkRef.current = null;
      };
      checkBeforeBooking.runAsync(data2);
    });
  };
  const onCheckout = async (data2) => {
    return new Promise((resolve) => {
      checkoutCallback.current = (e) => {
        resolve(e);
        checkoutCallback.current = null;
      };
      createBooking.runAsync(data2);
    });
  };
  const goPaymentFinish = (order_id, amount) => {
    var _a;
    const url = `/payment-finish/success?order_id=${order_id}&amount=${Number(
      amount
    )}`;
    (_a = utils == null ? void 0 : utils.history) == null ? void 0 : _a.replace(url);
  };
  const onPay = (payProps) => {
    payRef.current = payProps;
    const { is_deposit, deposit_amount, total_amount } = payProps;
    const _total = is_deposit ? deposit_amount || 0 : total_amount || 0;
    if (_total > 0) {
      utils.action({
        type: "pisell1.payment",
        data: { ...payProps, total_amount: _total },
        callback: (e) => {
          checkoutCallback.current && checkoutCallback.current(e);
        }
      });
    } else {
      if (is_deposit) {
        goPaymentFinish(payProps.order_id, _total);
        return;
      }
      freePay.runAsync(payProps.order_id);
    }
  };
  const contextValue = {
    onCheckBeforeBooking,
    onCheckout,
    onPay,
    createBooking,
    checkBeforeBooking,
    freePay
  };
  return /* @__PURE__ */ import_react.default.createElement(CheckoutContext.Provider, { value: contextValue }, children);
};
var useCheckout = () => {
  const context = import_react.default.useContext(CheckoutContext);
  if (!context) {
    throw new Error("useCheckout must be used within a CheckoutProvider");
  }
  return context;
};
var Checkout_default = {
  defaultState
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CheckoutContext,
  CheckoutProvider,
  useCheckout
});
