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 _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 _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; }
/**
 * 从商品列表里抽取所有 resource_id：
 * - 商品绑定的 default_resource / optional_resource
 * - 以及这些资源上声明的 combined_resource.resource_ids（若可从 product_resource 读取）
 *   用于一次性拉到子资源数据，判定组合资源的可用性。
 */
export function extractResourceIds(products) {
  var ids = new Set();
  var _iterator = _createForOfIteratorHelper(products),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var _product$product_reso;
      var product = _step.value;
      var resources = (_product$product_reso = product.product_resource) === null || _product$product_reso === void 0 ? void 0 : _product$product_reso.resources;
      if (!Array.isArray(resources)) continue;
      var _iterator2 = _createForOfIteratorHelper(resources),
        _step2;
      try {
        for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
          var resource = _step2.value;
          if (resource.status !== 1) continue;
          var defaultRes = resource.default_resource || [];
          var optionalRes = resource.optional_resource || [];
          for (var _i = 0, _arr = [].concat(_toConsumableArray(defaultRes), _toConsumableArray(optionalRes)); _i < _arr.length; _i++) {
            var id = _arr[_i];
            if (typeof id === 'number' && id > 0) ids.add(id);
          }
          var combined = resource.combined_resource;
          if (combined && combined.status === 1 && Array.isArray(combined.resource_ids)) {
            var _iterator3 = _createForOfIteratorHelper(combined.resource_ids),
              _step3;
            try {
              for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
                var _id = _step3.value;
                if (typeof _id === 'number' && _id > 0) ids.add(_id);
              }
            } catch (err) {
              _iterator3.e(err);
            } finally {
              _iterator3.f();
            }
          }
        }
      } catch (err) {
        _iterator2.e(err);
      } finally {
        _iterator2.f();
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return Array.from(ids);
}

/**
 * 每个 resourceId 可以被多个商品共享（同一场地关联多种运动商品）。
 * Map 的 value 改为 ResourceProductMapping[]，按商品出现顺序保留。
 */
export function buildResourceProductMap(products) {
  var map = new Map();
  var _iterator4 = _createForOfIteratorHelper(products),
    _step4;
  try {
    for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
      var _product$product_reso2;
      var product = _step4.value;
      var resources = (_product$product_reso2 = product.product_resource) === null || _product$product_reso2 === void 0 ? void 0 : _product$product_reso2.resources;
      if (!Array.isArray(resources)) continue;
      var _iterator5 = _createForOfIteratorHelper(resources),
        _step5;
      try {
        for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
          var resource = _step5.value;
          if (resource.status !== 1) continue;
          var allResourceIds = [].concat(_toConsumableArray(resource.default_resource || []), _toConsumableArray(resource.optional_resource || []));
          var _iterator6 = _createForOfIteratorHelper(allResourceIds),
            _step6;
          try {
            var _loop = function _loop() {
              var resId = _step6.value;
              var existing = map.get(resId);
              var mapping = {
                productId: product.id,
                productTitle: product.title,
                resourceName: resource.title,
                formId: resource.id,
                price: product.price || '0.00',
                resourceType: resource.type || 'single'
              };
              if (!existing) {
                map.set(resId, [mapping]);
              } else if (!existing.some(function (m) {
                return m.productId === mapping.productId;
              })) {
                existing.push(mapping);
              }
            };
            for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
              _loop();
            }
          } catch (err) {
            _iterator6.e(err);
          } finally {
            _iterator6.f();
          }
        }
      } catch (err) {
        _iterator5.e(err);
      } finally {
        _iterator5.f();
      }
    }
  } catch (err) {
    _iterator4.e(err);
  } finally {
    _iterator4.f();
  }
  return map;
}