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/plus/normalProductDetailModal/index.tsx
var normalProductDetailModal_exports = {};
__export(normalProductDetailModal_exports, {
  default: () => normalProductDetailModal_default
});
module.exports = __toCommonJS(normalProductDetailModal_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_lodash = require("lodash");
var import_skuDetailModal = __toESM(require("../../pro/skuDetailModal"));
var import_utils = require("../../components/booking/info2/service/editService/utils");
var import_utils2 = require("../../components/ticketBooking/components/addServiceVariant/utils");
var import_utils3 = require("../../components/booking/utils");
var NormalProductDetailModal = (0, import_react.forwardRef)(
  (props, ref) => {
    const { onConfirm: onConfirmProp, onRemove: onRemoveProp, onClose: onCloseProp } = props;
    const modalRef = (0, import_react.useRef)(null);
    const createPayloadRef = (0, import_react.useRef)(null);
    const openOnCloseRef = (0, import_react.useRef)(void 0);
    (0, import_react.useImperativeHandle)(ref, () => ({
      init: (payload) => {
        openOnCloseRef.current = payload == null ? void 0 : payload.onClose;
        if ((payload == null ? void 0 : payload.mode) === "create") {
          const p = payload;
          createPayloadRef.current = p;
          modalRef.current.open({
            id: p.productId,
            date: p.date,
            is_eject: p.isEject,
            productData: p.productData
          });
          return;
        }
        createPayloadRef.current = null;
        if ((payload == null ? void 0 : payload.mode) === "edit") {
          const p = payload;
          modalRef.current.open(p.cacheItem);
          return;
        }
        modalRef.current.open(payload);
      }
    }));
    const handleRemove = (0, import_ahooks.useMemoizedFn)((productData) => {
      onRemoveProp == null ? void 0 : onRemoveProp(productData);
    });
    const handleConfirm = (0, import_ahooks.useMemoizedFn)((data) => {
      var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
      if (createPayloadRef.current) {
        const payload = createPayloadRef.current;
        createPayloadRef.current = null;
        const quantity = Number(((_a = data == null ? void 0 : data._extend) == null ? void 0 : _a.quantity) || 1);
        const basePrice = Number(((_b = data == null ? void 0 : data._extend) == null ? void 0 : _b.price) ?? (data == null ? void 0 : data.price) ?? 0) || 0;
        let detail = {
          ...data || {},
          id: payload.productId,
          product_id: payload.productId,
          _extend: {
            ...(data == null ? void 0 : data._extend) || {},
            price: ((_c = data == null ? void 0 : data._extend) == null ? void 0 : _c.price) ?? basePrice,
            origin_total: ((_d = data == null ? void 0 : data._extend) == null ? void 0 : _d.origin_total) ?? ((_e = data == null ? void 0 : data._extend) == null ? void 0 : _e.total) ?? basePrice,
            quantity,
            skuValue: (0, import_lodash.cloneDeep)(((_f = data == null ? void 0 : data._extend) == null ? void 0 : _f.skuValue) || {})
          }
        };
        const lineTotal = (0, import_utils3.getProductTotalPrice)(detail);
        detail._extend.total = lineTotal;
        detail = (0, import_utils.setProductPrice)(detail, {
          newTotal: lineTotal,
          origin_total: detail._extend.origin_total
        });
        let formattedLine = {
          product_id: payload.productId,
          ...detail._extend.other,
          quantity
        };
        formattedLine = (0, import_utils2.formatDataKey)(formattedLine);
        onConfirmProp == null ? void 0 : onConfirmProp({
          mode: "create",
          data: formattedLine,
          detail
        });
        return;
      }
      const extend = {
        price: (data == null ? void 0 : data.price) ?? 0,
        ...(data == null ? void 0 : data._extend) || {}
      };
      const normalized = {
        ...data || {},
        _extend: extend
      };
      const total = (0, import_utils3.getProductTotalPrice)(normalized);
      normalized._extend.total = total;
      normalized._extend.origin_total = ((_g = normalized._extend) == null ? void 0 : _g.origin_total) ?? total;
      if (normalized == null ? void 0 : normalized.is_bundle) {
        normalized._extend.bundle_edit = 1;
      }
      normalized._extend._skuValue = (0, import_lodash.cloneDeep)(((_h = normalized == null ? void 0 : normalized._extend) == null ? void 0 : _h.skuValue) || {});
      const updated = (0, import_utils.setProductPrice)(normalized, {
        newTotal: total,
        origin_total: (_i = normalized._extend) == null ? void 0 : _i.origin_total
      });
      updated.note = (_j = updated._extend) == null ? void 0 : _j.note;
      onConfirmProp == null ? void 0 : onConfirmProp({ mode: "edit", data: updated });
    });
    const handleClose = (0, import_ahooks.useMemoizedFn)(() => {
      var _a;
      createPayloadRef.current = null;
      (_a = openOnCloseRef.current) == null ? void 0 : _a.call(openOnCloseRef);
      openOnCloseRef.current = void 0;
      onCloseProp == null ? void 0 : onCloseProp();
    });
    return /* @__PURE__ */ import_react.default.createElement(import_skuDetailModal.default, { onClose: handleClose, onConfirm: handleConfirm, onRemove: handleRemove, ref: modalRef });
  }
);
NormalProductDetailModal.displayName = "NormalProductDetailModal";
var normalProductDetailModal_default = NormalProductDetailModal;
