import type { Armor5e } from '@/lib/compendium/types';
import type { ArmorMechanics } from '../types';

/**
 * 5e equipment armor → unified mechanics.
 *
 * The source `ac` is a string ("11 + Dex", "14 + Dex (max 2)", "18", "+2" for a
 * shield) and `dexCap` is null (light/unlimited), a number (medium cap), or 0
 * (heavy). We trust `dexCap` and parse the leading number out of `ac`.
 */
export function normalizeArmor5e(raw: Armor5e): ArmorMechanics | null {
  if (!raw.name) return null;
  const type = (raw.type ?? '').toLowerCase();
  const isShield = type === 'shield';
  const acStr = raw.ac ?? '';
  // Shields read "+2"; armor reads "11 + Dex" or "18".
  const baseAc = Number(/-?\d+/.exec(acStr)?.[0] ?? (isShield ? 2 : 10));

  const category: ArmorMechanics['category'] = isShield
    ? 'shield'
    : type === 'light' ? 'light'
    : type === 'medium' ? 'medium'
    : type === 'heavy' ? 'heavy'
    : 'unarmored';

  // Shields don't cap Dex (they're additive); honor the source dexCap otherwise.
  const dexCap = isShield ? null : (raw.dexCap ?? null);

  return {
    name: raw.name,
    category,
    baseAc,
    dexCap,
    stealthDisadvantage: !!raw.stealthPenalty,
  };
}
