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 = typeof value === "object" ? JSON.stringify(value) : String(value);
  if (!text.trim()) return;
  lines.push(`${label}: ${text}`);
}
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 buildIndexSource(ctx, presenter, lines) {
  if (!lines.length) return null;
  return {
    text: lines,
    presenter,
    checksumSource: { record: ctx.record, customFields: ctx.customFields }
  };
}
const searchConfig = {
  entities: [
    {
      entityId: "customer_accounts:customer_user",
      enabled: true,
      priority: 6,
      buildSource: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        const lines = [];
        appendLine(lines, "Name", record.display_name ?? record.displayName);
        appendLine(lines, "Email", record.email);
        return buildIndexSource(
          ctx,
          {
            title: pickString(record.display_name, record.displayName) ?? String(record.id),
            subtitle: formatSubtitle(record.email),
            icon: "user",
            badge: t("customer_accounts.search.badge.customerUser", "Customer User")
          },
          lines
        );
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        return {
          title: pickString(record.display_name, record.displayName) ?? String(record.id),
          subtitle: formatSubtitle(record.email),
          icon: "user",
          badge: t("customer_accounts.search.badge.customerUser", "Customer User")
        };
      },
      resolveUrl: async (ctx) => `/backend/customer-accounts/users/${encodeURIComponent(String(ctx.record.id))}`,
      fieldPolicy: {
        searchable: ["display_name", "email"],
        excluded: ["password_hash", "email_hash"]
      }
    },
    {
      entityId: "customer_accounts:customer_role",
      enabled: true,
      priority: 6,
      buildSource: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        const lines = [];
        appendLine(lines, "Name", record.name);
        appendLine(lines, "Description", record.description);
        return buildIndexSource(
          ctx,
          {
            title: pickString(record.name) ?? String(record.id),
            subtitle: formatSubtitle(snippet(record.description)),
            icon: "shield",
            badge: t("customer_accounts.search.badge.customerRole", "Customer Role")
          },
          lines
        );
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        return {
          title: pickString(record.name) ?? String(record.id),
          subtitle: formatSubtitle(snippet(record.description)),
          icon: "shield",
          badge: t("customer_accounts.search.badge.customerRole", "Customer Role")
        };
      },
      resolveUrl: async (ctx) => `/backend/customer-accounts/roles/${encodeURIComponent(String(ctx.record.id))}/edit`,
      fieldPolicy: {
        searchable: ["name", "description"]
      }
    }
  ]
};
var search_default = searchConfig;
const config = searchConfig;
export {
  config,
  search_default as default,
  searchConfig
};
//# sourceMappingURL=search.js.map
