'use client';

import { useEffect } from 'react';
import { create } from 'zustand';

type Props = {
  year: Year;
  persons: Person[];
  gifts: Gift[];
};

type State = Props & {
  hydrate: (data: Partial<Props>) => void;
};

export const useGlobalStore = create<State>((set) => ({
  year: {} as Year,
  persons: [] as Person[],
  gifts: [] as Gift[],

  hydrate: (data: Partial<Props>) => set(data),
}));

export const GlobalStore = ({ children, year, persons, gifts }: Partial<Props> & { children: React.ReactNode }) => {
  const hydrate = useGlobalStore((s) => s.hydrate);

  useEffect(() => {
    hydrate({ year, persons, gifts });
  }, []);

  return children;
};
