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/pisellProResourcePickerModal/utils.ts
var utils_exports = {};
__export(utils_exports, {
  buildTabbarDataSourceFromRecords: () => buildTabbarDataSourceFromRecords,
  defaultGetSearchText: () => defaultGetSearchText,
  getMaxCategoryDepth: () => getMaxCategoryDepth,
  matchesSelection: () => matchesSelection,
  resolveCategoryPath: () => resolveCategoryPath
});
module.exports = __toCommonJS(utils_exports);
var import_materials = require("@pisell/materials");
function normalizeSegment(value) {
  if (value == null) return null;
  const t = String(value).trim();
  return t ? t : null;
}
function resolveCategoryPath(item, config) {
  var _a;
  if (!config) return [null, null, null];
  if (config.getCategoryPath) {
    const raw = config.getCategoryPath(item);
    return [
      normalizeSegment(raw == null ? void 0 : raw[0]),
      normalizeSegment(raw == null ? void 0 : raw[1]),
      normalizeSegment(raw == null ? void 0 : raw[2])
    ];
  }
  if (config.nestedPathField) {
    const nested = (0, import_materials.parseNestedPathSegments)(item[config.nestedPathField], 3);
    return [
      normalizeSegment(nested[0]),
      normalizeSegment(nested[1]),
      normalizeSegment(nested[2])
    ];
  }
  const out = [null, null, null];
  const levels = config.levels ?? [];
  for (let i = 0; i < 3; i++) {
    const field = (_a = levels[i]) == null ? void 0 : _a.field;
    if (field) out[i] = normalizeSegment(item[field]);
  }
  return out;
}
function matchesSelection(item, selection, config) {
  if (!config) return true;
  const path = resolveCategoryPath(item, config);
  for (let i = 0; i < 3; i++) {
    const picked = selection[i];
    if (picked != null && path[i] !== picked) return false;
  }
  return true;
}
function buildTabbarDataSourceFromRecords(items, config) {
  if (!config || items.length === 0) return [];
  const root = [];
  const ensureChild = (list, key, label) => {
    let found = list.find((n) => n.id === key);
    if (!found) {
      found = { id: key, name: label, children: [] };
      list.push(found);
    }
    return found;
  };
  for (const item of items) {
    const path = resolveCategoryPath(item, config);
    let cursor = root;
    for (let i = 0; i < 3; i++) {
      const seg = path[i];
      if (!seg) break;
      const node = ensureChild(cursor, seg, seg);
      if (!node.children) node.children = [];
      cursor = node.children;
    }
  }
  const cleanup = (nodes) => {
    nodes.forEach((n) => {
      if (n.children && n.children.length === 0) {
        delete n.children;
      } else if (n.children) {
        cleanup(n.children);
      }
    });
    nodes.sort(
      (a, b) => String(a.name).localeCompare(String(b.name), "zh-CN")
    );
    return nodes;
  };
  return cleanup(root);
}
function getMaxCategoryDepth(items, config) {
  if (!config || items.length === 0) return 0;
  let max = 0;
  for (const item of items) {
    const path = resolveCategoryPath(item, config);
    for (let i = 2; i >= 0; i--) {
      if (path[i]) {
        if (i + 1 > max) max = i + 1;
        break;
      }
    }
  }
  return max;
}
function defaultGetSearchText(record) {
  const title = typeof record.title === "string" ? record.title : typeof record.name === "string" ? record.name : "";
  const idStr = String(record.id ?? "").trim();
  return `${title} ${idStr}`.toLowerCase();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildTabbarDataSourceFromRecords,
  defaultGetSearchText,
  getMaxCategoryDepth,
  matchesSelection,
  resolveCategoryPath
});
