var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellReservation/floorMap/reservationHudResourceRootBuckets.ts
var reservationHudResourceRootBuckets_exports = {};
__export(reservationHudResourceRootBuckets_exports, {
  buildResourceRootBuckets: () => buildResourceRootBuckets,
  extractFilterFormRecordIds: () => extractFilterFormRecordIds
});
module.exports = __toCommonJS(reservationHudResourceRootBuckets_exports);
var HUD_RAIL_TREE_ROOT_CAP = 20;
function normalizeRecordId(raw) {
  if (typeof raw === "number" && Number.isFinite(raw)) {
    const n = Math.trunc(raw);
    if (n > 0) return String(n);
    return null;
  }
  if (typeof raw === "string") {
    const t = raw.trim();
    if (!t || t === "0") return null;
    const n = Number(t);
    if (Number.isFinite(n) && n > 0) return String(Math.trunc(n));
    if (/^\d+$/.test(t)) return t;
  }
  return null;
}
function normalizeMainFieldFirstLine(raw) {
  if (raw == null || typeof raw !== "string") return "";
  const first = raw.split(" ## ")[0];
  return (first == null ? void 0 : first.trim()) ?? "";
}
function getParentRecordId(item) {
  const p = item.parent_id ?? item.parentId;
  if (p == null || p === "") return null;
  if (typeof p === "number" && Number.isFinite(p)) {
    const n = Math.trunc(p);
    return n > 0 ? String(n) : null;
  }
  return normalizeRecordId(p);
}
function normalizeFormId(row) {
  const f = row.form_id ?? row.formId;
  if (typeof f === "number" && Number.isFinite(f)) {
    const n = Math.trunc(f);
    return n > 0 ? String(n) : null;
  }
  if (typeof f === "string") {
    const t = f.trim();
    if (!t || t === "0") return null;
    const n = Number(t);
    if (Number.isFinite(n) && n > 0) return String(Math.trunc(n));
    if (/^\d+$/.test(t)) return t;
  }
  return null;
}
function pickFormAggregateLabel(rows) {
  const codes = /* @__PURE__ */ new Set();
  for (const r of rows) {
    const c = typeof r.form_code === "string" ? r.form_code.trim() : "";
    if (c) codes.add(c);
  }
  if (codes.size >= 1) {
    return [...codes].sort((a, b) => a.localeCompare(b))[0] ?? "Resource";
  }
  let shortest = "";
  for (const r of rows) {
    const m = normalizeMainFieldFirstLine(r.main_field);
    if (!m) continue;
    if (!shortest || m.length < shortest.length) shortest = m;
  }
  return shortest || "Other";
}
function buildFormAggregateBuckets(rows) {
  const groups = /* @__PURE__ */ new Map();
  for (const row of rows) {
    const id = normalizeRecordId(row.id);
    if (!id) continue;
    const fk = normalizeFormId(row);
    const gk = fk ?? "__nof__";
    if (!groups.has(gk)) groups.set(gk, []);
    groups.get(gk).push(row);
  }
  const entries = [...groups.entries()].map(([formKey, groupRows]) => {
    let minSort = Infinity;
    for (const r of groupRows) {
      const s = typeof r.sort === "number" && Number.isFinite(r.sort) ? r.sort : Infinity;
      if (s < minSort) minSort = s;
    }
    if (!Number.isFinite(minSort)) minSort = 0;
    const label = pickFormAggregateLabel(groupRows);
    const keySet = /* @__PURE__ */ new Set();
    for (const r of groupRows) {
      const rid = normalizeRecordId(r.id);
      if (rid) keySet.add(rid);
    }
    const bucket = {
      id: `resourceForm:${formKey}`,
      label,
      keys: [...keySet]
    };
    return {
      sortKey: minSort,
      bucket
    };
  });
  entries.sort((a, b) => a.sortKey - b.sortKey);
  return entries.map((e) => e.bucket);
}
function extractFilterFormRecordIds(searchParams) {
  const filter = searchParams.filter;
  const values = typeof (filter == null ? void 0 : filter.values) === "object" && (filter == null ? void 0 : filter.values) !== null && !Array.isArray(filter.values) ? filter.values : void 0;
  const raw = values == null ? void 0 : values.form_record_ids;
  if (!Array.isArray(raw)) return [];
  return raw.map(String).filter(Boolean);
}
function buildTreeResourceRootBuckets(rows) {
  const idSet = /* @__PURE__ */ new Set();
  for (const row of rows) {
    const id = normalizeRecordId(row.id);
    if (id) idSet.add(id);
  }
  const childrenByParent = /* @__PURE__ */ new Map();
  for (const row of rows) {
    const id = normalizeRecordId(row.id);
    if (!id) continue;
    const pid = getParentRecordId(row);
    if (!pid || !idSet.has(pid)) continue;
    if (!childrenByParent.has(pid)) childrenByParent.set(pid, []);
    childrenByParent.get(pid).push(id);
  }
  const roots = [];
  const seen = /* @__PURE__ */ new Set();
  for (const row of rows) {
    const id = normalizeRecordId(row.id);
    if (!id || seen.has(id)) continue;
    const pid = getParentRecordId(row);
    if (!pid || !idSet.has(pid)) {
      roots.push(row);
      seen.add(id);
    }
  }
  roots.sort((a, b) => {
    const sa = typeof a.sort === "number" && Number.isFinite(a.sort) ? a.sort : 0;
    const sb = typeof b.sort === "number" && Number.isFinite(b.sort) ? b.sort : 0;
    return sa - sb;
  });
  function collectSubtreeIds(rootId) {
    const out = [];
    const stack = [rootId];
    while (stack.length) {
      const cur = stack.pop();
      out.push(cur);
      const kids = childrenByParent.get(cur);
      if (!(kids == null ? void 0 : kids.length)) continue;
      for (const k of kids) stack.push(k);
    }
    return out;
  }
  const buckets = [];
  for (const row of roots) {
    const id = normalizeRecordId(row.id);
    if (!id) continue;
    const main = normalizeMainFieldFirstLine(row.main_field);
    const code = typeof row.form_code === "string" && row.form_code.trim() !== "" ? String(row.form_code).trim() : "";
    const label = (main || code || id).trim();
    const keys = collectSubtreeIds(id);
    buckets.push({
      id: `resourceRoot:${id}`,
      label,
      keys
    });
  }
  return buckets;
}
function buildResourceRootBuckets(rawList) {
  if (!Array.isArray(rawList) || rawList.length === 0) return [];
  const rows = rawList.filter(
    (x) => x != null && typeof x === "object" && !Array.isArray(x)
  );
  const treeBuckets = buildTreeResourceRootBuckets(rows);
  if (treeBuckets.length <= HUD_RAIL_TREE_ROOT_CAP) {
    return treeBuckets;
  }
  return buildFormAggregateBuckets(rows);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildResourceRootBuckets,
  extractFilterFormRecordIds
});
