module.exports = {
  devServer: {
    server: {
      type: 'https',
      // options: {
      //   cert: '/path/to/cert.pem',
      //   key: '/path/to/key.pem',
      // }
    },
    setupMiddlewares: (middlewares, devServer) => {
      const { createProxyMiddleware } = require('http-proxy-middleware');
      
      if (!devServer) {
        throw new Error('webpack-dev-server is not defined');
      }
      
      devServer.app.use('/api', createProxyMiddleware({
        target: 'https://backstory:8911',
        changeOrigin: true,
        secure: false,
        buffer: false,
        proxyTimeout: 3600000,
        onProxyRes: function(proxyRes, req, res) {
          proxyRes.headers['cache-control'] = 'no-cache';
          
          if (req.url.includes('/docs') || 
              req.url.includes('/redoc') || 
              req.url.includes('/openapi.json')) {
            return; // Let original headers pass through
          }          
          // Remove any header that might cause buffering
          proxyRes.headers['transfer-encoding'] = 'chunked';
          delete proxyRes.headers['content-length'];
          
          // Set proper streaming headers
          proxyRes.headers['cache-control'] = 'no-cache';
          proxyRes.headers['content-type'] = 'text/event-stream';
          proxyRes.headers['connection'] = 'keep-alive';
        },
      }));
      
      return middlewares;
    }
  },
  webpack: {
    configure: (webpackConfig) => {
      webpackConfig.devtool = 'source-map';
      // Add .ts and .tsx to resolve.extensions
      webpackConfig.resolve.extensions = [
        ...webpackConfig.resolve.extensions,
        '.ts',
        '.tsx',
      ];
      return webpackConfig;
    },
  },
};
