import { jsx, jsxs } from "react/jsx-runtime";
import { withDataTableNamespaces } from "@open-mercato/ui/backend/DataTable";
function mapOfferRow(item) {
  const product = item.product && typeof item.product === "object" ? item.product : null;
  const prices = Array.isArray(item.prices) ? item.prices : [];
  const productDefaultPrices = Array.isArray(item.productDefaultPrices) ? item.productDefaultPrices : Array.isArray(item.product_default_prices) ? item.product_default_prices : [];
  const productChannelPriceSource = item.productChannelPrice && typeof item.productChannelPrice === "object" ? item.productChannelPrice : item.product_channel_price && typeof item.product_channel_price === "object" ? item.product_channel_price : null;
  return withDataTableNamespaces({
    id: typeof item.id === "string" ? item.id : "",
    channelId: typeof item.channelId === "string" ? item.channelId : typeof item.channel_id === "string" ? item.channel_id : null,
    title: typeof item.title === "string" && item.title.length ? item.title : "Untitled offer",
    description: typeof item.description === "string" ? item.description : null,
    productId: typeof item.productId === "string" ? item.productId : typeof item.product_id === "string" ? item.product_id : null,
    productTitle: typeof product?.title === "string" ? product.title : null,
    productSku: typeof product?.sku === "string" ? product.sku : null,
    productMediaUrl: typeof item.defaultMediaUrl === "string" ? item.defaultMediaUrl : typeof item.default_media_url === "string" ? item.default_media_url : typeof product?.defaultMediaUrl === "string" ? product.defaultMediaUrl : typeof product?.default_media_url === "string" ? product.default_media_url : null,
    prices: prices.map(mapPriceRow),
    productDefaultPrices: productDefaultPrices.map(mapPriceRow),
    productChannelPrice: mapPriceSummary(productChannelPriceSource),
    isActive: item.isActive === true || item.is_active === true,
    updatedAt: typeof item.updatedAt === "string" ? item.updatedAt : typeof item.updated_at === "string" ? item.updated_at : null
  }, item);
}
function renderOfferPriceSummary(row, t) {
  if (!row.prices.length) {
    if (row.productDefaultPrices.length) {
      return /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: row.productDefaultPrices.map((price) => renderPriceBadge(price, t, true)) });
    }
    if (row.productChannelPrice) {
      return /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground", children: t("sales.channels.offers.table.channelPrice", "Original product price {{price}}", {
        price: formatPriceValue(row.productChannelPrice)
      }) });
    }
    return /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground", children: t("sales.channels.offers.table.noOverrides", "No overrides") });
  }
  return /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: row.prices.map((price) => renderPriceBadge(price, t)) });
}
function mapPriceSummary(source) {
  if (!source) return null;
  return mapPriceRow(source);
}
function formatPriceValue(price) {
  if (!price) return "\u2014";
  const amount = price.displayMode === "including-tax" ? price.unitPriceGross ?? price.unitPriceNet : price.unitPriceNet ?? price.unitPriceGross;
  if (amount === null || amount === void 0) return price.currencyCode ?? "\u2014";
  return `${price.currencyCode ?? ""} ${String(amount)}`;
}
function mapPriceRow(source) {
  return {
    id: readString(source.id) ?? void 0,
    priceKindId: readString(source.priceKindId ?? source.price_kind_id),
    priceKindCode: readString(source.priceKindCode ?? source.price_kind_code),
    priceKindTitle: readString(source.priceKindTitle ?? source.price_kind_title),
    currencyCode: readString(source.currencyCode ?? source.currency_code),
    unitPriceNet: readPriceValue(source.unitPriceNet ?? source.unit_price_net),
    unitPriceGross: readPriceValue(source.unitPriceGross ?? source.unit_price_gross),
    displayMode: readString(source.displayMode ?? source.display_mode)
  };
}
function readPriceValue(value) {
  if (typeof value === "number" || typeof value === "string") return value;
  return null;
}
function readString(value) {
  if (typeof value === "string") return value;
  if (typeof value === "number") return value.toString();
  return null;
}
function renderPriceBadge(price, t, muted) {
  const label = price.priceKindTitle || price.priceKindCode || t("sales.channels.offers.table.price", "Price");
  const numeric = price.displayMode === "including-tax" ? price.unitPriceGross ?? price.unitPriceNet : price.unitPriceNet ?? price.unitPriceGross;
  const amount = numeric === null || numeric === void 0 ? "\u2014" : String(numeric);
  const className = ["rounded border px-2 py-1 text-xs", muted ? "bg-muted" : null].filter(Boolean).join(" ");
  return /* @__PURE__ */ jsxs("div", { className, children: [
    /* @__PURE__ */ jsx("div", { className: "font-medium", children: label }),
    /* @__PURE__ */ jsxs("div", { className: "text-muted-foreground", children: [
      price.currencyCode ?? "",
      " ",
      amount
    ] })
  ] }, `${price.id ?? "price"}-${label}`);
}
export {
  mapOfferRow,
  renderOfferPriceSummary
};
//# sourceMappingURL=offerTableUtils.js.map
