// ============================================================================
// KaratScan - shared weight-basis display metadata
// ----------------------------------------------------------------------------
// The public API exposes pricing.weight_basis as durable contract codes. Keep
// card/detail copy centralized so new backend values never render as icon-only
// chips when a compact label is missing.
// ============================================================================

(function () {
  const DISPLAY = {
    text: {
      compactLabel: "Stated wt",
      label: "Stated Weight",
      icon: "check",
    },
    clean_total_weight: {
      compactLabel: "Total wt",
      label: "Clean Total Weight",
      icon: "check",
    },
    explicit_gold_weight: {
      compactLabel: "Gold wt",
      label: "Explicit Gold Weight",
      icon: "check",
    },
    explicit_net_metal_weight: {
      compactLabel: "Net metal wt",
      label: "Explicit Net Metal Weight",
      icon: "check",
    },
    inferred_stone_adjusted_weight: {
      compactLabel: "Stone adj.",
      label: "Inferred Stone Adjusted Weight",
      icon: "spark",
    },
    diamond_weight_ignored_total_weight: {
      compactLabel: "Diamond adj.",
      label: "Diamond Weight Ignored Total Weight",
      icon: "spark",
    },
    homogeneous_lot_total_weight: {
      compactLabel: "Lot wt",
      label: "Homogeneous Lot Total Weight",
      icon: "check",
    },
    homogeneous_lot_diamond_adjusted_weight: {
      compactLabel: "Lot adj.",
      label: "Homogeneous Lot Diamond Adjusted Weight",
      icon: "spark",
    },
    image: {
      compactLabel: "Photo est.",
      label: "Photo Estimate",
      icon: "spark",
    },
    image_scale_ai: {
      compactLabel: "Photo est.",
      label: "Image Scale AI",
      icon: "spark",
    },
    image_scale_diamond_adjusted_weight: {
      compactLabel: "Photo adj.",
      label: "Image Scale Diamond Adjusted Weight",
      icon: "spark",
    },
    image_scale_diamond_weight_ignored: {
      compactLabel: "Photo adj.",
      label: "Image Scale Diamond Weight Ignored",
      icon: "spark",
    },
    none: {
      compactLabel: "No weight",
      label: "No Weight",
      icon: "spark",
    },
  };

  const humanizeCode = (value) => {
    if (!value) return "";
    return String(value)
      .replace(/[_-]+/g, " ")
      .replace(/\s+/g, " ")
      .trim()
      .replace(/\b\w/g, (c) => c.toUpperCase());
  };

  const weightBasisDisplay = (value) => {
    const code = value || "none";
    const known = DISPLAY[code];
    const label = known?.label || humanizeCode(code) || "Unknown";
    const compactLabel = known?.compactLabel || label;
    const icon = known?.icon || "spark";
    return {
      code,
      compactLabel,
      label,
      icon,
      title: `Weight basis: ${label}`,
      isKnown: !!known,
    };
  };

  const confidenceText = (item) =>
    item && item.weightConfidence ? ` Confidence: ${item.weightConfidence}.` : "";

  const weightBasisDescription = (item) => {
    const basis = item?.weightBasis || "none";
    const display = weightBasisDisplay(basis);
    if (basis === "text") {
      return `Listing text states the metal weight directly.${confidenceText(item)}`;
    }
    if (basis === "image" || basis === "image_scale_ai") {
      return `No stated weight - metal weight inferred from image-scale analysis of the photos.${confidenceText(item)} Treat as an estimate.`;
    }
    if (basis === "none") {
      return "No weight could be determined from the listing text or photos. Item cannot be priced until a weight is known.";
    }
    return `${display.label} (${basis})`;
  };

  window.WEIGHT_BASIS_DISPLAY = DISPLAY;
  window.WB_LABEL = Object.fromEntries(
    Object.entries(DISPLAY).map(([code, display]) => [code, display.compactLabel]),
  );
  window.weightBasisDisplay = weightBasisDisplay;
  window.weightBasisLabel = (value) => weightBasisDisplay(value).label;
  window.weightBasisDescription = weightBasisDescription;
})();
