import { P0A_COLOR_CARD_IDS } from "@dada/template-registry";

export interface ColorCardDefinition {
  cardId: typeof P0A_COLOR_CARD_IDS[number];
  displayName: string;
  mappingStatus: "confirmed_native_mapping" | "stable_web_style_native_mapping_provisional";
  rendererName: "horizontal_line" | "ticket_strip" | "vertical_stack" | "vertical_ticket";
  styleId: "style_01" | "style_02" | "style_08" | "style_16";
}

export type FiveColorPalette = readonly [string, string, string, string, string];

export interface ColorCardRenderPlan extends ColorCardDefinition {
  palette: FiveColorPalette;
}

export const P0A_COLOR_CARD_DEFINITIONS: readonly ColorCardDefinition[] = [
  { cardId: "COLOR001", displayName: "纵向票据", mappingStatus: "confirmed_native_mapping", rendererName: "vertical_ticket", styleId: "style_01" },
  { cardId: "COLOR002", displayName: "纵向色阶", mappingStatus: "stable_web_style_native_mapping_provisional", rendererName: "vertical_stack", styleId: "style_02" },
  { cardId: "COLOR008", displayName: "横向标尺", mappingStatus: "stable_web_style_native_mapping_provisional", rendererName: "horizontal_line", styleId: "style_08" },
  { cardId: "COLOR016", displayName: "横向票条", mappingStatus: "stable_web_style_native_mapping_provisional", rendererName: "ticket_strip", styleId: "style_16" },
] as const;

function paletteSnapshot(palette: readonly string[]): FiveColorPalette {
  if (palette.length !== 5 || palette.some((color) => !/^#[0-9A-Fa-f]{6}$/.test(color))) {
    throw new Error("color-card renderer requires exactly five colors");
  }
  return Object.freeze(palette.map((color) => color.toUpperCase())) as unknown as FiveColorPalette;
}

export function createP0aColorCardRenderPlans(palette: readonly string[]): readonly ColorCardRenderPlan[] {
  const snapshot = paletteSnapshot(palette);
  return P0A_COLOR_CARD_DEFINITIONS.map((definition) => ({ ...definition, palette: snapshot }));
}
