import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
function pickString(...candidates) {
  for (const candidate of candidates) {
    if (typeof candidate !== "string") continue;
    const trimmed = candidate.trim();
    if (trimmed.length > 0) return trimmed;
  }
  return null;
}
function snippet(value, max = 140) {
  if (typeof value !== "string") return void 0;
  const trimmed = value.trim();
  if (!trimmed.length) return void 0;
  if (trimmed.length <= max) return trimmed;
  return `${trimmed.slice(0, max - 3)}...`;
}
function appendLine(lines, label, value) {
  if (value === null || value === void 0) return;
  const text = Array.isArray(value) ? value.map((item) => item === null || item === void 0 ? "" : String(item)).filter(Boolean).join(", ") : typeof value === "object" ? JSON.stringify(value) : String(value);
  if (!text.trim()) return;
  lines.push(`${label}: ${text}`);
}
function appendCustomFieldLines(lines, customFields) {
  for (const [key, value] of Object.entries(customFields)) {
    if (value === null || value === void 0) continue;
    appendLine(lines, key.replace(/^cf:/, ""), value);
  }
}
function formatSubtitle(...parts) {
  const text = parts.map((part) => part === null || part === void 0 ? "" : String(part)).map((part) => part.trim()).filter(Boolean);
  if (text.length === 0) return void 0;
  return text.join(" \xB7 ");
}
function buildResourcePresenter(t, record, customFields) {
  const title = pickString(record.name, record.display_name, record.displayName, customFields.name, customFields.display_name) ?? record.id ?? t("resources.search.badge.resource", "Resource");
  const description = snippet(record.description ?? customFields.description);
  const capacity = record.capacity ?? record.capacity_value ?? record.capacityValue;
  const capacityUnit = pickString(record.capacity_unit_name, record.capacityUnitName, record.capacity_unit_value, record.capacityUnitValue);
  const capacityLabel = capacity != null ? `${capacity}${capacityUnit ? ` ${capacityUnit}` : ""}` : null;
  return {
    title: String(title),
    subtitle: formatSubtitle(description, capacityLabel),
    icon: "box",
    badge: t("resources.search.badge.resource", "Resource")
  };
}
function buildResourceTypePresenter(t, record, customFields) {
  const title = pickString(record.name, record.display_name, record.displayName, customFields.name, customFields.display_name) ?? record.id ?? t("resources.search.badge.resourceType", "Resource type");
  const description = snippet(record.description ?? customFields.description);
  return {
    title: String(title),
    subtitle: formatSubtitle(description),
    icon: "shapes",
    badge: t("resources.search.badge.resourceType", "Resource type")
  };
}
function buildIndexSource(ctx, presenter, lines) {
  appendCustomFieldLines(lines, ctx.customFields);
  if (!lines.length) return null;
  return {
    text: lines,
    presenter,
    checksumSource: { record: ctx.record, customFields: ctx.customFields }
  };
}
const searchConfig = {
  entities: [
    {
      entityId: "resources:resources_resource",
      enabled: true,
      priority: 8,
      buildSource: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        const lines = [];
        appendLine(lines, "Name", record.name ?? record.display_name ?? ctx.customFields.name);
        appendLine(lines, "Description", record.description ?? ctx.customFields.description);
        appendLine(lines, "Capacity", record.capacity ?? record.capacity_value ?? record.capacityValue);
        appendLine(lines, "Capacity unit", record.capacity_unit_name ?? record.capacityUnitName ?? record.capacity_unit_value);
        appendLine(lines, "Resource type", record.resource_type_id ?? record.resourceTypeId);
        appendLine(lines, "Tags", record.tags);
        return buildIndexSource(ctx, buildResourcePresenter(t, record, ctx.customFields), lines);
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        return buildResourcePresenter(t, ctx.record, ctx.customFields);
      },
      resolveUrl: async (ctx) => `/backend/resources/resources/${encodeURIComponent(String(ctx.record.id))}`,
      fieldPolicy: {
        searchable: ["name", "description", "capacity", "capacity_unit_name", "capacity_unit_value", "tags"]
      }
    },
    {
      entityId: "resources:resources_resource_type",
      enabled: true,
      priority: 7,
      buildSource: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        const lines = [];
        appendLine(lines, "Name", record.name ?? record.display_name ?? ctx.customFields.name);
        appendLine(lines, "Description", record.description ?? ctx.customFields.description);
        appendLine(lines, "Icon", record.appearance_icon ?? record.appearanceIcon);
        appendLine(lines, "Color", record.appearance_color ?? record.appearanceColor);
        return buildIndexSource(ctx, buildResourceTypePresenter(t, record, ctx.customFields), lines);
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        return buildResourceTypePresenter(t, ctx.record, ctx.customFields);
      },
      resolveUrl: async (ctx) => `/backend/resources/resource-types/${encodeURIComponent(String(ctx.record.id))}/edit`,
      fieldPolicy: {
        searchable: ["name", "description", "appearance_icon", "appearance_color"]
      }
    }
  ]
};
var search_default = searchConfig;
const config = searchConfig;
export {
  config,
  search_default as default,
  searchConfig
};
//# sourceMappingURL=search.js.map
