import * as React from 'react';

import NotFound from 'pages/404';
import InternalError from 'pages/500';

interface IErrorProps {
  errorCode: number;
}

const Error = ({ errorCode }: IErrorProps) => {
  switch (errorCode) {
    case 200:
    case 404:
      return (<NotFound />);
    default:
      return (<InternalError />);
  }
};

Error.getInitialProps = async ({ res, xhr }: any) => {
  const errorCode = res ? res.statusCode : (xhr ? xhr.status : null);

  if (errorCode >= 500) {
    console.error(`Page failed to render with status ${errorCode}`);
  }

  return { errorCode };
};

export default Error;
