import {
  createPageContext,
  usePageContextReducer,
  usePreloadSettingData,
} from '@hooks';
import {ChildrenFC, PAGE_SIZE_LIST} from '@utils';

type Props = {
  context: ReturnType<typeof createPageContext>;
  initPageSize?: string;
  preloadKey?: string;
};

const PageProvider: ChildrenFC<Props> = function ({
  children,
  context,
  initPageSize = PAGE_SIZE_LIST[0],
  preloadKey,
}) {
  const preloadData = usePreloadSettingData(preloadKey);

  const state = usePageContextReducer({
    pageSize: preloadData?.pageLimit
      ? Number(preloadData.pageLimit)
      : Number(initPageSize),
    page: 1,
  });
  const {Provider} = context;

  return <Provider value={state}>{children}</Provider>;
};

export default PageProvider;
