export type Rgb = [number, number, number];
export type DeckRgba = [number, number, number, number];

export function rgbToHex(rgb: Rgb): string {
  const toHex = (v: number) => {
    const clamped = Math.max(0, Math.min(255, Math.round(v)));
    return clamped.toString(16).padStart(2, "0");
  };
  return `#${toHex(rgb[0])}${toHex(rgb[1])}${toHex(rgb[2])}`;
}

export function rgba(rgb: Rgb, alpha01: number): string {
  const a = Math.max(0, Math.min(1, alpha01));
  return `rgba(${rgb[0]},${rgb[1]},${rgb[2]},${a})`;
}

export function deckRgba(rgb: Rgb, alpha255: number): DeckRgba {
  const a = Math.max(0, Math.min(255, Math.round(alpha255)));
  return [rgb[0], rgb[1], rgb[2], a];
}

// CN-permit (legacy) ship category colors. Used by both map layers and legend.
export const LEGACY_CODE_COLORS_RGB: Record<string, Rgb> = {
  PT: [30, 64, 175], // #1e40af
  "PT-S": [234, 88, 12], // #ea580c
  GN: [16, 185, 129], // #10b981
  OT: [139, 92, 246], // #8b5cf6
  PS: [239, 68, 68], // #ef4444
  FC: [245, 158, 11], // #f59e0b
  C21: [236, 72, 153], // #ec4899
};

export const LEGACY_CODE_COLORS_HEX: Record<string, string> = Object.fromEntries(
  Object.entries(LEGACY_CODE_COLORS_RGB).map(([k, rgb]) => [k, rgbToHex(rgb)]),
) as Record<string, string>;

// Non-target AIS ships should be visible but muted (speed encoded mainly via brightness).
export const OTHER_AIS_SPEED_RGB = {
  fast: [148, 163, 184] as Rgb, // SOG >= 10
  moving: [100, 116, 139] as Rgb, // 1 <= SOG < 10
  stopped: [71, 85, 105] as Rgb, // SOG < 1
};

export const OTHER_AIS_SPEED_HEX = {
  fast: rgbToHex(OTHER_AIS_SPEED_RGB.fast),
  moving: rgbToHex(OTHER_AIS_SPEED_RGB.moving),
  stopped: rgbToHex(OTHER_AIS_SPEED_RGB.stopped),
};

// Overlay palette: keep a cohesive "warm alert" family, but ensure each overlay type is distinguishable.
export const OVERLAY_RGB = {
  pairNormal: [59, 130, 246] as Rgb, // blue-500
  pairWarn: [251, 113, 133] as Rgb, // rose-400 (쌍 이격 경고)
  fcTransfer: [249, 115, 22] as Rgb, // orange-500 (환적 연결)
  fleetRange: [250, 204, 21] as Rgb, // yellow-400 (선단 범위)
  suspicious: [239, 68, 68] as Rgb, // red-500
};

