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 _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; }
/**
 * Holder 选项（id → label），与 booking `formatHolders` 规则对齐。
 */

/**
 * 从动态表单字段中提取展示名（main_field 为空时兜底）。
 */
function pickLabelFromRawItem(item) {
  var _ref, _ref2, _ref3, _item$main_field;
  var direct = (_ref = (_ref2 = (_ref3 = (_item$main_field = item === null || item === void 0 ? void 0 : item.main_field) !== null && _item$main_field !== void 0 ? _item$main_field : item === null || item === void 0 ? void 0 : item.label) !== null && _ref3 !== void 0 ? _ref3 : item === null || item === void 0 ? void 0 : item.name) !== null && _ref2 !== void 0 ? _ref2 : item === null || item === void 0 ? void 0 : item.search_field) !== null && _ref !== void 0 ? _ref : '';
  if (String(direct).trim()) return String(direct).trim();
  var skipKeys = new Set(['form_record_id', 'form_id', 'id', 'value', 'status', 'expired_at', 'created_at', 'relation_record', 'search_field', 'main_field', 'avatar', 'subtitle', 'second_field', 'sub_field', 'form_to_form_value']);
  for (var _i = 0, _Object$entries = Object.entries(item || {}); _i < _Object$entries.length; _i++) {
    var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
      key = _Object$entries$_i[0],
      val = _Object$entries$_i[1];
    if (skipKeys.has(key)) continue;
    if (typeof val === 'string' && val.trim()) return val.trim();
  }
  return '';
}

/**
 * 将 getPetsData 原始列表转为 ProductCard / LineItem 可用的 holder 选项。
 *
 * @example
 * formatHolderOptions([{ form_record_id: 12, main_field: 'Max' }]);
 * // => [{ id: 12, label: 'Max' }]
 */
export function formatHolderOptions(rawHolders) {
  if (!Array.isArray(rawHolders) || rawHolders.length === 0) return [];
  return rawHolders.map(function (item) {
    var _ref4, _item$form_record_id;
    var id = (_ref4 = (_item$form_record_id = item === null || item === void 0 ? void 0 : item.form_record_id) !== null && _item$form_record_id !== void 0 ? _item$form_record_id : item === null || item === void 0 ? void 0 : item.id) !== null && _ref4 !== void 0 ? _ref4 : item === null || item === void 0 ? void 0 : item.value;
    var label = pickLabelFromRawItem(item);
    if (id === undefined || id === null || id === '') return null;
    return {
      id: id,
      label: String(label)
    };
  }).filter(function (item) {
    return item != null;
  });
}

/**
 * 按 id 合并 Holder 选项；incoming 同 id 覆盖 existing。
 *
 * @example
 * mergeHolderOptions([{ id: 1, label: 'A' }], [{ id: 2, label: 'B' }]);
 * // => [{ id: 1, label: 'A' }, { id: 2, label: 'B' }]
 */
export function mergeHolderOptions(existing, incoming) {
  var map = new Map();
  var _iterator = _createForOfIteratorHelper(existing),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var option = _step.value;
      map.set(String(option.id), option);
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  var _iterator2 = _createForOfIteratorHelper(incoming),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var _option = _step2.value;
      map.set(String(_option.id), _option);
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  return Array.from(map.values());
}

/**
 * 归一化 holder_id（单个或数组）。
 *
 * @example
 * normalizeHolderIds([1, 2]); // => [1, 2]
 * normalizeHolderIds(3);      // => [3]
 */
export function normalizeHolderIds(value) {
  if (value === undefined || value === null || value === '') return [];
  return Array.isArray(value) ? value : [value];
}

/**
 * 按 holderOptions 反查展示名；找不到时回退为 id 字符串。
 *
 * @example
 * resolveHolderLabels(12, [{ id: 12, label: 'Max' }]); // => ['Max']
 */
export function resolveHolderLabels(holderIds, options) {
  return normalizeHolderIds(holderIds).map(function (id) {
    var matched = options.find(function (option) {
      return String(option.id) === String(id);
    });
    return (matched === null || matched === void 0 ? void 0 : matched.label) || String(id);
  });
}