const DEFAULT_ADMIN_PATH = '/admin';
const CMS_EXTERNAL_PACKAGES = [
    '@actuate-media/cms-core',
    'sharp',
    'jose',
    'pg',
    '@prisma/client',
    '@prisma/adapter-pg',
];
const ACTUATE_PACKAGES = [
    '@actuate-media/cms-admin',
    '@actuate-media/platform-vercel',
    '@actuate-media/platform-aws',
    '@actuate-media/plugin-seo',
    '@actuate-media/plugin-forms',
    '@actuate-media/plugin-media',
    '@actuate-media/plugin-email',
    '@actuate-media/plugin-blocks',
    '@actuate-media/plugin-navigation',
    '@actuate-media/plugin-redirects',
    '@actuate-media/plugin-ai',
    '@actuate-media/plugin-commerce',
];
const VERCEL_BLOB_REMOTE_PATTERN = {
    protocol: 'https',
    hostname: '**.blob.vercel-storage.com',
};
function hasRemotePattern(patterns, hostname) {
    return patterns.some((pattern) => {
        return (!!pattern &&
            typeof pattern === 'object' &&
            pattern.hostname === hostname);
    });
}
/** Wrap a Next.js configuration with Actuate CMS settings. */
export function withActuateCMS(cmsConfig, nextConfig = {}) {
    const adminPath = cmsConfig.admin?.path ?? DEFAULT_ADMIN_PATH;
    const existingExternals = nextConfig.serverExternalPackages ?? [];
    const mergedExternals = [...new Set([...existingExternals, ...CMS_EXTERNAL_PACKAGES])];
    const existingTranspile = nextConfig.transpilePackages ?? [];
    const mergedTranspile = [...new Set([...existingTranspile, ...ACTUATE_PACKAGES])].filter((pkg) => !mergedExternals.includes(pkg));
    const existingExperimental = (nextConfig.experimental ?? {});
    const existingServerActions = (existingExperimental.serverActions ?? {});
    const existingImages = (nextConfig.images ?? {});
    const existingRemotePatterns = (existingImages.remotePatterns ?? []);
    const remotePatterns = hasRemotePattern(existingRemotePatterns, VERCEL_BLOB_REMOTE_PATTERN.hostname)
        ? existingRemotePatterns
        : [...existingRemotePatterns, VERCEL_BLOB_REMOTE_PATTERN];
    const existingWebpack = nextConfig.webpack;
    // Next.js 16 enables Turbopack for `next build` by default. Injecting a
    // no-op `webpack` hook when the consumer did not supply one triggers a hard
    // build error ("webpack config and no turbopack config"). Only attach
    // `webpack` when the consumer already customizes it.
    const wrapped = {
        ...nextConfig,
        serverExternalPackages: mergedExternals,
        transpilePackages: mergedTranspile,
        experimental: {
            ...existingExperimental,
            serverActions: {
                ...existingServerActions,
            },
        },
        images: {
            ...existingImages,
            remotePatterns,
        },
        env: {
            ...nextConfig.env,
            ACTUATE_ADMIN_PATH: adminPath,
            ACTUATE_COLLECTIONS: Object.keys(cmsConfig.collections ?? {}).join(','),
            ACTUATE_GLOBALS: Object.keys(cmsConfig.globals ?? {}).join(','),
        },
    };
    if (existingWebpack) {
        ;
        wrapped.webpack = (config, options) => existingWebpack(config, options);
    }
    return wrapped;
}
/** Returns the configured admin path (reads from env at runtime). */
export function getAdminPath() {
    return process.env.ACTUATE_ADMIN_PATH ?? DEFAULT_ADMIN_PATH;
}
//# sourceMappingURL=next.js.map