import { ENTRY_POINTS, START_ENVIRONMENT_NAMES } from "../constants.js";
import { createVirtualModule } from "./createVirtualModule.js";
import { normalizeViteClientBuild } from "./start-manifest-plugin/normalized-client-build.js";
import { normalizePath } from "vite";
//#region src/vite/plugins.ts
function createVirtualClientEntryPlugin(opts) {
	return createVirtualModule({
		name: "tanstack-start-core:virtual-client-entry",
		moduleId: ENTRY_POINTS.client,
		enforce: "pre",
		load() {
			return `import ${JSON.stringify(normalizePath(opts.getClientEntry()).replaceAll("\\", "/"))}`;
		}
	});
}
function createPostBuildPlugin(opts) {
	return {
		name: "tanstack-start-core:post-build",
		enforce: "post",
		buildApp: {
			order: "post",
			async handler(builder) {
				const { startConfig } = opts.getConfig();
				await opts.postServerBuild({
					builder,
					startConfig
				});
			}
		}
	};
}
function createDevBaseRewritePlugin(opts) {
	return {
		name: "tanstack-start-core:dev-base-rewrite",
		configureServer(server) {
			if (!opts.shouldRewriteDevBase()) return;
			const basePrefix = opts.resolvedStartConfig.basePaths.publicBase.replace(/\/$/, "");
			server.middlewares.use((req, _res, next) => {
				if (req.url && !req.url.startsWith(basePrefix)) req.url = basePrefix + req.url;
				next();
			});
		}
	};
}
function createCaptureClientBuildPlugin(opts) {
	return {
		name: "tanstack-start:core:capture-bundle",
		applyToEnvironment(environment) {
			return environment.name === START_ENVIRONMENT_NAMES.client;
		},
		enforce: "post",
		generateBundle(_options, bundle) {
			const environment = this.environment.name;
			if (environment !== START_ENVIRONMENT_NAMES.client) throw new Error(`Unexpected environment for client build capture: ${environment}`);
			opts.capturedClientBuild[environment] = normalizeViteClientBuild(bundle);
		}
	};
}
//#endregion
export { createCaptureClientBuildPlugin, createDevBaseRewritePlugin, createPostBuildPlugin, createVirtualClientEntryPlugin };

//# sourceMappingURL=plugins.js.map