// ─── Disenchanting Actions ─────────────────────────────────────────────────

import type { GameState, EquipmentInstance } from '../types';

export function disenchantEquipment(
  instanceId: string,
  get: () => GameState,
  set: (fn: (state: GameState) => Partial<GameState>) => void
) {
  const state = get();
  const instance = state.equipmentInstances[instanceId];
  if (!instance || instance.enchantments.length === 0) return;

  const disenchantLevel = 0;
  const recoveryRate = 0.1 + disenchantLevel * 0.2;

  let totalRecovered = 0;
  for (const ench of instance.enchantments) {
    totalRecovered += Math.floor(ench.actualCost * recoveryRate);
  }

  set((state) => ({
    rawMana: state.rawMana + totalRecovered,
    equipmentInstances: {
      ...state.equipmentInstances,
      [instanceId]: {
        ...instance,
        enchantments: [],
        usedCapacity: 0,
      },
    },
    log: [`✨ Disenchanted ${instance.name}, recovered ${totalRecovered} mana.`, ...state.log.slice(0, 49)],
  }));
}
