// ─── Enchantment Preparation Actions ────────────────────────────────────────

import type { GameState } from '../types';
import * as CraftingPrep from '../crafting-prep';

export function startPreparing(
  equipmentInstanceId: string,
  get: () => GameState,
  set: (fn: (state: GameState) => Partial<GameState>) => void
): boolean {
  const state = get();
  const instance = state.equipmentInstances[equipmentInstanceId];

  const validation = CraftingPrep.canPrepareEquipment(
    instance,
    instance?.tags || []
  );
  if (!validation.canPrepare) return false;

  if (!instance) return false;

  const costs = CraftingPrep.calculatePreparationCosts(instance.totalCapacity);

  if (state.rawMana < costs.manaTotal) return false;

  set(() => ({
    currentAction: 'prepare' as const,
    preparationProgress: CraftingPrep.initializePreparationProgress(
      equipmentInstanceId,
      instance.totalCapacity
    ),
  }));

  return true;
}

export function cancelPreparation(
  set: (fn: (state: GameState) => Partial<GameState>) => void
) {
  set(() => ({
    currentAction: 'meditate' as const,
    preparationProgress: null,
  }));
}
