/**
 * LANE-RENDER — world palette, per the style guide (docs/FKTRY_LORE.md §8).
 *
 * Rule the guide gives us: the base world is desaturated industrial neutrals; ALL
 * saturation belongs to media/artifacts. So machine BODIES live here as grimy
 * neutrals, and machine ACCENTS are derived from data (the colour of what the
 * machine makes) in registry.ts — colour = value = literally the resource.
 */
import * as THREE from 'three';
import type { MachineKind } from '../contracts';

const hsl = { h: 0, s: 0, l: 0 };

/**
 * Perceptual (sRGB) lightness of a data colour. Reasoned in sRGB rather than the
 * linear working space so the thresholds below mean what they look like.
 */
export function lightnessOf(hex: string): number {
  new THREE.Color(hex).getHSL(hsl, THREE.SRGBColorSpace);
  return hsl.l;
}

/**
 * Data colours are authored for the codex, not for legibility on a near-black ground:
 * MDAT ORE is #1a1a22, which renders as an invisible smudge. Lift very dark colours to
 * a floor, keeping hue and saturation, so every item still reads as itself on a belt.
 */
export function readable(hex: string, minL = 0.34): THREE.Color {
  const c = new THREE.Color(hex);
  c.getHSL(hsl, THREE.SRGBColorSpace);
  if (hsl.l < minL) c.setHSL(hsl.h, hsl.s, minL, THREE.SRGBColorSpace);
  return c;
}

/** Below this a derived accent is too dark to be "the impossible element" (§8). */
export const ACCENT_MIN_L = 0.3;

export const WORLD = {
  bg: 0x0a0812,
  crust: 0x0c0a15,
  gridLine: 0x181425,
  gridMajor: 0x241d38,
} as const;

/** Grimy industrial bodies. Desaturated on purpose — the accent does the talking. */
export const BODY_BY_KIND: Record<MachineKind, number> = {
  extractor: 0x6e6134, // grimy yellow chassis (codex §4 seam extractor)
  crafter: 0x3b3944,
  belt: 0x2a2833,
  splitter: 0x35333f,
  power: 0x45434f,
  buffer: 0x3a3f48,
  shipper: 0x4a4652,
  lab: 0x3d3a4e, // research bench chassis (v4 kind; placeholder until art-directed)
};

/** Fallback accent when a machine runs no recipes (belts, power, shippers). */
export const ACCENT_BY_KIND: Record<MachineKind, number> = {
  extractor: 0x9fe8ff, // CRT monitor head
  crafter: 0x3fffe0,
  belt: 0x4a5568,
  splitter: 0x7fb0ff,
  power: 0xffb03f, // serene LED face
  buffer: 0x7fffd4,
  shipper: 0x3fd4ff, // uplink to THE SCREEN
  lab: 0xb0a0ff, // research glow (v4 kind; placeholder)
};

/** Body height in world units. Silhouette differentiation on a flat iso grid. */
export const HEIGHT_BY_KIND: Record<MachineKind, number> = {
  extractor: 1.15,
  crafter: 0.8,
  belt: 0.14,
  splitter: 0.45,
  power: 0.95,
  buffer: 0.7,
  shipper: 1.3,
  lab: 1.1, // v4 kind; placeholder
};

export const JAM_AMBER = 0xffa726;
