import { AxiosError } from 'axios';

import { ResponseCode } from 'src/types/services';

type ErrorCodeResponse = {
  code?: string;
};

export const isPostNotFoundError = (error: AxiosError<ErrorCodeResponse>) => {
  const requestPath = (error.config?.url || '').split('?')[0];

  return (
    error.response?.status === 404 &&
    error.response?.data?.code === ResponseCode.POST_NOT_FOUND &&
    /^\/?posts\/\d+$/.test(requestPath)
  );
};

export const isGroupPostNotFoundError = (
  error: AxiosError<ErrorCodeResponse>,
) => {
  const requestPath = (error.config?.url || '').split('?')[0];

  return (
    error.response?.status === 404 &&
    error.response?.data?.code === ResponseCode.GROUP_POST_NOT_FOUND &&
    /^\/?groups\/\d+\/posts\/\d+$/.test(requestPath)
  );
};
