'use client'; import { useCombatStore, useCraftingStore, useManaStore, usePrestigeStore } from '@/lib/game/stores'; import { GameCard, ElementBadge } from '@/components/ui'; import { Badge } from '@/components/ui/badge'; import { ELEMENTS, SPELLS_DEF } from '@/lib/game/constants'; import { ENCHANTMENT_EFFECTS } from '@/lib/game/data/enchantment-effects'; import { canAffordSpellCost } from '@/lib/game/utils'; import { formatSpellCost, getSpellCostColor } from '@/lib/game/formatting'; export function SpellsTab() { // Use modular stores directly const spells = useCombatStore((s) => s.spells); const activeSpell = useCombatStore((s) => s.activeSpell); const setSpell = useCombatStore((s) => s.setSpell); const equippedInstances = useCraftingStore((s) => s.equippedInstances); const equipmentInstances = useCraftingStore((s) => s.equipmentInstances); const rawMana = useManaStore((s) => s.rawMana); const elements = useManaStore((s) => s.elements); const signedPacts = usePrestigeStore((s) => s.signedPacts); const unlockedEffects = useCraftingStore((s) => s.unlockedEffects); // Get spells from equipment const equipmentSpellIds: string[] = []; const spellSources: Record = {}; // Guard against undefined stores during initialization if (!equippedInstances || !equipmentInstances) { return (
Loading spell data...
); } for (const instanceId of Object.values(equippedInstances || {})) { if (!instanceId) continue; const instance = equipmentInstances[instanceId]; if (!instance) continue; for (const ench of instance.enchantments) { const effectDef = ENCHANTMENT_EFFECTS?.[ench.effectId]; if (effectDef?.effect.type === 'spell' && effectDef.effect.spellId) { const spellId = effectDef.effect.spellId; if (!equipmentSpellIds.includes(spellId)) { equipmentSpellIds.push(spellId); } if (!spellSources[spellId]) { spellSources[spellId] = []; } spellSources[spellId].push(instance.name); } } } const canCastSpell = (spellId: string): boolean => { const spell = SPELLS_DEF[spellId]; if (!spell || !spell.cost) return false; return canAffordSpellCost(spell.cost, rawMana, elements); }; const hasPactSpells = signedPacts && signedPacts.length > 0; return (
{/* Equipment-Granted Spells */}

Known Spells

Spells are obtained by enchanting equipment with spell effects. Visit the Crafting tab to design and apply enchantments.

{equipmentSpellIds.length > 0 ? (
{equipmentSpellIds.map(id => { const def = SPELLS_DEF[id]; if (!def) return null; const isActive = activeSpell === id; const canCast = canCastSpell(id); const elemDef = def.elem === 'raw' ? null : ELEMENTS[def.elem]; const sources = spellSources[id] || []; return (

{def.name}

Equipment
{def.elem !== 'raw' && ( {elemDef?.name} )} ⚔️ {def.dmg} dmg
Cost: {formatSpellCost(def.cost)}
From: {sources.join(', ')}
{isActive ? ( Active ) : ( )}
); })}
) : (
No spells known yet
Enchant a staff with a spell effect to gain spells
)}
{/* Pact Spells (from guardian defeats) - Empty State */} {!hasPactSpells && (

Pact Spells

Defeat guardians and sign pacts to unlock powerful spells

)} {hasPactSpells && (

Pact Spells

Spells earned through guardian pacts appear here.

)} {/* Spell Reference - show all available spells for enchanting */}

Spell Reference

These spells can be applied to equipment through the enchanting system. Research enchantment effects in the Skills tab to unlock them for designing.

{Object.entries(SPELLS_DEF).map(([id, def]) => { const elemDef = def.elem === 'raw' ? null : ELEMENTS[def.elem]; const isUnlocked = unlockedEffects?.includes(`spell_${id}`); return (

{def.name}

{def.tier > 0 && ( T{def.tier} )} {isUnlocked && ( Unlocked )}
{def.elem !== 'raw' && ( {elemDef?.name} )} ⚔️ {def.dmg} dmg
Cost: {formatSpellCost(def.cost)}
{def.desc && (
{def.desc}
)} {!isUnlocked && (
Research to unlock for enchanting
)}
); })}
); } SpellsTab.displayName = "SpellsTab"; import { DebugName } from '@/lib/game/debug-context';