import { create } from 'zustand';

interface ScrollState {
  homeScrollPosition: number;
  setHomeScrollPosition: (position: number) => void;
  restoreHomeScrollPosition: () => void;
}

export const useScrollStore = create<ScrollState>((set, get) => ({
  homeScrollPosition: 0,
  setHomeScrollPosition: (position) => set({ homeScrollPosition: position }),
  restoreHomeScrollPosition: () => {
    const { homeScrollPosition } = get();
    const tryScroll = () => {
      if (document.body.scrollHeight >= homeScrollPosition + window.innerHeight) {
        window.scrollTo({
          top: homeScrollPosition,
          behavior: 'instant'
        });
      } else {
        requestAnimationFrame(tryScroll);
      }
    };
    requestAnimationFrame(tryScroll);
  }
}));