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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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';
import { buildTimeSlotGrid, generateTimeLabels } from "./timeSlot";
export function buildDateRangeSummary(params) {
  var startDate = params.startDate,
    endDate = params.endDate,
    config = params.config,
    rawResources = params.rawResources,
    resourceProductMap = params.resourceProductMap,
    quotationModule = params.quotationModule,
    resolveConfig = params.resolveConfig;
  var result = [];
  var productIds = quotationModule ? _toConsumableArray(new Set(_toConsumableArray(resourceProductMap.values()).flat().map(function (m) {
    return m.productId;
  }))) : [];
  var cursor = dayjs(startDate);
  var end = dayjs(endDate);
  var _loop = function _loop() {
    var date = cursor.format('YYYY-MM-DD');
    var effectiveConfig = (resolveConfig === null || resolveConfig === void 0 ? void 0 : resolveConfig(date)) || config;
    var quotationPriceMap;
    if (quotationModule && productIds.length) {
      var timeLabels = generateTimeLabels(effectiveConfig);
      var timePoints = timeLabels.map(function (label) {
        return "".concat(date, " ").concat(label);
      });
      quotationPriceMap = quotationModule.buildProductPriceMap({
        productIds: productIds,
        timePoints: timePoints
      });
    }
    var grid = buildTimeSlotGrid({
      date: date,
      config: effectiveConfig,
      rawResources: rawResources,
      resourceProductMap: resourceProductMap,
      quotationPriceMap: quotationPriceMap
    });
    var totalSlots = 0;
    var availableSlots = 0;
    var lowestPrice = null;
    var _iterator = _createForOfIteratorHelper(grid.resources),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var resource = _step.value;
        var _iterator2 = _createForOfIteratorHelper(resource.slots),
          _step2;
        try {
          for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
            var slot = _step2.value;
            if (slot.status === 'past' || slot.status === 'unavailable') continue;
            totalSlots++;
            if (slot.status === 'available' || slot.status === 'partially_occupied') {
              availableSlots++;
              if (slot.price !== null) {
                var priceNum = Number(slot.price);
                if (Number.isFinite(priceNum) && (lowestPrice === null || priceNum < lowestPrice)) {
                  lowestPrice = priceNum;
                }
              }
            }
          }
        } catch (err) {
          _iterator2.e(err);
        } finally {
          _iterator2.f();
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    var status;
    if (totalSlots === 0) {
      status = 'unavailable';
    } else if (availableSlots === 0) {
      status = 'sold_out';
    } else if ((totalSlots - availableSlots) / totalSlots >= effectiveConfig.fewLeftThreshold) {
      status = 'few_left';
    } else {
      status = 'available';
    }
    result.push({
      date: date,
      status: status,
      startingPrice: lowestPrice !== null ? lowestPrice.toFixed(2) : null,
      totalSlots: totalSlots,
      availableSlots: availableSlots
    });
    cursor = cursor.add(1, 'day');
  };
  while (!cursor.isAfter(end)) {
    _loop();
  }
  return result;
}