import path from "node:path";
import { fileURLToPath } from "node:url";

//#region src/index.ts
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const REACT_PACKAGE_ROOT = path.resolve(__dirname, "../..");
const REACT_PACKAGE_BUNDLED_WHEEL_DIR = path.resolve(REACT_PACKAGE_ROOT, "./build/wheels");
function vitePluginStliteReact(options) {
	const { wheelOutputDir = "wheels" } = options ?? {};
	return {
		name: "vite-plugin-stlite-react",
		config: () => {
			return {
				resolve: { alias: {
					"streamlit.whl": options?.streamlitWheelFilePath ?? path.join(REACT_PACKAGE_BUNDLED_WHEEL_DIR, "streamlit-1.50.0-cp313-none-any.whl"),
					"stlite_lib.whl": options?.stliteLibWheelFilePath ?? path.join(REACT_PACKAGE_BUNDLED_WHEEL_DIR, "stlite_lib-0.1.0-py3-none-any.whl")
				} },
				assetsInclude: ["**/*.whl"],
				optimizeDeps: { exclude: ["parquet-wasm"] }
			};
		},
		outputOptions(outputOptions) {
			const originalAssetFileNames = outputOptions.assetFileNames;
			return {
				...outputOptions,
				assetFileNames: (assetInfo) => {
					if (assetInfo.names?.some((name) => name.endsWith(".whl"))) return `${wheelOutputDir}/[name][extname]`;
					if (typeof originalAssetFileNames === "function") return originalAssetFileNames(assetInfo);
					return originalAssetFileNames ?? "assets/[name]-[hash][extname]";
				}
			};
		}
	};
}

//#endregion
export { vitePluginStliteReact as default };