import withSerwistInit from '@serwist/next';
import { withSentryConfig } from '@sentry/nextjs';
import withPzConfig from '@akinon/next/with-pz-config.js';

/** @type {import('next').NextConfig} */
const nextConfig = {
  rewrites: async () => {
    return [
      {
        source: '/:pz/sitemap.xml',
        destination: '/:pz/xml-sitemap'
      },
      {
        source: '/:pz/sitemap/:node',
        destination: '/:pz/xml-sitemap/:node'
      }
    ];
  },
  headers: async () => {
    return [
      {
        source: '/sw.js',
        headers: [
          {
            key: 'Content-Type',
            value: 'application/javascript; charset=utf-8'
          },
          {
            key: 'Cache-Control',
            value: 'no-cache, no-store, must-revalidate'
          },
          {
            key: 'Content-Security-Policy',
            value: "default-src 'self'; script-src 'self'"
          }
        ]
      }
    ];
  }
};

const withSerwist = withSerwistInit({
  swSrc: 'src/app/sw.ts',
  swDest: 'public/sw.js',
  disable: process.env.NODE_ENV === 'development'
});

const sentryConfig = {
  silent: true,
  dryRun: !process.env.SENTRY_DSN,
  org: 'akinon'
  // project: 'enter_your_project_name_here',
  // authToken: 'enter_your_auth_token_here'
};

const enhancedConfig = withPzConfig(nextConfig);

export default withSentryConfig(withSerwist(enhancedConfig), sentryConfig);
