import React, { forwardRef, useContext, useImperativeHandle, useRef } from 'react';
import { useMemoizedFn } from 'ahooks';
import SkuDetailModal from "../../../../../pro/skuDetailModal";
import { Context } from "../../../info/model";
import { setProductPrice } from "../editService/utils";
var EditNormalService = /*#__PURE__*/forwardRef(function (props, ref) {
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var modalRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      init: function init(cacheItem) {
        modalRef.current.open(cacheItem);
      }
    };
  });
  var handleRemove = useMemoizedFn(function (productData) {
    var _ref, _state$service;
    var newProducts = (_ref = (state === null || state === void 0 || (_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.value) || []) === null || _ref === void 0 ? void 0 : _ref.filter(function (item) {
      return item._id !== productData._id;
    });
    dispatch({
      type: 'setService',
      payload: {
        editModal: false,
        value: newProducts,
        edit: true,
        cacheItem: null
      }
    });
  });
  var handleConfirm = useMemoizedFn(function (data) {
    var _ref2, _state$service2;
    var newProducts = (_ref2 = (state === null || state === void 0 || (_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.value) || []) === null || _ref2 === void 0 ? void 0 : _ref2.map(function (item) {
      if (item._id == data._id) {
        var _data$_extend, _data$_extend2;
        data = setProductPrice(data, {
          newTotal: (_data$_extend = data._extend) === null || _data$_extend === void 0 ? void 0 : _data$_extend.total,
          origin_total: (_data$_extend2 = data._extend) === null || _data$_extend2 === void 0 ? void 0 : _data$_extend2.origin_total
        });
        return data;
      }
      return item;
    });
    dispatch({
      type: 'setService',
      payload: {
        editModal: false,
        value: newProducts,
        edit: true,
        cacheItem: null
      }
    });
  });
  var handleClose = useMemoizedFn(function () {
    dispatch({
      type: 'setService',
      payload: {
        editModal: false,
        cacheItem: null,
        changeService: false
      }
    });
  });
  return /*#__PURE__*/React.createElement(SkuDetailModal, {
    onClose: handleClose,
    onConfirm: handleConfirm,
    onRemove: handleRemove,
    ref: modalRef
  });
});
export default EditNormalService;