import { useRouter } from 'next/router';

import {
  FORM_SUBMIT_MODE_CREATE,
  FORM_SUBMIT_MODE_DUPLICATE,
  FORM_SUBMIT_MODE_UPDATE,
} from '@/modules/forms/editor/constants';

const useFormSubmitMode = () => {
  const router = useRouter();
  const [entityParam, duplicateParam] = router.query.params;
  const id = parseInt(entityParam, 10);
  const isDuplicate = !!duplicateParam;

  switch (true) {
    case id && !isDuplicate:
      return FORM_SUBMIT_MODE_UPDATE;
    case id && isDuplicate:
      return FORM_SUBMIT_MODE_DUPLICATE;
    default:
      return FORM_SUBMIT_MODE_CREATE;
  }
};

export default useFormSubmitMode;
