/**
 * Replace layout placeholders with sample values for admin previews (AI Helper, etc.).
 */

import type { CanvasElement, PriceScenario } from "@/types";
import { applyCanvasPlaceholders } from "./canvasPlaceholders";

function getWcDecimalSeparator(): string {
  return window.oppbAdmin?.currency?.decimal_separator || ".";
}

function splitPrice(value: number, separator: string): { full: string; main: string; decimal: string } {
  const [main, decimal = "00"] = value.toFixed(2).split(".");
  return {
    full: `${main}${separator}${decimal}`,
    main,
    decimal,
  };
}

/**
 * Sample placeholder values aligned with canvas element preview mocks.
 */
export function getSamplePlaceholderValues(scenario: PriceScenario): Record<string, string> {
  const separator = getWcDecimalSeparator();
  const regular = splitPrice(99.99, separator);
  const sale = splitPrice(79.99, separator);
  const min = splitPrice(19.99, separator);
  const max = splitPrice(99.99, separator);
  const minRegular = splitPrice(29.99, separator);
  const maxRegular = splitPrice(129.99, separator);
  const minSale = splitPrice(19.99, separator);
  const maxSale = splitPrice(99.99, separator);
  const discountAmount = splitPrice(20, separator);

  const common: Record<string, string> = {
    "{productName}": "Sample Product",
    "{productId}": "123",
    "{productID}": "123",
    "{currencySymbol}": "$",
    "{taxLabel}": "incl. VAT",
    "{stockStatus}": "In stock",
  };

  const simple: Record<string, string> = {
    "{regularPrice}": regular.full,
    "{regularPriceMain}": regular.main,
    "{regularPriceDecimal}": regular.decimal,
    "{regularPriceSeparator}": separator,
  };

  const salePrices: Record<string, string> = {
    ...simple,
    "{salePrice}": sale.full,
    "{salePriceMain}": sale.main,
    "{salePriceDecimal}": sale.decimal,
    "{salePriceSeparator}": separator,
    "{discount}": "20",
    "{discountPercentage}": "20",
    "{discountAmount}": `$${discountAmount.full}`,
    "{discountAmountRaw}": discountAmount.full,
    "{savedAmount}": `$${discountAmount.full}`,
  };

  const variable: Record<string, string> = {
    "{minPrice}": min.full,
    "{minPriceMain}": min.main,
    "{minPriceDecimal}": min.decimal,
    "{maxPrice}": max.full,
    "{maxPriceMain}": max.main,
    "{maxPriceDecimal}": max.decimal,
  };

  const variableSale: Record<string, string> = {
    ...variable,
    "{minRegularPrice}": minRegular.full,
    "{minRegularPriceMain}": minRegular.main,
    "{minRegularPriceDecimal}": minRegular.decimal,
    "{maxRegularPrice}": maxRegular.full,
    "{maxRegularPriceMain}": maxRegular.main,
    "{maxRegularPriceDecimal}": maxRegular.decimal,
    "{minSalePrice}": minSale.full,
    "{minSalePriceMain}": minSale.main,
    "{minSalePriceDecimal}": minSale.decimal,
    "{maxSalePrice}": maxSale.full,
    "{maxSalePriceMain}": maxSale.main,
    "{maxSalePriceDecimal}": maxSale.decimal,
    "{discount}": "23",
    "{discountPercentage}": "23",
    "{discountAmount}": `$${discountAmount.full}`,
    "{discountAmountRaw}": discountAmount.full,
    "{savedAmount}": `$${discountAmount.full}`,
  };

  switch (scenario) {
    case "sale":
      return { ...common, ...salePrices };
    case "variable_simple":
      return { ...common, ...variable };
    case "variable_sale":
      return { ...common, ...variableSale };
    case "simple":
    default:
      return { ...common, ...simple };
  }
}

/**
 * Apply canvas + product/price sample values for live HTML preview.
 */
export function applyPreviewPlaceholders(
  html: string,
  elements: CanvasElement[],
  scenario: PriceScenario,
): string {
  let output = applyCanvasPlaceholders(html, elements);
  const samples = getSamplePlaceholderValues(scenario);

  for (const [key, value] of Object.entries(samples)) {
    output = output.split(key).join(value);
  }

  return output;
}
