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/appointmentBooking/components/ConfirmInformation/index.tsx
var ConfirmInformation_exports = {};
__export(ConfirmInformation_exports, {
  default: () => ConfirmInformation_default
});
module.exports = __toCommonJS(ConfirmInformation_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var import_context = require("../../context");
var import_hooks = require("../../hooks");
var import_Item = __toESM(require("../Cart/Item"));
var import_utils2 = require("../../utils");
var import_utils3 = require("./utils");
var import_index = require("./index.less");
var ConfirmInformation = (0, import_react.forwardRef)((props, ref) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  const config = ((_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.constants) == null ? void 0 : _b.config) || {};
  const utils = (0, import_hooks.contextUtils)();
  const { appointmentBooking, summary, isAgreeDepositPolicy, setLoading } = (0, import_context.useAppointmentBookingContext)();
  const { isRetailTemplate } = (0, import_hooks.useAppointmentBookingSetting)();
  const goPaymentFinish = (order_id, amount) => {
    var _a2;
    const url = `/payment-finish/success?order_id=${order_id}&amount=${Number(
      amount
    )}`;
    (_a2 = utils == null ? void 0 : utils.history) == null ? void 0 : _a2.replace(url);
    setLoading(false);
  };
  (0, import_react.useEffect)(() => {
    if (isRetailTemplate) {
      handleForm();
    }
  }, [isRetailTemplate]);
  const handleForm = async (isSubmit) => {
    var _a2;
    const carts = await appointmentBooking.getCart();
    const key = "isRetailFormSubmit";
    const isRetailFormSubmit = appointmentBooking.getOtherData(key);
    const callback = () => {
      if (isSubmit) {
        onCheckout();
      }
    };
    if (isRetailFormSubmit) {
      return callback();
    }
    const params = (0, import_utils3.getFormParams)(carts);
    (_a2 = utils == null ? void 0 : utils.action) == null ? void 0 : _a2.call(utils, {
      type: "pisell1.goProductForm",
      data: params,
      callback: async (e) => {
        var _a3;
        if ((e == null ? void 0 : e.type) !== "close") {
          await appointmentBooking.updateCart({
            _id: (_a3 = carts == null ? void 0 : carts[0]) == null ? void 0 : _a3._id,
            relationForms: e
          });
          appointmentBooking.setOtherData(key, true);
          callback();
        }
        setLoading(false);
      }
    });
  };
  const handleNext = async () => {
    var _a2;
    const { deposit } = summary;
    if ((deposit == null ? void 0 : deposit.hasDeposit) && ((_a2 = deposit == null ? void 0 : deposit.protocols) == null ? void 0 : _a2.length) > 0 && !isAgreeDepositPolicy) {
      return (0, import_utils2.Toast)(import_utils.locales.getText("pisell2.appointment.deposit.agree.policy"));
    }
    setLoading(true);
    if (isRetailTemplate) {
      handleForm(true);
      return;
    }
    onCheckout();
  };
  const onCheckout = async () => {
    try {
      const { deposit } = summary;
      const { data } = await appointmentBooking.submitOrder();
      const { is_deposit, deposit_amount, total_amount, order_id } = data || {};
      const _total = is_deposit ? deposit_amount : total_amount;
      if (_total > 0) {
        utils.action({
          type: "pisell1.payment",
          data: { ...data, total_amount: _total },
          callback: (e) => {
          }
        });
        setTimeout(() => {
          setLoading(false);
        }, 200);
      } else {
        if (deposit == null ? void 0 : deposit.hasDeposit) {
          goPaymentFinish(order_id, _total);
          return;
        }
        const freeResult = await appointmentBooking.getFreeProduct(order_id);
        console.log(freeResult, "freeResult");
        if (freeResult == null ? void 0 : freeResult.status) {
          goPaymentFinish(order_id, total_amount);
        } else {
          (0, import_utils2.Toast)(freeResult == null ? void 0 : freeResult.message);
        }
      }
    } catch (error) {
      (0, import_utils2.Toast)((error == null ? void 0 : error.message) || "");
      setTimeout(() => {
        setLoading(false);
      }, 50);
    }
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    next: handleNext
  }));
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "appointment-confirm-information-wrap" }, (0, import_utils.isMobile)() ? /* @__PURE__ */ import_react.default.createElement(import_Item.default, { onNext: handleNext }) : /* @__PURE__ */ import_react.default.createElement("div", { className: "appointment-confirm-information-content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "image" }, /* @__PURE__ */ import_react.default.createElement(
    "img",
    {
      src: `${config == null ? void 0 : config.awsStatic}/checkout_confirm.png`,
      loading: "lazy"
    }
  )), /* @__PURE__ */ import_react.default.createElement("span", { className: "tips" }, import_utils.locales.getText("pisell2.appointment.tips")), /* @__PURE__ */ import_react.default.createElement("span", { className: "tips-content" }, import_utils.locales.getText("pisell2.appointment.tips.content"))));
});
var ConfirmInformation_default = ConfirmInformation;
