import React, { useContext, useMemo } from 'react';
import { AdjustTimeModal } from "../../../plus/salesSdk/adjustTime";
import { Context } from "../info/model";
import { getResourceEndTime, mapAdjustTimeBooking, mapAdjustTimeProducts } from "./adapter";

/**
 * 预约详情加时弹窗单例：由 state.service.addTimeModal + cacheItem 驱动。
 */
export var AdjustTimeModalHost = function AdjustTimeModalHost() {
  var _state$service, _state$service2, _state$service3;
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var open = Boolean((_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.addTimeModal);
  var cacheItem = (_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.cacheItem;
  var resourceEndTime = useMemo(function () {
    if (!open || !cacheItem) return undefined;
    return getResourceEndTime(state, cacheItem);
  }, [open, cacheItem, (_state$service3 = state.service) === null || _state$service3 === void 0 ? void 0 : _state$service3.resourcesOrigin]);
  var booking = useMemo(function () {
    if (!cacheItem) return null;
    return mapAdjustTimeBooking(cacheItem);
  }, [cacheItem]);
  var products = useMemo(function () {
    if (!cacheItem) return [];
    return mapAdjustTimeProducts(state, cacheItem);
  }, [cacheItem, state.addTimeProducts]);
  var handleClose = function handleClose() {
    dispatch({
      type: 'setService',
      payload: {
        addTimeModal: false
      }
    });
  };
  if (!booking) {
    return null;
  }
  return /*#__PURE__*/React.createElement(AdjustTimeModal, {
    open: open,
    booking: booking,
    products: products,
    resourceEndTime: resourceEndTime,
    currencySymbol: state.amountSymbol || '$',
    onCancel: handleClose,
    onCheckout: function onCheckout(result) {
      handleClose();
      console.info('[AdjustTime] checkout result', result);
    }
  });
};
export default AdjustTimeModalHost;