import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { findOneWithDecryption, findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
import { CatalogProduct, CatalogProductCategory, CatalogProductPrice, CatalogProductVariant } from "../data/entities.js";
async function resolveEm() {
  const { resolve } = await createRequestContainer();
  return resolve("em");
}
function formatVariantPrice(amount, currencyCode) {
  if (!amount) return null;
  const value = Number(amount);
  if (!Number.isFinite(value)) return currencyCode ? `${currencyCode.toUpperCase()} ${amount}` : amount;
  if (!currencyCode) return value.toLocaleString();
  try {
    return new Intl.NumberFormat(void 0, {
      style: "currency",
      currency: currencyCode.toUpperCase()
    }).format(value);
  } catch {
    return `${currencyCode.toUpperCase()} ${value.toLocaleString()}`;
  }
}
async function loadCatalogProductPreview(entityId, ctx) {
  const { t } = await resolveTranslations();
  const defaultTitle = t("catalog.messageObjects.product.title");
  if (!ctx.organizationId) {
    return { title: defaultTitle, subtitle: entityId };
  }
  const em = await resolveEm();
  const entity = await findOneWithDecryption(
    em,
    CatalogProduct,
    {
      id: entityId,
      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"
    };
  }
  return {
    title: entity.title,
    subtitle: entity.subtitle ?? void 0
  };
}
async function loadCatalogVariantPreview(entityId, ctx) {
  const { t } = await resolveTranslations();
  const defaultTitle = t("catalog.variants.form.editTitle");
  if (!ctx.organizationId) {
    return { title: defaultTitle, subtitle: entityId };
  }
  const em = await resolveEm();
  const variant = await findOneWithDecryption(
    em,
    CatalogProductVariant,
    {
      id: entityId,
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId,
      deletedAt: null
    },
    void 0,
    { tenantId: ctx.tenantId, organizationId: ctx.organizationId }
  );
  if (!variant) {
    return {
      title: defaultTitle,
      subtitle: entityId,
      status: t("catalog.variants.form.errors.notFound"),
      statusColor: "gray"
    };
  }
  const prices = await findWithDecryption(
    em,
    CatalogProductPrice,
    {
      variant,
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId
    },
    { orderBy: { createdAt: "ASC" }, limit: 10 },
    { tenantId: ctx.tenantId, organizationId: ctx.organizationId }
  );
  const firstPrice = prices.find((entry) => Boolean(entry.unitPriceGross || entry.unitPriceNet));
  const priceLabel = formatVariantPrice(
    firstPrice?.unitPriceGross ?? firstPrice?.unitPriceNet ?? null,
    firstPrice?.currencyCode ?? null
  );
  const metadata = {};
  const skuLabel = t("catalog.variants.form.skuLabel");
  const pricesLabel = t("catalog.variants.form.pricesLabel");
  if (variant.sku && variant.sku.trim().length > 0) metadata[skuLabel] = variant.sku;
  if (priceLabel) metadata[pricesLabel] = priceLabel;
  return {
    title: variant.name ?? "",
    subtitle: variant.sku ?? void 0,
    metadata: Object.keys(metadata).length > 0 ? metadata : void 0
  };
}
async function loadCatalogCategoryPreview(entityId, ctx) {
  const { t } = await resolveTranslations();
  const defaultTitle = t("catalog.messageObjects.category.title");
  if (!ctx.organizationId) {
    return { title: defaultTitle, subtitle: entityId };
  }
  const em = await resolveEm();
  const entity = await findOneWithDecryption(
    em,
    CatalogProductCategory,
    {
      id: entityId,
      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"
    };
  }
  return {
    title: entity.name,
    subtitle: entity.description ?? void 0
  };
}
export {
  loadCatalogCategoryPreview,
  loadCatalogProductPreview,
  loadCatalogVariantPreview
};
//# sourceMappingURL=messageObjectPreviews.js.map
