import { useCallback, useState } from 'react';

export function fisherYates(input) {
  const a = [...input];
  for (let i = a.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
}

function buildShuffle(deck, size) {
  return fisherYates(deck).slice(0, size).map((c) => ({
    ...c,
    reversed: Math.random() < 0.5,
  }));
}

export function useTarotShuffle(deck, size = 16) {
  const [slice, setSlice] = useState(() => buildShuffle(deck, size));
  const reshuffle = useCallback(() => {
    setSlice(buildShuffle(deck, size));
  }, [deck, size]);
  return { slice, reshuffle };
}
