import { computeMaxMana } from '../utils';
import { useUIStore } from './uiStore';
import { usePrestigeStore } from './prestigeStore';
import { useManaStore } from './manaStore';
import { useCombatStore } from './combatStore';

export const createResetGame = (set: (state: any) => void, initialState: any) => () => {
  // Clear all persisted state
  if (typeof window !== 'undefined') {
    localStorage.removeItem('mana-loop-ui-storage');
    localStorage.removeItem('mana-loop-prestige-storage');
    localStorage.removeItem('mana-loop-mana-storage');
    localStorage.removeItem('mana-loop-combat-storage');
    localStorage.removeItem('mana-loop-game-storage');
    localStorage.removeItem('mana-loop-crafting-storage');
    localStorage.removeItem('mana-loop-attunement-storage');
  }

  const startFloor = 1;

  useUIStore.getState().resetUI();
  usePrestigeStore.getState().resetPrestige();
  useManaStore.getState().resetMana({}, {}, {}, {});
  useCombatStore.getState().resetCombat(startFloor);

  set({
    ...initialState,
    initialized: true,
  });
};

export const createGatherMana = () => () => {
  const manaState = useManaStore.getState();
  const prestigeState = usePrestigeStore.getState();

  // Base click mana (no skill bonuses)
  const cm = 1;

  const max = computeMaxMana(
    {
      skills: {},
      prestigeUpgrades: prestigeState.prestigeUpgrades,
      skillUpgrades: {},
      skillTiers: {}
    },
    undefined
  );

  useManaStore.getState().gatherMana(cm, max);
};
