function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * 普通商品详情弹窗：基于 SkuDetailModal，仅 open / close / confirm / remove 回调，不依赖 Booking Context。
 */
import React, { forwardRef, useImperativeHandle, useRef } from 'react';
import { useMemoizedFn } from 'ahooks';
import { cloneDeep } from 'lodash';
import SkuDetailModal from "../../pro/skuDetailModal";
import { setProductPrice } from "../../components/booking/info2/service/editService/utils";
import { formatDataKey } from "../../components/ticketBooking/components/addServiceVariant/utils";
import { getProductTotalPrice } from "../../components/booking/utils";
var NormalProductDetailModal = /*#__PURE__*/forwardRef(function (props, ref) {
  var onConfirmProp = props.onConfirm,
    onRemoveProp = props.onRemove,
    onCloseProp = props.onClose;
  var modalRef = useRef(null);
  var createPayloadRef = useRef(null);
  var openOnCloseRef = useRef(undefined);
  useImperativeHandle(ref, function () {
    return {
      init: function init(payload) {
        openOnCloseRef.current = payload === null || payload === void 0 ? void 0 : payload.onClose;
        if ((payload === null || payload === void 0 ? void 0 : payload.mode) === 'create') {
          var 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 || payload === void 0 ? void 0 : payload.mode) === 'edit') {
          var _p = payload;
          modalRef.current.open(_p.cacheItem);
          return;
        }
        modalRef.current.open(payload);
      }
    };
  });
  var handleRemove = useMemoizedFn(function (productData) {
    onRemoveProp === null || onRemoveProp === void 0 || onRemoveProp(productData);
  });
  var handleConfirm = useMemoizedFn(function (data) {
    var _data$_extend$origin_2, _data$_extend7, _data$_extend8, _data$_extend9, _updated$_extend;
    if (createPayloadRef.current) {
      var _data$_extend, _data$_extend$origin_, _data$_extend2, _data$_extend3, _data$_extend4, _data$_extend5, _data$_extend6;
      var payload = createPayloadRef.current;
      createPayloadRef.current = null;
      var quantity = Number((data === null || data === void 0 || (_data$_extend = data._extend) === null || _data$_extend === void 0 ? void 0 : _data$_extend.quantity) || 1);
      var detail = _objectSpread(_objectSpread({}, data || {}), {}, {
        id: payload.productId,
        product_id: payload.productId,
        _extend: _objectSpread(_objectSpread({}, (data === null || data === void 0 ? void 0 : data._extend) || {}), {}, {
          origin_total: (_data$_extend$origin_ = data === null || data === void 0 || (_data$_extend2 = data._extend) === null || _data$_extend2 === void 0 ? void 0 : _data$_extend2.origin_total) !== null && _data$_extend$origin_ !== void 0 ? _data$_extend$origin_ : data === null || data === void 0 || (_data$_extend3 = data._extend) === null || _data$_extend3 === void 0 ? void 0 : _data$_extend3.total,
          quantity: quantity,
          skuValue: cloneDeep((data === null || data === void 0 || (_data$_extend4 = data._extend) === null || _data$_extend4 === void 0 ? void 0 : _data$_extend4.skuValue) || {})
        })
      });
      data._extend.total = getProductTotalPrice(data);
      detail = setProductPrice(detail, {
        newTotal: data === null || data === void 0 || (_data$_extend5 = data._extend) === null || _data$_extend5 === void 0 ? void 0 : _data$_extend5.total,
        origin_total: (_data$_extend6 = data._extend) === null || _data$_extend6 === void 0 ? void 0 : _data$_extend6.origin_total
      });
      var formattedLine = _objectSpread(_objectSpread({
        product_id: payload.productId
      }, detail._extend.other), {}, {
        quantity: quantity
      });
      formattedLine = formatDataKey(formattedLine);
      onConfirmProp === null || onConfirmProp === void 0 || onConfirmProp({
        mode: 'create',
        data: formattedLine,
        detail: detail
      });
      return;
    }
    var total = getProductTotalPrice(data);
    data._extend.total = total;
    data._extend.origin_total = (_data$_extend$origin_2 = (_data$_extend7 = data._extend) === null || _data$_extend7 === void 0 ? void 0 : _data$_extend7.origin_total) !== null && _data$_extend$origin_2 !== void 0 ? _data$_extend$origin_2 : total;
    if (data !== null && data !== void 0 && data.is_bundle) {
      data._extend.bundle_edit = 1;
    }
    data._extend._skuValue = cloneDeep((data === null || data === void 0 || (_data$_extend8 = data._extend) === null || _data$_extend8 === void 0 ? void 0 : _data$_extend8.skuValue) || {});
    var updated = setProductPrice(data, {
      newTotal: total,
      origin_total: (_data$_extend9 = data._extend) === null || _data$_extend9 === void 0 ? void 0 : _data$_extend9.origin_total
    });
    updated.note = (_updated$_extend = updated._extend) === null || _updated$_extend === void 0 ? void 0 : _updated$_extend.note;
    onConfirmProp === null || onConfirmProp === void 0 || onConfirmProp({
      mode: 'edit',
      data: updated
    });
  });
  var handleClose = useMemoizedFn(function () {
    var _openOnCloseRef$curre;
    createPayloadRef.current = null;
    (_openOnCloseRef$curre = openOnCloseRef.current) === null || _openOnCloseRef$curre === void 0 || _openOnCloseRef$curre.call(openOnCloseRef);
    openOnCloseRef.current = undefined;
    onCloseProp === null || onCloseProp === void 0 || onCloseProp();
  });
  return /*#__PURE__*/React.createElement(SkuDetailModal, {
    onClose: handleClose,
    onConfirm: handleConfirm,
    onRemove: handleRemove,
    ref: modalRef
  });
});
NormalProductDetailModal.displayName = 'NormalProductDetailModal';
export default NormalProductDetailModal;