function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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); }
import { useEffect, useMemo, useRef, useState } from 'react';
import dayjs from 'dayjs';
import { resolveResourceIdFromRow } from "./bookingDetailHostActions";
import { EMPTY_RESOURCE_BOOKING_ITEM_LISTS } from "./bookingDetailModalConstants";
/**
 * 把宿主 sales.getResourceBookingItem 任意返回值规范为三段子列表数组。
 */
export function normalizeResourceBookingItemLists(raw) {
  if (raw == null || _typeof(raw) !== 'object') {
    return EMPTY_RESOURCE_BOOKING_ITEM_LISTS;
  }
  var r = raw;
  var arr = function arr(v) {
    return Array.isArray(v) ? v : [];
  };
  return {
    bookings: arr(r.bookings),
    completedBookings: arr(r.completedBookings),
    unpaidBookings: arr(r.unpaidBookings)
  };
}
/**
 * 打开弹窗时拉取 sales.getResourceBookingItem，并按 activeTab 返回当前 Tab 的 bookings 列表。
 */
export function useResourceBookingItem(activeTab, options) {
  var open = options.open,
    raw = options.raw,
    tableRow = options.tableRow,
    floorDayBookingList = options.floorDayBookingList,
    currentTime = options.currentTime,
    followWallClock = options.followWallClock,
    pisellos = options.pisellos,
    disabled = options.disabled;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    resourceBookingItem = _useState2[0],
    setResourceBookingItem = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    resourceBookingItemLoading = _useState4[0],
    setResourceBookingItemLoading = _useState4[1];
  var resourceBookingItemReqIdRef = useRef(0);
  useEffect(function () {
    if (!open || raw == null || disabled) {
      setResourceBookingItem(null);
      setResourceBookingItemLoading(false);
      resourceBookingItemReqIdRef.current += 1;
    }
  }, [open, raw, disabled]);
  useEffect(function () {
    var _salesModule;
    if (!open || raw == null || disabled) return;
    var getModule = pisellos === null || pisellos === void 0 ? void 0 : pisellos.getModule;
    if (typeof getModule !== 'function') return;
    var salesModule;
    try {
      salesModule = getModule.call(pisellos, 'sales');
    } catch (_unused) {
      return;
    }
    var fn = (_salesModule = salesModule) === null || _salesModule === void 0 ? void 0 : _salesModule.getResourceBookingItem;
    if (typeof fn !== 'function') return;
    var resourceId = resolveResourceIdFromRow(tableRow);
    if (resourceId == null) return;
    var fmt = 'YYYY-MM-DD HH:mm';
    var currentTimeStr = currentTime ? dayjs(currentTime).format(fmt) : dayjs().format(fmt);
    /**
     * 与 useReservationSalesHostData 里 getResourceBookingList 的处理对齐：
     * - 跟随墙钟时 `deviceTime` 与 `currentTime` 相同，避免与 `dayjs()` 差一分钟；
     * - 关闭跟随后 `deviceTime` 走实时设备时间，保留「游标(业务) vs 此刻(设备)」双通道语义。
     */
    var deviceTimeStr = followWallClock ? currentTimeStr : dayjs().format(fmt);
    var bookingList = Array.isArray(floorDayBookingList) ? floorDayBookingList : [];
    var reqId = ++resourceBookingItemReqIdRef.current;
    setResourceBookingItemLoading(true);
    var callParams = {
      resourceId: resourceId,
      currentTime: currentTimeStr,
      bookingList: bookingList,
      deviceTime: deviceTimeStr
    };
    var apply = function apply(result) {
      if (resourceBookingItemReqIdRef.current !== reqId) return;
      setResourceBookingItem(normalizeResourceBookingItemLists(result));
      setResourceBookingItemLoading(false);
    };
    var fail = function fail() {
      if (resourceBookingItemReqIdRef.current !== reqId) return;
      setResourceBookingItem(EMPTY_RESOURCE_BOOKING_ITEM_LISTS);
      setResourceBookingItemLoading(false);
    };
    try {
      var result = fn.call(salesModule, callParams);
      if (result != null && typeof result.then === 'function') {
        result.then(apply).catch(fail);
      } else {
        apply(result);
      }
    } catch (_unused2) {
      fail();
    }
  }, [open, raw, tableRow, floorDayBookingList, currentTime, followWallClock, pisellos, disabled]);
  var activeTabBookings = useMemo(function () {
    if (!resourceBookingItem) return [];
    var arr = activeTab === 'current-occupied' ? resourceBookingItem.bookings : activeTab === 'ended' ? resourceBookingItem.completedBookings : resourceBookingItem.unpaidBookings;
    return arr.filter(function (x) {
      return x != null && _typeof(x) === 'object';
    });
  }, [resourceBookingItem, activeTab]);
  return {
    resourceBookingItem: resourceBookingItem,
    resourceBookingItemLoading: resourceBookingItemLoading,
    activeTabBookings: activeTabBookings
  };
}