import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
import { CustomerCompanyProfile, CustomerDeal, CustomerEntity, CustomerPersonProfile } from "../data/entities.js";
function formatCurrency(amount, currency) {
  if (!amount) return null;
  const value = Number(amount);
  if (!Number.isFinite(value)) return currency ? `${amount} ${currency}` : amount;
  if (!currency) return value.toLocaleString();
  try {
    return new Intl.NumberFormat(void 0, { style: "currency", currency }).format(value);
  } catch {
    return `${value.toLocaleString()} ${currency}`;
  }
}
function statusColor(status) {
  if (!status) return void 0;
  const value = status.toLowerCase();
  if (value.includes("won") || value.includes("active") || value.includes("qualified")) return "green";
  if (value.includes("lost") || value.includes("rejected") || value.includes("inactive")) return "red";
  if (value.includes("open") || value.includes("new") || value.includes("pending")) return "amber";
  return "blue";
}
async function resolveEm() {
  const { resolve } = await createRequestContainer();
  return resolve("em");
}
async function loadCustomerPersonPreview(entityId, ctx) {
  const { t } = await resolveTranslations();
  const defaultTitle = t("customers.messageObjects.person.title");
  if (!ctx.organizationId) {
    return { title: defaultTitle, subtitle: entityId };
  }
  const em = await resolveEm();
  const entity = await findOneWithDecryption(
    em,
    CustomerEntity,
    {
      id: entityId,
      kind: "person",
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId,
      deletedAt: null
    },
    void 0,
    { tenantId: ctx.tenantId, organizationId: ctx.organizationId }
  );
  if (!entity) {
    return { title: defaultTitle, subtitle: entityId, status: t("customers.messageObjects.notFound"), statusColor: "gray" };
  }
  const profile = await findOneWithDecryption(
    em,
    CustomerPersonProfile,
    {
      entity,
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId
    },
    void 0,
    { tenantId: ctx.tenantId, organizationId: ctx.organizationId }
  );
  const subtitleParts = [entity.primaryEmail, entity.primaryPhone].filter((part) => Boolean(part && part.trim().length > 0));
  const metadata = {};
  const phoneLabel = t("customers.people.detail.highlights.primaryPhone");
  const jobTitleLabel = t("customers.people.detail.fields.jobTitle");
  if (entity.primaryPhone && entity.primaryPhone.trim().length > 0) metadata[phoneLabel] = entity.primaryPhone;
  if (profile?.jobTitle && profile.jobTitle.trim().length > 0) metadata[jobTitleLabel] = profile.jobTitle;
  return {
    title: entity.displayName,
    subtitle: subtitleParts.join(" \u2022 ") || entityId,
    status: entity.status ?? void 0,
    statusColor: statusColor(entity.status),
    metadata: Object.keys(metadata).length > 0 ? metadata : void 0
  };
}
async function loadCustomerCompanyPreview(entityId, ctx) {
  const { t } = await resolveTranslations();
  const defaultTitle = t("customers.messageObjects.company.title");
  if (!ctx.organizationId) {
    return { title: defaultTitle, subtitle: entityId };
  }
  const em = await resolveEm();
  const entity = await findOneWithDecryption(
    em,
    CustomerEntity,
    {
      id: entityId,
      kind: "company",
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId,
      deletedAt: null
    },
    void 0,
    { tenantId: ctx.tenantId, organizationId: ctx.organizationId }
  );
  if (!entity) {
    return { title: defaultTitle, subtitle: entityId, status: t("customers.messageObjects.notFound"), statusColor: "gray" };
  }
  const profile = await findOneWithDecryption(
    em,
    CustomerCompanyProfile,
    {
      entity,
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId
    },
    void 0,
    { tenantId: ctx.tenantId, organizationId: ctx.organizationId }
  );
  const subtitleParts = [entity.primaryEmail, entity.primaryPhone].filter((part) => Boolean(part && part.trim().length > 0));
  const metadata = {};
  const phoneLabel = t("customers.companies.detail.highlights.primaryPhone");
  const industryLabel = t("customers.companies.detail.fields.industry");
  if (entity.primaryPhone && entity.primaryPhone.trim().length > 0) metadata[phoneLabel] = entity.primaryPhone;
  if (profile?.industry && profile.industry.trim().length > 0) metadata[industryLabel] = profile.industry;
  return {
    title: entity.displayName,
    subtitle: subtitleParts.join(" \u2022 ") || entityId,
    status: entity.status ?? void 0,
    statusColor: statusColor(entity.status),
    metadata: Object.keys(metadata).length > 0 ? metadata : void 0
  };
}
async function loadCustomerDealPreview(entityId, ctx) {
  const { t } = await resolveTranslations();
  const defaultTitle = t("customers.messageObjects.deal.title");
  if (!ctx.organizationId) {
    return { title: defaultTitle, subtitle: entityId };
  }
  const em = await resolveEm();
  const deal = await findOneWithDecryption(
    em,
    CustomerDeal,
    {
      id: entityId,
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId,
      deletedAt: null
    },
    void 0,
    { tenantId: ctx.tenantId, organizationId: ctx.organizationId }
  );
  if (!deal) {
    return { title: defaultTitle, subtitle: entityId, status: t("customers.messageObjects.notFound"), statusColor: "gray" };
  }
  const amount = formatCurrency(deal.valueAmount, deal.valueCurrency);
  const probability = typeof deal.probability === "number" ? `${deal.probability}%` : null;
  const subtitle = [amount, probability].filter((part) => Boolean(part && part.length > 0)).join(" \u2022 ");
  const metadata = {};
  const valueLabel = t("customers.deals.detail.fields.value");
  const probabilityLabel = t("customers.deals.detail.fields.probability");
  if (amount) metadata[valueLabel] = amount;
  if (probability) metadata[probabilityLabel] = probability;
  return {
    title: deal.title,
    subtitle: subtitle || entityId,
    status: deal.status ?? void 0,
    statusColor: statusColor(deal.status),
    metadata: Object.keys(metadata).length > 0 ? metadata : void 0
  };
}
export {
  loadCustomerCompanyPreview,
  loadCustomerDealPreview,
  loadCustomerPersonPreview
};
//# sourceMappingURL=messageObjectPreviews.js.map
