import React, { useSyncExternalStore } from 'react';
import dayjs from 'dayjs';
import { locales } from '@pisell/utils';
import { SalesSdkMultiDayTimeSelectModal } from "../components/SalesSdkMultiDayTimeSelectModal";
import { useSalesSdkHostBridges } from "../context/SalesSdkHostBridgesContext";

/**
 * 承接 `openMultiDaySelect` Promise Host 的跨日弹窗渲染（实例级 bridge）。
 */
export var MultiDayHostRenderer = function MultiDayHostRenderer() {
  var bridges = useSalesSdkHostBridges();
  var bridge = bridges.multiDay;
  var pending = useSyncExternalStore(bridge.subscribe, bridge.getSnapshot, function () {
    return null;
  });
  var open = !!pending;
  var payload = pending === null || pending === void 0 ? void 0 : pending.payload;
  var defaultStartDate = payload !== null && payload !== void 0 && payload.defaultStartDate ? dayjs(payload.defaultStartDate) : dayjs();
  if (!open) {
    return null;
  }
  return /*#__PURE__*/React.createElement(SalesSdkMultiDayTimeSelectModal, {
    open: open,
    productName: payload === null || payload === void 0 ? void 0 : payload.productName,
    locale: locales.locale,
    defaultStartDate: defaultStartDate.isValid() ? defaultStartDate : dayjs(),
    onCancel: function onCancel() {
      return bridge.complete(null);
    },
    onConfirm: function onConfirm(startDate, endDate, duration) {
      bridge.complete({
        startDate: startDate.format('YYYY-MM-DD'),
        endDate: endDate.format('YYYY-MM-DD'),
        duration: duration
      });
    }
  });
};