// NOTE: @ionic/react and @ionic/react-router are pinned to 8.8.7 in package.json.
// 8.8.8 introduced a regression that breaks IonRouterOutlet page transition animations.
// Do not upgrade until the upstream fix is confirmed.
import { defineConfig, type Plugin } from 'vite';
import react from '@vitejs/plugin-react';

const injectRouterBasename: Plugin = {
  name: 'inject-router-basename',
  enforce: 'pre',
  transform(code, id) {
    if (!/\.[jt]sx$/.test(id)) return null;
    const next = code.replace(
      /<IonReactRouter(?![^>]*basename)([^>]*)>/g,
      '<IonReactRouter$1 basename={import.meta.env.BASE_URL}>',
    );
    return next === code ? null : next;
  },
};

const hideScrollbars: Plugin = {
  name: 'hide-scrollbars',
  transformIndexHtml(html) {
    return html.replace(
      '</head>',
      '<style>*{scrollbar-width:none!important;-ms-overflow-style:none!important}*::-webkit-scrollbar{display:none!important}</style></head>',
    );
  },
};

export default defineConfig({
  plugins: [injectRouterBasename, hideScrollbars, react()],
  server: {
    host: '0.0.0.0',
    port: 8100,
    allowedHosts: true,
    hmr: {
      clientPort: process.env.HMR_CLIENT_PORT ? parseInt(process.env.HMR_CLIENT_PORT) : undefined,
    },
  },
  css: { devSourcemap: false },
});
