import { useEffect } from 'react';
import { dispatchLoadingEvent } from '@assets/filters/common/events/events';

export function useCatalogLoading(isQueryLoading: boolean, newPage: number) {
   useEffect(() => {
      const urlParams = new URLSearchParams(window.location.search);
      const currentPage = urlParams.get('page');
      const pageChanged = newPage !== parseInt(`${currentPage ?? 1}`, 10);

      if (isQueryLoading) {
         dispatchLoadingEvent(!pageChanged);
      }
   }, [isQueryLoading, newPage]);
}
