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); }
import { useMemoizedFn } from "ahooks";
import { useEffect, useMemo, useRef } from "react";
import useHolderConfig from "./useHolderConfig";
import { isOpenAddTime } from "../utils";
import useGetHolderList from "./useGetHolderList";
import { getProductExtend } from "../../info2/service/addService/utils";
import { getIsNoSpecAndPackage } from "../../info2/utilsByBooking";
var useInfoHolder = function useInfoHolder(state, dispatch) {
  var _state$client, _state$bookingConfig2;
  var detailRef = useRef();
  var editNormalServiceRef = useRef(null);
  var _useHolderConfig = useHolderConfig(state),
    holder = _useHolderConfig.holder,
    isFormSubject = _useHolderConfig.isFormSubject,
    appointment_subject = _useHolderConfig.appointment_subject;
  var getHolders = useGetHolderList(state, dispatch);
  useEffect(function () {
    if (isFormSubject) {
      getHolders();
    }
  }, [appointment_subject, holder, (_state$client = state.client) === null || _state$client === void 0 || (_state$client = _state$client.value) === null || _state$client === void 0 ? void 0 : _state$client.id]);
  var appointment_card = useMemo(function () {
    var _state$bookingConfig;
    return (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.information_tab) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_card) || {
      quick_time: 1
    };
  }, [state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.config) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.information_tab]);
  var showAddTimeModal = useMemo(function () {
    return isOpenAddTime(state);
  }, [state, appointment_card.quick_time]);
  var handleEdit = useMemoizedFn(function (cacheItem, type) {
    var isNoSpecAndPackage = getIsNoSpecAndPackage(cacheItem);
    if (isNoSpecAndPackage) {
      var _editNormalServiceRef;
      (_editNormalServiceRef = editNormalServiceRef.current) === null || _editNormalServiceRef === void 0 || _editNormalServiceRef.init(cacheItem);
    } else {
      var _detailRef$current, _detailRef$current$in;
      (_detailRef$current = detailRef.current) === null || _detailRef$current === void 0 || (_detailRef$current$in = _detailRef$current.init) === null || _detailRef$current$in === void 0 || _detailRef$current$in.call(_detailRef$current, cacheItem, false, type === 'edit_product');
    }
  });
  var dispatchService = function dispatchService(type, payload) {
    dispatch({
      type: type,
      payload: _objectSpread({}, payload)
    });
  };
  var handleAction = useMemoizedFn(function (dataSource) {
    var cacheItem = getProductExtend({
      cacheItem: _objectSpread(_objectSpread({}, dataSource._origin), {}, {
        new: 0
      }),
      state: state
    });
    dispatch({
      type: "setService",
      payload: {
        addTimeModal: true,
        cacheItem: cacheItem
      }
    });
  });
  return {
    detailRef: detailRef,
    handleEdit: handleEdit,
    dispatchService: dispatchService,
    handleAction: handleAction,
    showAddTimeModal: showAddTimeModal,
    editNormalServiceRef: editNormalServiceRef
  };
};
export default useInfoHolder;