import type { NextConfig } from 'next';
import path from 'path';

const sharedSrc = path.resolve(import.meta.dirname, '../shared/src');

const nextConfig: NextConfig = {
  turbopack: {},
  webpack: (config) => {
    // The shared package source uses ESM `.js` extensions on imports (e.g. `./enums/index.js`).
    // When Next.js resolves via tsconfig paths to the raw `.ts` source, webpack needs to
    // know that `.js` imports inside that directory should resolve to `.ts`/`.tsx` files.
    config.resolve = config.resolve ?? {};
    config.resolve.extensionAlias = {
      ...config.resolve.extensionAlias,
      '.js': ['.tsx', '.ts', '.jsx', '.js'],
    };

    // Ensure the shared source directory is included in the module resolution
    config.resolve.alias = {
      ...config.resolve.alias,
      '@meshitrack/shared': sharedSrc,
    };

    return config;
  },
};

export default nextConfig;
