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

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

export function startPreparing(
  equipmentInstanceId: string,
  rawMana: number,
  get: () => CraftingState,
  set: (partial: Partial<CraftingState>) => 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 (rawMana < costs.manaTotal) return false;

  set({
    preparationProgress: CraftingPrep.initializePreparationProgress(
      equipmentInstanceId,
      instance.totalCapacity
    ),
  });

  return true;
}

export function cancelPreparation(
  set: (partial: Partial<CraftingState>) => void
) {
  set({
    preparationProgress: null,
  });
}
