import { defineConfig } from 'vite';
import { resolve } from 'path';

/**
 * Vite config for CyyWordpress theme.
 *
 * Outputs pre-hashed assets to assets/js/ and assets/css/.
 * Run `npm run dev`   → HMR dev server on :3000
 * Run `npm run build` → production bundles with contenthash
 */
export default defineConfig( ( { command } ) => ( {
	root: resolve( __dirname, 'src' ),

	resolve: {
		alias: {
			'@scss': resolve( __dirname, 'scss' ),
			'@': resolve( __dirname, 'src' ),
		},
	},

	css: {
		preprocessorOptions: {
			scss: {
				// Expose abstracts to every SCSS file without manual imports.
				additionalData: `@use "@scss/abstracts/variables" as *;
@use "@scss/abstracts/mixins" as *;
`,
			},
		},
	},

	build: {
		outDir: resolve( __dirname, 'assets' ),
		emptyOutDir: false,
		manifest: true,
		rollupOptions: {
			input: {
				main: resolve( __dirname, 'src/ts/main.ts' ),
				admin: resolve( __dirname, 'src/admin/customizer.ts' ),
			},
			output: {
				entryFileNames: 'js/[name].[hash].js',
				chunkFileNames: 'js/[name].[hash].js',
				assetFileNames: ( assetInfo ) => {
					if ( assetInfo.name?.endsWith( '.css' ) ) {
						return 'css/[name].[hash][extname]';
					}
					return 'img/[name].[hash][extname]';
				},
			},
		},
	},

	server: {
		port: 3000,
		// Proxy WordPress requests through the local server.
		// Point WP_HOME to http://localhost:3000 in wp-config.php during dev.
		cors: true,
	},
} ) );
