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 buildTeamPresenter(t, record, customFields) {
  const title = pickString(record.name, record.display_name, record.displayName, customFields.name, customFields.display_name) ?? record.id ?? t("staff.search.badge.team", "Team");
  const description = snippet(record.description ?? customFields.description);
  return {
    title: String(title),
    subtitle: formatSubtitle(description),
    icon: "users",
    badge: t("staff.search.badge.team", "Team")
  };
}
function buildTeamMemberPresenter(t, record, customFields) {
  const title = pickString(record.display_name, record.displayName, record.name, customFields.display_name, customFields.name) ?? record.id ?? t("staff.search.badge.teamMember", "Employee");
  const description = snippet(record.description ?? customFields.description);
  const tags = Array.isArray(record.tags) ? record.tags.join(", ") : void 0;
  return {
    title: String(title),
    subtitle: formatSubtitle(description, tags),
    icon: "user",
    badge: t("staff.search.badge.teamMember", "Employee")
  };
}
function buildTeamRolePresenter(t, record, customFields) {
  const title = pickString(record.name, record.display_name, record.displayName, customFields.name, customFields.display_name) ?? record.id ?? t("staff.search.badge.teamRole", "Role");
  const description = snippet(record.description ?? customFields.description);
  return {
    title: String(title),
    subtitle: formatSubtitle(description),
    icon: "shield",
    badge: t("staff.search.badge.teamRole", "Role")
  };
}
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: "staff:staff_team",
      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, "Active", record.is_active ?? record.isActive);
        return buildIndexSource(ctx, buildTeamPresenter(t, record, ctx.customFields), lines);
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        return buildTeamPresenter(t, ctx.record, ctx.customFields);
      },
      resolveUrl: async (ctx) => `/backend/staff/teams/${encodeURIComponent(String(ctx.record.id))}/edit`,
      fieldPolicy: {
        searchable: ["name", "description"]
      }
    },
    {
      entityId: "staff:staff_team_member",
      enabled: true,
      priority: 7,
      buildSource: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        const lines = [];
        appendLine(lines, "Name", record.display_name ?? record.displayName ?? ctx.customFields.display_name);
        appendLine(lines, "Description", record.description ?? ctx.customFields.description);
        appendLine(lines, "Tags", record.tags);
        appendLine(lines, "Active", record.is_active ?? record.isActive);
        return buildIndexSource(ctx, buildTeamMemberPresenter(t, record, ctx.customFields), lines);
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        return buildTeamMemberPresenter(t, ctx.record, ctx.customFields);
      },
      resolveUrl: async (ctx) => `/backend/staff/team-members/${encodeURIComponent(String(ctx.record.id))}`,
      fieldPolicy: {
        searchable: ["display_name", "description", "tags"]
      }
    },
    {
      entityId: "staff:staff_team_role",
      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, buildTeamRolePresenter(t, record, ctx.customFields), lines);
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        return buildTeamRolePresenter(t, ctx.record, ctx.customFields);
      },
      resolveUrl: async (ctx) => `/backend/staff/team-roles/${encodeURIComponent(String(ctx.record.id))}/edit`,
      fieldPolicy: {
        searchable: ["name", "description", "appearance_icon", "appearance_color"]
      }
    },
    {
      entityId: "staff:staff_time_project",
      enabled: true,
      priority: 7,
      buildSource: async (ctx) => {
        const { t } = await resolveTranslations();
        const record = ctx.record;
        const lines = [];
        appendLine(lines, "Name", record.name);
        appendLine(lines, "Code", record.code);
        appendLine(lines, "Description", record.description);
        appendLine(lines, "Type", record.project_type ?? record.projectType);
        appendLine(lines, "Cost Center", record.cost_center ?? record.costCenter);
        const presenter = {
          title: String(record.name ?? record.id ?? t("staff.search.badge.timeProject", "Project")),
          subtitle: formatSubtitle(record.code, record.project_type ?? record.projectType),
          icon: "clock",
          badge: t("staff.search.badge.timeProject", "Project")
        };
        return buildIndexSource(ctx, presenter, lines);
      },
      formatResult: async (ctx) => {
        const { t } = await resolveTranslations();
        return {
          title: String(ctx.record.name ?? ctx.record.id ?? ""),
          subtitle: formatSubtitle(ctx.record.code, ctx.record.project_type ?? ctx.record.projectType),
          icon: "clock",
          badge: t("staff.search.badge.timeProject", "Project")
        };
      },
      resolveUrl: async (ctx) => `/backend/staff/timesheets/projects/${encodeURIComponent(String(ctx.record.id))}`,
      fieldPolicy: {
        searchable: ["name", "code", "description", "project_type", "cost_center"]
      }
    }
  ]
};
var search_default = searchConfig;
const config = searchConfig;
export {
  config,
  search_default as default,
  searchConfig
};
//# sourceMappingURL=search.js.map
