import { create } from 'zustand';
import type { RollResult, RollMode } from '@/lib/dice/notation';
import type { Degree } from '@/lib/dice/check';

export interface TrayRoll {
  /** increments each roll, used as an animation key */
  seq: number;
  label: string;
  result: RollResult;
  dc?: number;
  degree?: Degree;
}

interface RollState {
  last: TrayRoll | null;
  /** advantage/disadvantage toggle applied to d20 rolls everywhere */
  mode: RollMode;
  push: (roll: Omit<TrayRoll, 'seq'>) => void;
  dismiss: () => void;
  setMode: (mode: RollMode) => void;
  /** click the same mode again to turn it off */
  toggleMode: (mode: Exclude<RollMode, 'normal'>) => void;
}

let seq = 0;
export const useRollStore = create<RollState>((set, get) => ({
  last: null,
  mode: 'normal',
  push: (roll) => set({ last: { ...roll, seq: ++seq } }),
  dismiss: () => set({ last: null }),
  setMode: (mode) => set({ mode }),
  toggleMode: (mode) => set({ mode: get().mode === mode ? 'normal' : mode }),
}));
