import React, { useEffect, useContext, useMemo } from 'react';
import AddService from "./addService";
import { Context } from "../../info/model";
import EditService from "./editService";
import "../../info/service/index.less";
import useInfoHolder from "../../info/hooks/useInfoHolder";
import Lists from "../../info/service/Lists";
import useQuotation from "../../hooks/useQuotation";
import usePromotion from "../../hooks/usePromotion";
import EditNormalService from "./editNormalService";
import { setOpenNormalServiceHandler } from "./openNormalService";
import { useIsActive } from "../../../ticketBooking/context/IsActiveContext";
var Service = function Service() {
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var isActive = useIsActive();
  var _useInfoHolder = useInfoHolder(state, dispatch),
    detailRef = _useInfoHolder.detailRef,
    handleEdit = _useInfoHolder.handleEdit,
    handleAction = _useInfoHolder.handleAction,
    dispatchService = _useInfoHolder.dispatchService,
    showAddTimeModal = _useInfoHolder.showAddTimeModal,
    editNormalServiceRef = _useInfoHolder.editNormalServiceRef;
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  useEffect(function () {
    // 将内部的 EditNormalService ref 暴露为一个可全局调用的函数
    if (isActive && state.isBookingTicket2) {
      setOpenNormalServiceHandler(function (payload) {
        var _editNormalServiceRef;
        editNormalServiceRef === null || editNormalServiceRef === void 0 || (_editNormalServiceRef = editNormalServiceRef.current) === null || _editNormalServiceRef === void 0 || _editNormalServiceRef.init(payload);
      });
    }
    // return () => {
    //   // 组件卸载时清理 handler，避免持有失效的 ref
    //   setOpenNormalServiceHandler(undefined);
    // };
  }, [isActive]);

  // const clearCart = useClearCart(state, dispatch);

  var _useQuotation = useQuotation(state, dispatch),
    dom = _useQuotation.dom;
  var _usePromotion = usePromotion(state, dispatch, {
      onGiftSelection: function onGiftSelection(params) {
        console.log(params, 'params444');
      }
    }),
    run = _usePromotion.run,
    formatProductList = _usePromotion.formatProductList;
  useEffect(function () {
    var unsubscribe = run();
    return function () {
      unsubscribe === null || unsubscribe === void 0 || unsubscribe();
    };
  }, [run]);
  var renderMemo = useMemo(function () {
    console.log('render info22222');
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__booking-service-wrap"
    }, /*#__PURE__*/React.createElement(Lists, {
      isParallelResource: true,
      onEdit: handleEdit,
      onAction: handleAction,
      onChange: function onChange(e, type) {
        return dispatchService(type || 'setService', e);
      }
    }), !state.isBookingCreatePage ? /*#__PURE__*/React.createElement(AddService, {
      onEdit: handleEdit
    }) : null), /*#__PURE__*/React.createElement(EditService, {
      ref: detailRef
    }), /*#__PURE__*/React.createElement(EditNormalService, {
      ref: editNormalServiceRef
    }), dom);
  }, [dom, showAddTimeModal]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, renderMemo);
};
export default /*#__PURE__*/React.memo(Service);