/**
 * Visual family for heat exchangers — drives glyph + node badge so DX /
 * flooded / plate / coil / MCHX are distinguishable at a glance.
 *
 * Generic `Condenser` / `Evaporator` pick shell vs coil from the secondary
 * fluid: Air → finned coil, Water/brine → shell & tube. That avoids the
 * inverted look on air-cooled chillers (air condenser drawn as a vessel,
 * water evaporator drawn as a DX fin pack).
 */

export type HxFamilyId =
  | "dx"
  | "flooded"
  | "plate"
  | "coil"
  | "mchx"
  | "shell"
  | "generic";

export interface HxFamily {
  id: HxFamilyId;
  /** Short French badge on the canvas */
  badge: string;
  /** Palette / tooltip line */
  label: string;
  accent: string;
}

export interface HxVisualOpts {
  /** Secondary / HTF fluid id (e.g. "Air", "Water"). */
  secondaryFluid?: string | null;
}

const FAMILIES: Record<HxFamilyId, HxFamily> = {
  dx: {
    id: "dx",
    badge: "DX",
    label: "Détente directe",
    accent: "#1565c0",
  },
  flooded: {
    id: "flooded",
    badge: "Noyé",
    label: "Calandre noyée",
    accent: "#0d7377",
  },
  plate: {
    id: "plate",
    badge: "Plaques",
    label: "Échangeur à plaques",
    accent: "#1a56a8",
  },
  coil: {
    id: "coil",
    badge: "Coil",
    label: "Batterie à ailettes",
    accent: "#c27803",
  },
  mchx: {
    id: "mchx",
    badge: "MCHX",
    label: "Microcanaux",
    accent: "#d97706",
  },
  shell: {
    id: "shell",
    badge: "Tubes",
    label: "Calandre / tubes",
    accent: "#c0392b",
  },
  generic: {
    id: "generic",
    badge: "HX",
    label: "Échangeur",
    accent: "#475569",
  },
};

function isAirSecondary(fluid?: string | null): boolean {
  return String(fluid ?? "").trim().toLowerCase() === "air";
}

/** Returns a visual family for heat-exchanger types; null for non-HX. */
export function hxFamily(type?: string, opts?: HxVisualOpts): HxFamily | null {
  const t = String(type || "");
  if (!t) return null;
  if (t.startsWith("Bphx") || t.includes("Bphx")) return FAMILIES.plate;
  if (t.includes("Flooded")) return FAMILIES.flooded;
  if (t.includes("Mchx")) return FAMILIES.mchx;
  if (
    t.includes("FinCoil") ||
    t.includes("AirCooled") ||
    t === "CondenserCoil" ||
    t === "EvaporatorCoil"
  ) {
    return FAMILIES.coil;
  }
  if (t === "Evaporator") {
    // Air-side DX coil vs water/brine shell-and-tube DX.
    return isAirSecondary(opts?.secondaryFluid) ? FAMILIES.coil : FAMILIES.shell;
  }
  if (t === "Condenser") {
    return isAirSecondary(opts?.secondaryFluid) ? FAMILIES.coil : FAMILIES.shell;
  }
  if (t === "HeatExchanger") return FAMILIES.generic;
  if (t.includes("Condenser") || t.includes("Evaporator")) return FAMILIES.shell;
  return null;
}

/** Glyph key used by ComponentIcon — finer than the old include-based map. */
export function hxGlyphKey(type?: string, opts?: HxVisualOpts): string | null {
  const t = String(type || "");
  if (!t) return null;

  // Generic Condenser / Evaporator: geometry follows the secondary fluid,
  // duty direction follows the role (heat out vs heat in).
  if (t === "Condenser" || t === "Evaporator") {
    const air = isAirSecondary(opts?.secondaryFluid);
    if (t === "Condenser") return air ? "hx_coil" : "hx_shell_cond";
    return air ? "hx_dx" : "hx_shell_evap";
  }

  const f = hxFamily(t, opts);
  if (!f) return null;
  switch (f.id) {
    case "plate":
      return "hx_plate";
    case "flooded":
      return "hx_flooded";
    case "coil":
      return "hx_coil";
    case "mchx":
      return "hx_mchx";
    case "dx":
      return "hx_dx";
    case "shell":
      return t.includes("Evaporator") ? "hx_shell_evap" : "hx_shell_cond";
    default:
      return "hx";
  }
}
