"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.runFrameworkPostChecks = runFrameworkPostChecks;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
function readIfExists(file) {
    try {
        return fs.readFileSync(file, 'utf8');
    }
    catch {
        return undefined;
    }
}
function findFirstExisting(targetDir, names) {
    for (const name of names) {
        const p = path.join(targetDir, name);
        if (fs.existsSync(p))
            return p;
    }
    return undefined;
}
const NEXTJS_CONFIG_FILES = [
    'next.config.js',
    'next.config.mjs',
    'next.config.cjs',
    'next.config.ts',
];
const NEXTJS_STANDALONE_RX = /output\s*:\s*['"`]standalone['"`]/;
const NEXTJS_CHECK = {
    framework: 'nextjs',
    run(targetDir) {
        const configPath = findFirstExisting(targetDir, NEXTJS_CONFIG_FILES);
        if (!configPath) {
            return [
                {
                    level: 'warn',
                    title: 'Missing next.config — standalone output not configured',
                    detail: 'The Dockerfile copies from `.next/standalone` and `.next/static`, which only exist when Next.js is built with `output: "standalone"`. Without it the build will fail with "/app/.next/standalone: not found".',
                    hint: 'Create `next.config.js` at the project root containing:\n    module.exports = { output: "standalone" };',
                },
            ];
        }
        const raw = readIfExists(configPath) ?? '';
        if (NEXTJS_STANDALONE_RX.test(raw))
            return [];
        const filename = path.basename(configPath);
        return [
            {
                level: 'warn',
                title: `${filename} does not enable Next.js standalone output`,
                detail: 'The Dockerfile copies from `.next/standalone` and `.next/static`, which only exist when Next.js is built with `output: "standalone"`. Without it the build will fail with "/app/.next/standalone: not found".',
                hint: `Add \`output: "standalone"\` to the config object in ${filename}, for example:\n    /** @type {import("next").NextConfig} */\n    const nextConfig = {\n      output: "standalone",\n      // ...your existing options\n    };\n    module.exports = nextConfig;`,
            },
        ];
    },
};
const NUXT_CONFIG_FILES = [
    'nuxt.config.ts',
    'nuxt.config.js',
    'nuxt.config.mjs',
];
const NUXT_NODE_PRESET_RX = /preset\s*:\s*['"`]node[_-]server['"`]/;
const NUXT_CHECK = {
    framework: 'nuxt',
    run(targetDir) {
        const configPath = findFirstExisting(targetDir, NUXT_CONFIG_FILES);
        if (!configPath) {
            return [
                {
                    level: 'info',
                    title: 'No nuxt.config found',
                    detail: 'The Dockerfile runs `.output/server/index.mjs`, the default Nitro Node preset output. If you have set a custom Nitro preset (vercel, cloudflare, ...) the build artifacts will differ and the Dockerfile may need adjusting.',
                    hint: 'No action needed if you use the default Node preset. Otherwise, ensure the Nitro preset emits a Node server entrypoint.',
                },
            ];
        }
        const raw = readIfExists(configPath) ?? '';
        if (!NUXT_NODE_PRESET_RX.test(raw))
            return [];
        return [];
    },
};
const SVELTEKIT_PKG = 'package.json';
const SVELTEKIT_ADAPTER_RX = /@sveltejs\/adapter-node/;
const SVELTEKIT_CHECK = {
    framework: 'sveltekit',
    run(targetDir) {
        const pkgPath = path.join(targetDir, SVELTEKIT_PKG);
        const raw = readIfExists(pkgPath);
        if (!raw)
            return [];
        if (SVELTEKIT_ADAPTER_RX.test(raw))
            return [];
        return [
            {
                level: 'warn',
                title: 'SvelteKit project is not using @sveltejs/adapter-node',
                detail: 'The Dockerfile runs `node build/index.js`, the output of the Node adapter. Other adapters (vercel, cloudflare, static) emit different artifacts and the container build will fail.',
                hint: 'Install the Node adapter and reference it in svelte.config.js:\n    npm i -D @sveltejs/adapter-node\n    // svelte.config.js\n    import adapter from "@sveltejs/adapter-node";\n    export default { kit: { adapter: adapter() } };',
            },
        ];
    },
};
const CHECKS = new Map([
    [NEXTJS_CHECK.framework, NEXTJS_CHECK],
    [NUXT_CHECK.framework, NUXT_CHECK],
    [SVELTEKIT_CHECK.framework, SVELTEKIT_CHECK],
]);
function runFrameworkPostChecks(framework, targetDir) {
    const check = CHECKS.get(framework);
    if (!check)
        return [];
    try {
        return check.run(targetDir);
    }
    catch {
        return [];
    }
}
