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 buildRuleSetPresenter(t, record, customFields) {
  const title = pickString(record.name, record.display_name, record.displayName, customFields.name, customFields.display_name) ?? record.id ?? t("planner.search.badge.availabilityRuleSet", "Availability rule set");
  const description = snippet(record.description ?? customFields.description);
  const timezone = pickString(record.timezone, customFields.timezone);
  return {
    title: String(title),
    subtitle: formatSubtitle(description, timezone),
    icon: "calendar-check",
    badge: t("planner.search.badge.availabilityRuleSet", "Availability rule set")
  };
}
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: "planner:planner_availability_rule_set",
      enabled: true,
      priority: 6,
      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, "Timezone", record.timezone ?? ctx.customFields.timezone);
        return buildIndexSource(ctx, buildRuleSetPresenter(t, record, ctx.customFields), lines);
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        return buildRuleSetPresenter(t, ctx.record, ctx.customFields);
      },
      resolveUrl: async (ctx) => `/backend/planner/availability-rulesets/${encodeURIComponent(String(ctx.record.id))}`,
      fieldPolicy: {
        searchable: ["name", "description", "timezone"]
      }
    }
  ]
};
var search_default = searchConfig;
const config = searchConfig;
export {
  config,
  search_default as default,
  searchConfig
};
//# sourceMappingURL=search.js.map
