/**
 * Vite SPA CDN build preparation (pure).
 *
 * For cloud deploys, hashed assets are uploaded to the Agentuity CDN.
 * Without an absolute Vite `base`, the SPA keeps loading `/assets/…` from
 * the app origin. This module returns command/env overrides so the adapter
 * can apply them without mutating detection results or touching disk.
 *
 * Single mechanism: inject `vite … --base=<cdn>/` into the build command.
 * If the build command does not invoke `vite`, we warn and leave base alone.
 */
import { PACK_ONLY_DEPLOYMENT_ID, resolveAgentuityCdnBase, resolveAgentuityCdnOrigin, } from "../cdn-origin.js";
import { injectViteBaseFlag } from "../vite-cli-base.js";
export { PACK_ONLY_DEPLOYMENT_ID } from "../cdn-origin.js";
export { injectViteBaseFlag } from "../vite-cli-base.js";
export { resolveAgentuityCdnBase as resolveViteCdnBase } from "../cdn-origin.js";
/**
 * Compute CDN base overrides for a Vite deploy build.
 * Pure: does not mutate inputs or touch the filesystem.
 */
export function prepareViteCdnBuild(options) {
    const env = options.env ?? process.env;
    const logs = [];
    const cdnBase = resolveAgentuityCdnBase({
        deploymentId: options.deploymentId,
        env,
    });
    if (!cdnBase) {
        options.logger.debug('Vite CDN base: skipped (no deployment id / AGENTUITY_CDN_ORIGIN)');
        return {
            buildCommand: options.buildCommand,
            buildEnv: options.buildEnv,
            logs,
        };
    }
    const cdnOrigin = resolveAgentuityCdnOrigin({
        deploymentId: options.deploymentId,
        env,
    });
    const deploymentId = options.deploymentId?.trim() || env.AGENTUITY_CLOUD_DEPLOYMENT_ID?.trim() || undefined;
    const buildEnv = {
        ...options.buildEnv,
        ...(cdnOrigin ? { AGENTUITY_CDN_ORIGIN: cdnOrigin } : {}),
        ...(deploymentId && deploymentId !== PACK_ONLY_DEPLOYMENT_ID
            ? { AGENTUITY_CLOUD_DEPLOYMENT_ID: deploymentId }
            : {}),
    };
    const buildCommand = injectViteBaseFlag(options.buildCommand, cdnBase);
    if (buildCommand !== options.buildCommand) {
        logs.push(`✓ Vite CDN base via CLI: --base=${cdnBase}`);
        options.logger.debug('Vite CDN: injected --base into build command');
    }
    else {
        logs.push('⚠ Vite CDN: build command does not invoke vite; could not inject --base — asset URLs may stay on the app origin');
        options.logger.debug('Vite CDN: build command %j has no vite segment for --base inject', options.buildCommand);
    }
    return {
        cdnBase,
        buildCommand,
        buildEnv,
        logs,
    };
}
//# sourceMappingURL=cdn-build.js.map