function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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; }
import dayjs from "dayjs";
/**
 * 计算资源在指定时间段内的总可用时间（以分钟为单位）
 * @param resource 需要计算可用时间的资源
 * @param timeSlots 要检查可用性的时间段
 * @param currentCapacity 当前预约所需的容量
 * @returns 总可用时间（分钟）
 */
export function calculateResourceAvailableTime(_ref) {
  var resource = _ref.resource,
    timeSlots = _ref.timeSlots,
    _ref$currentCapacity = _ref.currentCapacity,
    currentCapacity = _ref$currentCapacity === void 0 ? 1 : _ref$currentCapacity;
  // 过滤出与给定日期相同的资源时间
  var matchingTimes = resource.times.filter(function (time) {
    return dayjs(time.start_at).isSame(dayjs(timeSlots.start_at), 'day');
  });
  if (matchingTimes.length === 0) return 0;

  // 计算所有时间段与目标时间槽的重叠部分
  var overlaps = [];
  var _iterator = _createForOfIteratorHelper(matchingTimes),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var time = _step.value;
      // 计算实际重叠的时间段
      var overlapStart = dayjs(time.start_at).isAfter(dayjs(timeSlots.start_at)) ? dayjs(time.start_at) : dayjs(timeSlots.start_at);
      var overlapEnd = dayjs(time.end_at).isBefore(dayjs(timeSlots.end_at)) ? dayjs(time.end_at) : dayjs(timeSlots.end_at);

      // 只有当重叠时间段有效时才添加
      if (overlapStart.isBefore(overlapEnd)) {
        overlaps.push({
          start: overlapStart,
          end: overlapEnd
        });
      }
    }

    // 合并重叠的时间段，计算并集
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  if (overlaps.length === 0) return 0;

  // 按开始时间排序
  overlaps.sort(function (a, b) {
    return a.start.diff(b.start);
  });

  // 合并重叠的时间段
  var merged = [];
  var current = overlaps[0];
  for (var i = 1; i < overlaps.length; i++) {
    var next = overlaps[i];

    // 如果当前时间段与下一个时间段重叠或相邻，则合并
    if (current.end.isSameOrAfter(next.start)) {
      current.end = current.end.isAfter(next.end) ? current.end : next.end;
    } else {
      // 不重叠，添加当前时间段到结果中，开始处理下一个
      merged.push(current);
      current = next;
    }
  }
  merged.push(current);

  // 计算所有合并后时间段的总时长
  var totalAvailableMinutes = 0;
  for (var _i = 0, _merged = merged; _i < _merged.length; _i++) {
    var segment = _merged[_i];
    totalAvailableMinutes += segment.end.diff(segment.start, 'minute');
  }
  return totalAvailableMinutes;
}

/**
 * 查找最快可用的资源，如果有多个资源在相同时间点可用，则选择空闲时间最长的资源
 * @param resources 资源列表
 * @param currentCapacity 当前预约所需的容量
 * @param countMap 已预约数量映射
 * @returns 最快可用的资源
 */
export function findFastestAvailableResource(_ref2) {
  var resources = _ref2.resources,
    _ref2$currentCapacity = _ref2.currentCapacity,
    currentCapacity = _ref2$currentCapacity === void 0 ? 1 : _ref2$currentCapacity,
    _ref2$countMap = _ref2.countMap,
    countMap = _ref2$countMap === void 0 ? {} : _ref2$countMap;
  var currentTime = dayjs();
  var fastestTime = null;
  var fastestResources = [];
  console.log('[TimeslotUtils] 查找最快可用资源:', {
    currentTime: currentTime.format('YYYY-MM-DD HH:mm:ss'),
    resourceCount: resources.length,
    currentCapacity: currentCapacity,
    countMap: countMap
  });

  // 遍历所有资源，找到最快可用的时间点
  var _iterator2 = _createForOfIteratorHelper(resources),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var _resource = _step2.value;
      // 获取资源当天且还在工作时间内的时间段
      var todayTimes = _resource.times.filter(function (time) {
        var isToday = dayjs(time.start_at).isSame(currentTime, 'day');
        var isStillWorking = dayjs(time.end_at).isAfter(currentTime);
        return isToday && isStillWorking;
      });
      if (todayTimes.length === 0) {
        console.log("[TimeslotUtils] \u8D44\u6E90 ".concat(_resource.id, "(").concat(_resource.main_field, ") \u4ECA\u65E5\u65E0\u53EF\u7528\u65F6\u95F4\u6BB5"));
        continue;
      }
      var _iterator4 = _createForOfIteratorHelper(todayTimes),
        _step4;
      try {
        var _loop = function _loop() {
            var _time$event_list, _time$event_list2;
            var time = _step4.value;
            var workStartTime = dayjs(time.start_at);
            var workEndTime = dayjs(time.end_at);

            // 确定检查的起始时间（当前时间 vs 工作开始时间）
            var nextAvailableTime = currentTime.isBefore(workStartTime) ? workStartTime : currentTime;
            console.log("[TimeslotUtils] \u68C0\u67E5\u8D44\u6E90 ".concat(_resource.id, "(").concat(_resource.main_field, "):"), {
              workTime: "".concat(workStartTime.format('HH:mm'), "-").concat(workEndTime.format('HH:mm')),
              checkStartTime: nextAvailableTime.format('HH:mm:ss'),
              eventCount: ((_time$event_list = time.event_list) === null || _time$event_list === void 0 ? void 0 : _time$event_list.length) || 0
            });

            // 获取所有影响的预约事件（当前时间之后的）
            var relevantEvents = ((_time$event_list2 = time.event_list) === null || _time$event_list2 === void 0 ? void 0 : _time$event_list2.filter(function (event) {
              var eventEnd = dayjs(event.end_at);
              return eventEnd.isAfter(nextAvailableTime);
            })) || [];

            // 按开始时间排序
            relevantEvents.sort(function (a, b) {
              return dayjs(a.start_at).diff(dayjs(b.start_at));
            });

            // 检查从 nextAvailableTime 开始的可用性
            var finalAvailableTime = nextAvailableTime;
            var _iterator5 = _createForOfIteratorHelper(relevantEvents),
              _step5;
            try {
              for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
                var _event = _step5.value;
                var _eventStart = dayjs(_event.start_at);
                var _eventEnd = dayjs(_event.end_at);
                console.log("[TimeslotUtils] \u68C0\u67E5\u4E8B\u4EF6\u51B2\u7A81:", {
                  resourceId: _resource.id,
                  eventTime: "".concat(_eventStart.format('HH:mm'), "-").concat(_eventEnd.format('HH:mm')),
                  checkTime: finalAvailableTime.format('HH:mm:ss'),
                  pax: _event.pax || 1
                });

                // 检查资源类型和容量
                if (_resource.resourceType === 'single' || (_resource.capacity || 1) === 1) {
                  // 单人预约资源：检查时间冲突
                  if (finalAvailableTime.isBefore(_eventEnd) && _eventStart.isBefore(finalAvailableTime.add(30, 'minute'))) {
                    // 有冲突，使用事件结束时间
                    finalAvailableTime = _eventEnd;
                    console.log("[TimeslotUtils] \u53D1\u73B0\u65F6\u95F4\u51B2\u7A81\uFF0C\u8C03\u6574\u5230: ".concat(finalAvailableTime.format('HH:mm:ss')));
                  }
                } else {
                  // 多人预约资源：检查容量
                  var totalCapacity = _resource.capacity || 0;
                  var currentUsedCapacity = countMap[_resource.id] || 0;
                  var eventUsedCapacity = _event.pax || 1;

                  // 如果在事件时间范围内，检查容量是否足够
                  if (finalAvailableTime.isBefore(_eventEnd) && _eventStart.isBefore(finalAvailableTime.add(30, 'minute'))) {
                    var totalRequiredCapacity = currentUsedCapacity + currentCapacity + eventUsedCapacity;
                    if (totalRequiredCapacity > totalCapacity) {
                      // 容量不足，使用事件结束时间
                      finalAvailableTime = _eventEnd;
                      console.log("[TimeslotUtils] \u5BB9\u91CF\u4E0D\u8DB3\uFF0C\u8C03\u6574\u5230: ".concat(finalAvailableTime.format('HH:mm:ss')));
                    }
                  }
                }
              }

              // 确保可用时间在工作时间内
            } catch (err) {
              _iterator5.e(err);
            } finally {
              _iterator5.f();
            }
            if (finalAvailableTime.isAfter(workEndTime)) {
              console.log("[TimeslotUtils] \u8D44\u6E90 ".concat(_resource.id, " \u53EF\u7528\u65F6\u95F4\u8D85\u51FA\u5DE5\u4F5C\u65F6\u95F4\uFF0C\u8DF3\u8FC7"));
              return 0; // continue
            }
            console.log("[TimeslotUtils] \u8D44\u6E90 ".concat(_resource.id, "(").concat(_resource.main_field, ") \u6700\u5FEB\u53EF\u7528\u65F6\u95F4: ").concat(finalAvailableTime.format('HH:mm:ss')));

            // 更新最快可用时间和资源
            if (!fastestTime || finalAvailableTime.isBefore(fastestTime)) {
              fastestTime = finalAvailableTime;
              fastestResources = [_resource];
              console.log("[TimeslotUtils] \u66F4\u65B0\u6700\u5FEB\u65F6\u95F4: ".concat(fastestTime.format('HH:mm:ss'), ", \u8D44\u6E90: ").concat(_resource.main_field));
            } else if (finalAvailableTime.isSame(fastestTime)) {
              fastestResources.push(_resource);
              console.log("[TimeslotUtils] \u6DFB\u52A0\u76F8\u540C\u65F6\u95F4\u8D44\u6E90: ".concat(_resource.main_field));
            }
            return 1; // break
            // 每个资源只需要计算一次
          },
          _ret;
        for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
          _ret = _loop();
          if (_ret === 0) continue;
          if (_ret === 1) break;
        }
      } catch (err) {
        _iterator4.e(err);
      } finally {
        _iterator4.f();
      }
    }

    // 如果没有找到可用资源，返回null
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  if (!fastestTime || fastestResources.length === 0) {
    console.log('[TimeslotUtils] 未找到可用资源');
    return null;
  }
  console.log("[TimeslotUtils] \u627E\u5230 ".concat(fastestResources.length, " \u4E2A\u6700\u5FEB\u53EF\u7528\u8D44\u6E90\uFF0C\u65F6\u95F4: ").concat(fastestTime.format('HH:mm:ss')));

  // 如果只有一个最快可用的资源，直接返回
  if (fastestResources.length === 1) {
    console.log("[TimeslotUtils] \u8FD4\u56DE\u552F\u4E00\u6700\u5FEB\u8D44\u6E90: ".concat(fastestResources[0].main_field));
    return fastestResources[0];
  }

  // 如果有多个最快可用的资源，选择空闲时间最长的资源
  var selectedResource = fastestResources[0];
  var maxIdleTime = 0;
  console.log("[TimeslotUtils] \u6BD4\u8F83 ".concat(fastestResources.length, " \u4E2A\u8D44\u6E90\u7684\u7A7A\u95F2\u65F6\u95F4:"));
  for (var _i2 = 0, _fastestResources = fastestResources; _i2 < _fastestResources.length; _i2++) {
    var _workingTime$event_li;
    var resource = _fastestResources[_i2];
    // 找到该资源当天且还在工作的时间段
    var workingTime = resource.times.find(function (time) {
      var isToday = dayjs(time.start_at).isSame(fastestTime, 'day');
      var isStillWorking = dayjs(time.end_at).isAfter(fastestTime);
      return isToday && isStillWorking;
    });
    if (!workingTime) continue;
    var workEndTime = dayjs(workingTime.end_at);

    // 计算从最快可用时间到工作结束时间的空闲时长（分钟）
    var totalIdleTime = workEndTime.diff(fastestTime, 'minute');

    // 减去已有预约占用的时间
    var relevantEvents = ((_workingTime$event_li = workingTime.event_list) === null || _workingTime$event_li === void 0 ? void 0 : _workingTime$event_li.filter(function (event) {
      var eventStart = dayjs(event.start_at);
      var eventEnd = dayjs(event.end_at);
      // 只计算在最快可用时间之后的预约
      return eventEnd.isAfter(fastestTime) && eventStart.isAfter(fastestTime);
    })) || [];
    var _iterator3 = _createForOfIteratorHelper(relevantEvents),
      _step3;
    try {
      for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
        var event = _step3.value;
        var eventStart = dayjs(event.start_at);
        var eventEnd = dayjs(event.end_at);
        var eventDuration = eventEnd.diff(eventStart, 'minute');
        totalIdleTime -= eventDuration;
      }
    } catch (err) {
      _iterator3.e(err);
    } finally {
      _iterator3.f();
    }
    console.log("[TimeslotUtils] \u8D44\u6E90 ".concat(resource.id, "(").concat(resource.main_field, "):"), {
      工作结束时间: workEndTime.format('HH:mm'),
      总工作时长: workEndTime.diff(fastestTime, 'minute') + '分钟',
      预约占用时长: workEndTime.diff(fastestTime, 'minute') - totalIdleTime + '分钟',
      实际空闲时长: totalIdleTime + '分钟'
    });
    if (totalIdleTime > maxIdleTime) {
      maxIdleTime = totalIdleTime;
      selectedResource = resource;
      console.log("[TimeslotUtils] \u66F4\u65B0\u6700\u4F73\u9009\u62E9: ".concat(resource.main_field, " (\u7A7A\u95F2").concat(totalIdleTime, "\u5206\u949F)"));
    }
  }
  console.log("[TimeslotUtils] \u6700\u7EC8\u9009\u62E9\u8D44\u6E90: ".concat(selectedResource.main_field, " (\u6700\u957F\u7A7A\u95F2").concat(maxIdleTime, "\u5206\u949F)"));
  return selectedResource;
}

/**
 * 给定一个时间列表，通过开始和结束时间过滤出符合条件的时间段
 *
 * @export
 * @param {TimeSliceItem[]} times
 * @param {Dayjs} startTime
 * @param {Dayjs} endTime
 * @return {*} 
 */
export function filterConditionTimeSlots(times, startTime, endTime) {
  return times.filter(function (n) {
    return !dayjs(n.start_at).isAfter(dayjs(startTime)) && !dayjs(n.end_at).isBefore(dayjs(endTime));
  });
}