import { AXES, BRAIN_D, fireMetal, type BrainVec, type MetalShot } from "@/lib/magi/brain9";
import { fireCharlie256, type CubeSeal } from "@/lib/magi/qhp";

export const RUSH9_FIRE_LAW =
  "CHARLIE seals through 256³ map onto Rush's 9D brain and fire. Cirq. QHP. Additive. MAGI does not dispatch 115.";

export type Rush9Fire = {
  seal: CubeSeal;
  vec: BrainVec;
  shot: MetalShot;
  axes: { id: string; label: string; v: number }[];
};

function hex01(hex: string) {
  return (parseInt(hex, 16) >>> 0) / 0xffffffff;
}

export function mapSealsTo9(seal: CubeSeal): BrainVec {
  const v: number[] = seal.seals.slice(0, BRAIN_D).map((s) => hex01(s.hex));
  while (v.length < BRAIN_D) v.push((seal.wire % 256) / 255);
  return v.slice(0, BRAIN_D);
}

export function fireRush9(existing?: CubeSeal | null): Rush9Fire {
  const seal = existing ?? fireCharlie256();
  const vec = mapSealsTo9(seal);
  const shot = fireMetal(vec);
  return {
    seal,
    vec,
    shot,
    axes: AXES.map((a, i) => ({ id: a.id, label: a.label, v: vec[i] ?? 0 })),
  };
}
