import { useEffect } from 'react';

import { PCN_GET_SELF_SERVE_SOURCES } from '@/modules/@common/acl/constants';

import { hasPermission } from '@/modules/@common/user/helpers';
import { getUserPermissionsSelector } from '@/modules/@common/user/redux/selectors';
import { setAction } from '@/modules/feeds/Editor/redux/slices';

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

export default function useSetIsSelfServeUser() {
  const dispatch = useAppDispatch();
  const userPermissions = useAppSelector(getUserPermissionsSelector);
  const hasSelfServeSourcePermission = hasPermission(PCN_GET_SELF_SERVE_SOURCES, userPermissions);

  useEffect(() => {
    dispatch(setAction({ isSelfServeUser: hasSelfServeSourcePermission }));
  }, [hasSelfServeSourcePermission]);
}
