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/booking/info2/service/editNormalService/index.tsx
var editNormalService_exports = {};
__export(editNormalService_exports, {
  default: () => editNormalService_default
});
module.exports = __toCommonJS(editNormalService_exports);
var import_react = __toESM(require("react"));
var import_ahooks = require("ahooks");
var import_skuDetailModal = __toESM(require("../../../../../pro/skuDetailModal"));
var import_model = require("../../../info/model");
var import_utils = require("../editService/utils");
var import_utils2 = require("../../../../ticketBooking/components/addServiceVariant/utils");
var import_utils3 = require("../../../utils");
var import_lodash = require("lodash");
var import_useCartChangeEffect = __toESM(require("../../../../ticketBooking/hooks/pisellos/useCartChangeEffect"));
var EditNormalService = (0, import_react.forwardRef)((props, ref) => {
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const { onCartChange } = (0, import_useCartChangeEffect.default)();
  const modalRef = (0, import_react.useRef)(null);
  const createPayloadRef = (0, import_react.useRef)(null);
  const openOnCloseRef = (0, import_react.useRef)(void 0);
  const editCallbackRef = (0, import_react.useRef)(void 0);
  const [skuDetailConfig, setSkuDetailConfig] = (0, import_react.useState)(null);
  (0, import_react.useImperativeHandle)(ref, () => ({
    init: (payload) => {
      setSkuDetailConfig((payload == null ? void 0 : payload.skuDetailConfig) || {});
      openOnCloseRef.current = payload == null ? void 0 : payload.onClose;
      if ((payload == null ? void 0 : payload.mode) === "create") {
        createPayloadRef.current = payload;
        editCallbackRef.current = void 0;
        modalRef.current.open({
          id: payload.productId,
          date: payload.date,
          is_eject: payload.isEject,
          productData: payload.productData,
          skuDetailConfig: (payload == null ? void 0 : payload.skuDetailConfig) || {}
        });
        return;
      }
      createPayloadRef.current = null;
      editCallbackRef.current = payload == null ? void 0 : payload.callback;
      if ((payload == null ? void 0 : payload.mode) === "edit") {
        const cacheItemWithConfig = {
          ...payload.cacheItem,
          skuDetailConfig: (payload == null ? void 0 : payload.skuDetailConfig) || {}
        };
        modalRef.current.open(cacheItemWithConfig);
        return;
      }
      modalRef.current.open(payload);
    }
  }));
  const handleRemove = (0, import_ahooks.useMemoizedFn)((productData) => {
    var _a, _b;
    const newProducts = (_b = ((_a = state == null ? void 0 : state.service) == null ? void 0 : _a.value) || []) == null ? void 0 : _b.filter((item) => {
      return item._id !== productData._id;
    });
    dispatch({
      type: "setService",
      payload: {
        editModal: false,
        value: newProducts,
        edit: true,
        cacheItem: null
      }
    });
    if (skuDetailConfig == null ? void 0 : skuDetailConfig.onRemove) {
      skuDetailConfig == null ? void 0 : skuDetailConfig.onRemove(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 skuValue = ((_a = data == null ? void 0 : data._extend) == null ? void 0 : _a.skuValue) || {
        product_id: payload.productId,
        product_variant_id: 0,
        bundle: [],
        option: []
      };
      const quantity = Number(((_b = data == null ? void 0 : data._extend) == null ? void 0 : _b.quantity) || 1);
      let detail = {
        ...data || {},
        id: payload.productId,
        product_id: payload.productId,
        _extend: {
          ...(data == null ? void 0 : data._extend) || {},
          origin_total: ((_c = data == null ? void 0 : data._extend) == null ? void 0 : _c.origin_total) ?? ((_d = data == null ? void 0 : data._extend) == null ? void 0 : _d.total),
          quantity,
          skuValue: (0, import_lodash.cloneDeep)(((_e = data == null ? void 0 : data._extend) == null ? void 0 : _e.skuValue) || {})
        }
      };
      data._extend.total = (0, import_utils3.getProductTotalPrice)(data);
      detail = (0, import_utils.setProductPrice)(detail, {
        newTotal: (_f = data == null ? void 0 : data._extend) == null ? void 0 : _f.total,
        origin_total: (_g = data._extend) == null ? void 0 : _g.origin_total
      });
      let e = {
        product_id: payload.productId,
        ...detail._extend.other,
        quantity
      };
      e = (0, import_utils2.formatDataKey)(e);
      payload.callback(e, void 0, detail, false);
      if (skuDetailConfig == null ? void 0 : skuDetailConfig.onConfirm) {
        skuDetailConfig == null ? void 0 : skuDetailConfig.onConfirm(data);
      }
      return;
    }
    console.log("handleConfirm", data, (_h = data == null ? void 0 : data._extend) == null ? void 0 : _h.origin_total);
    const newProducts = (_j = ((_i = state == null ? void 0 : state.service) == null ? void 0 : _i.value) || []) == null ? void 0 : _j.map((item) => {
      var _a2, _b2, _c2, _d2;
      if (item._id == data._id) {
        const total = (0, import_utils3.getProductTotalPrice)(data);
        data._extend.total = total;
        data._extend.origin_total = ((_a2 = data._extend) == null ? void 0 : _a2.origin_total) ?? total;
        if (data == null ? void 0 : data.is_bundle) {
          data._extend.bundle_edit = 1;
        }
        data._extend._skuValue = (0, import_lodash.cloneDeep)(((_b2 = data == null ? void 0 : data._extend) == null ? void 0 : _b2.skuValue) || {});
        data = (0, import_utils.setProductPrice)(data, {
          newTotal: total,
          origin_total: (_c2 = data._extend) == null ? void 0 : _c2.origin_total
        });
        data.note = (_d2 = data._extend) == null ? void 0 : _d2.note;
        return data;
      }
      return item;
    });
    if (editCallbackRef.current) {
      editCallbackRef.current(data);
      editCallbackRef.current = void 0;
    }
    if (skuDetailConfig == null ? void 0 : skuDetailConfig.onConfirm) {
      skuDetailConfig == null ? void 0 : skuDetailConfig.onConfirm(data);
      return;
    }
    onCartChange("update", newProducts);
    dispatch({
      type: "setService",
      payload: {
        editModal: false,
        value: newProducts,
        edit: true,
        cacheItem: null
      }
    });
  });
  const handleClose = (0, import_ahooks.useMemoizedFn)(() => {
    var _a;
    dispatch({
      type: "setService",
      payload: {
        editModal: false,
        cacheItem: null,
        changeService: false
      }
    });
    if (skuDetailConfig == null ? void 0 : skuDetailConfig.onClose) {
      skuDetailConfig == null ? void 0 : skuDetailConfig.onClose();
    }
    createPayloadRef.current = null;
    editCallbackRef.current = void 0;
    (_a = openOnCloseRef.current) == null ? void 0 : _a.call(openOnCloseRef);
    openOnCloseRef.current = void 0;
  });
  return /* @__PURE__ */ import_react.default.createElement(
    import_skuDetailModal.default,
    {
      onClose: handleClose,
      onConfirm: handleConfirm,
      onRemove: handleRemove,
      ref: modalRef
    }
  );
});
var editNormalService_default = EditNormalService;
