const fs = require('fs');

const template = `
'use client';

import { useSentryUncaughtErrors } from '@akinon/next/hooks';
import PzErrorPage from '@akinon/next/views/error-page';

export default function ErrorPage({
  error,
  reset
}: {
  error: Error & { digest?: string; isServerError?: boolean };
  reset: () => void;
}) {
  // DO NOT REMOVE THIS LINE TO REPORT UNCAUGHT ERRORS TO SENTRY
  useSentryUncaughtErrors(error);

  return <PzErrorPage error={error} reset={reset} />;
}

`;

const transform = (fileInfo, api, options) => {
  const filePath = fileInfo.path;

  if (filePath.endsWith('error.tsx')) {
    fs.writeFileSync(filePath, template, { encoding: 'utf8' });
  }
};

module.exports = transform;
