import { useEffect } from 'react';

import { FeedType } from '@/modules/feeds/types';

import { defaultStoreValuesDependsOnFeedType, setAction, StateType } from '../redux/slices';

import { useAppDispatch } from '@/modules/@common/store/hooks';

export default function useSetStoreDefaultValues(type: FeedType): void {
  const dispatch = useAppDispatch();

  useEffect(() => {
    if (!type) return;

    const defaults = Object.fromEntries(
      (Object.keys(defaultStoreValuesDependsOnFeedType) as Array<keyof StateType>)
        .map((key) => [key, defaultStoreValuesDependsOnFeedType[key]?.[type]] as const)
        .filter(([, value]) => value !== undefined),
    ) as Partial<StateType>;

    dispatch(setAction(defaults));
  }, [type]);
}
