import { useMemo, useState } from "react"; import { Check } from "lucide-react"; import { AVATAR_COLORS } from "~/lib/avatar-colors"; import { generateFlagConfig } from "~/lib/flag-generator"; import { FLAG_PATTERNS, type FlagConfig, type FlagPattern } from "~/lib/flag-types"; import { cn } from "~/lib/utils"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { FlagSvg } from "~/components/ui/FlagSvg"; interface FlagBuilderProps { value?: FlagConfig | null; seed: string; onChange: (config: FlagConfig) => void; } const SWATCHES = [ ...AVATAR_COLORS, "#22c55e", "#14b8a6", "#06b6d4", "#0ea5e9", "#6366f1", "#a855f7", "#d946ef", "#ec4899", "#f43f5e", "#dc2626", "#f97316", "#eab308", "#84cc16", "#ffffff", "#d1d5db", "#6b7280", "#111827", "#000000", ]; const COLOR_SLOT_KEYS = ["primary", "secondary", "accent"]; const FALLBACK_COLORS = ["#adf661", "#111827", "#ffffff"]; const HEX_COLOR_RE = /^#[0-9a-fA-F]{6}$/; function normalizeColors(colors: string[]): string[] { return [ colors[0] ?? FALLBACK_COLORS[0], colors[1] ?? FALLBACK_COLORS[1], colors[2] ?? FALLBACK_COLORS[2], ]; } export function FlagBuilder({ value: configValue, seed, onChange }: FlagBuilderProps) { const initial = useMemo(() => configValue ?? generateFlagConfig(seed), [seed, configValue]); const [customInputs, setCustomInputs] = useState(() => normalizeColors(initial.colors)); const [selectedColorIndex, setSelectedColorIndex] = useState(0); const colors = normalizeColors(initial.colors); const config = { ...initial, colors }; const colorSlots = colors.map((color, index) => ({ key: COLOR_SLOT_KEYS[index], label: `Color ${index + 1}`, color, index, })); const activeColorIndex = Math.min(selectedColorIndex, colors.length - 1); const activeColor = colors[activeColorIndex]; const customColor = customInputs[activeColorIndex] ?? activeColor; const isValidCustomColor = HEX_COLOR_RE.test(customColor); function updatePattern(pattern: FlagPattern) { onChange({ ...config, pattern }); } function updateColor(index: number, color: string) { const nextColors = [...colors]; nextColors[index] = color; onChange({ ...config, colors: nextColors }); } return (