import React, { useEffect, useContext, useMemo, useRef } from 'react';
import AddService from "./addService";
import { Context } from "../model";
import EditService from "./editService";
import "./index.less";
import ServiceList from "./serviceList";
import AddTimeModal from "./addTimeModal";
import useClearCart from "../../hooks/useClearCart";
import useQuotation from "../../hooks/useQuotation";
var Service = function Service() {
  var _state$bookingConfig2;
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var detailRef = useRef();
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  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 () {
    // event预约不需要
    if ((state === null || state === void 0 ? void 0 : state.renderType) === 'eventBooking') {
      return false;
    }

    // 禁止编辑
    if (state !== null && state !== void 0 && state.disabledEdit) {
      return false;
    }

    // 未开启加时
    if (appointment_card.quick_time !== 1) {
      return false;
    }
    return true;
  }, [state, appointment_card.quick_time]);
  var handleEdit = function handleEdit(cacheItem, type) {
    setTimeout(function () {
      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');
    }, 100);
  };
  var clearCart = useClearCart(state, dispatch);
  var _useQuotation = useQuotation(state, dispatch),
    dom = _useQuotation.dom;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__booking-service-wrap"
  }, clearCart, /*#__PURE__*/React.createElement(ServiceList, {
    onEdit: handleEdit
  }), !state.isBookingCreatePage ? /*#__PURE__*/React.createElement(AddService, {
    onEdit: handleEdit
  }) : null), /*#__PURE__*/React.createElement(EditService, {
    ref: detailRef
  }), showAddTimeModal && /*#__PURE__*/React.createElement(AddTimeModal, null), dom);
};
export default /*#__PURE__*/React.memo(Service);