import { useSelector } from 'react-redux';

import { PCN_POST_FORMS_EDITOR, PCN_PUT_FORMS_EDITOR } from '@/modules/@common/acl/constants';
import {
  FORM_SUBMIT_MODE_CREATE,
  FORM_SUBMIT_MODE_DUPLICATE,
  FORM_SUBMIT_MODE_UPDATE,
  STATUS_ARCHIVED,
} from '@/modules/forms/editor/constants';

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

import useFormSubmitMode from './useFormSubmitMode';

function useGetReadOnlyStatus(status) {
  const formSubmitMode = useFormSubmitMode();
  const userPermissions = useSelector(getUserPermissionsSelector);

  if (status === STATUS_ARCHIVED) {
    return true;
  }

  if (formSubmitMode === FORM_SUBMIT_MODE_UPDATE) {
    return !hasPermission(PCN_PUT_FORMS_EDITOR, userPermissions);
  }

  if ([FORM_SUBMIT_MODE_CREATE, FORM_SUBMIT_MODE_DUPLICATE].includes(formSubmitMode)) {
    return !hasPermission(PCN_POST_FORMS_EDITOR, userPermissions);
  }

  return false;
}

export default useGetReadOnlyStatus;
