'use client'; import { useState } from 'react'; import { Progress } from '@/components/ui/progress'; import { GameCard } from '@/components/ui/game-card'; import { SectionHeader } from '@/components/ui/section-header'; import { ActionButton } from '@/components/ui/action-button'; import { Scroll, Hammer, Sparkles, Anvil } from 'lucide-react'; import { fmt } from '@/lib/game/stores'; import { EnchantmentDesigner, EnchantmentPreparer, EnchantmentApplier, EquipmentCrafter, } from '@/components/game/crafting'; import { useCombatStore, useCraftingStore } from '@/lib/game/stores'; import { DebugName } from '@/lib/game/debug-context'; import { useGameToast } from '@/components/game/GameToast'; export function CraftingTab() { const showToast = useGameToast(); const currentAction = useCombatStore((s) => s.currentAction); const designProgress = useCraftingStore((s) => s.designProgress); const preparationProgress = useCraftingStore((s) => s.preparationProgress); const applicationProgress = useCraftingStore((s) => s.applicationProgress); const equipmentCraftingProgress = useCraftingStore((s) => s.equipmentCraftingProgress); const pauseApplication = useCraftingStore((s) => s.pauseApplication); const resumeApplication = useCraftingStore((s) => s.resumeApplication); const [activeTab, setActiveTab] = useState<'fabricate' | 'enchant'>('fabricate'); const [enchantStage, setEnchantStage] = useState<'design' | 'prepare' | 'apply'>('design'); // Safe toFixed helper const safeToFixed = (value: number | undefined, decimals: number = 0): string => { if (value === undefined || isNaN(value)) return '0'; return value.toFixed(decimals); }; // Safe percentage calculation const calcPercent = (progress: number, required: number): number => { if (!required || required === 0) return 0; return (progress / required) * 100; }; // Handle enchantment application with toast const handleEnchantmentApplied = () => { showToast('success', 'Enchantment Applied', 'The enchantment has been successfully applied!'); }; // Handle enchantment capacity exceeded const handleCapacityExceeded = (itemName: string, used: number, total: number) => { showToast('error', 'Enchantment Capacity Exceeded', `${itemName} can only hold ${total} enchantments (${used}/${total} used). Remove some enchantments first.`); }; return (
{/* Top Sub-Tabs: Fabricate / Enchant */}
setActiveTab('fabricate')} className={activeTab === 'fabricate' ? 'ring-2 ring-[var(--interactive-primary)]' : ''} > Fabricate setActiveTab('enchant')} className={activeTab === 'enchant' ? 'ring-2 ring-[var(--interactive-primary)]' : ''} > Enchant
{/* Fabricate Content: EquipmentCrafter */} {activeTab === 'fabricate' && ( )} {/* Enchant Content: Design → Prepare → Apply workflow */} {activeTab === 'enchant' && (
{/* Enchant Sub-Navigation (no numbered stepper) */}
setEnchantStage('design')} className={enchantStage === 'design' ? 'ring-2 ring-[var(--interactive-primary)]' : ''} > Design setEnchantStage('prepare')} className={enchantStage === 'prepare' ? 'ring-2 ring-[var(--interactive-primary)]' : ''} > Prepare setEnchantStage('apply')} className={enchantStage === 'apply' ? 'ring-2 ring-[var(--interactive-primary)]' : ''} > Apply
{/* Enchant Stage Content */} {enchantStage === 'design' && ( {}} selectedEffects={[]} setSelectedEffects={() => {}} designName={''} setDesignName={() => {}} selectedDesign={null} setSelectedDesign={() => {}} /> )} {enchantStage === 'prepare' && ( {}} /> )} {enchantStage === 'apply' && ( {}} selectedDesign={null} setSelectedDesign={() => {}} onEnchantmentApplied={handleEnchantmentApplied} onCapacityExceeded={handleCapacityExceeded} /> )}
)} {/* Current Activity Indicator: Crafting */} {currentAction === 'craft' && equipmentCraftingProgress && ( {safeToFixed(calcPercent(equipmentCraftingProgress.progress, equipmentCraftingProgress.required), 0)}% } />
Crafting equipment...
)} {/* Current Activity Indicator: Designing */} {currentAction === 'design' && designProgress && ( useCraftingStore.getState().cancelDesign()}> Cancel } />
Designing: {designProgress.name}
)} {/* Current Activity Indicator: Preparing */} {currentAction === 'prepare' && preparationProgress && ( useCraftingStore.getState().cancelPreparation()}> Cancel } />
Preparing equipment... Mana paid: {fmt(preparationProgress.manaCostPaid)}
)} {/* Current Activity Indicator: Enchanting */} {currentAction === 'enchant' && applicationProgress && ( {applicationProgress.paused ? ( Resume ) : ( <> Pause { useCraftingStore.getState().cancelApplication(); showToast('warning', 'Enchantment Cancelled', 'The enchantment application was cancelled.'); }}>Cancel )}
} />
{applicationProgress.paused ? 'Enchantment paused' : 'Applying enchantment...'} {safeToFixed(calcPercent(applicationProgress.progress, applicationProgress.required), 0)}%
)}
); } CraftingTab.displayName = 'CraftingTab';