import fs from 'fs';
import path from 'path';
const DEV_CONFIG_FILENAMES = [
    '.n8nac-dev.json',
    '.n8n-as-code-dev.json',
];
export function resolveN8nacCommandRefs(options = {}) {
    const env = options.env ?? process.env;
    const override = cleanCommand(options.override);
    if (override)
        return buildRefs(override, 'override');
    const envCommand = cleanCommand(env.N8NAC_COMMAND);
    if (envCommand)
        return buildRefs(envCommand, 'env');
    const workspaceCommand = cleanCommand(readWorkspaceCommand(options.projectRoot));
    if (workspaceCommand)
        return buildRefs(workspaceCommand, 'workspace-config');
    const published = options.distTag ? `npx --yes n8nac@${options.distTag}` : 'npx --yes n8nac';
    return buildRefs(published, 'published');
}
export function getN8nacDevConfigFilenames() {
    return DEV_CONFIG_FILENAMES;
}
function buildRefs(cliCmd, source) {
    return {
        cliCmd,
        skillsCmd: `${cliCmd} skills`,
        source,
    };
}
function cleanCommand(value) {
    const trimmed = value?.trim();
    return trimmed || undefined;
}
function readWorkspaceCommand(projectRoot) {
    if (!projectRoot)
        return undefined;
    for (const filename of DEV_CONFIG_FILENAMES) {
        const filePath = path.join(projectRoot, filename);
        if (!fs.existsSync(filePath))
            continue;
        try {
            const config = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
            return config.commands?.n8nac ?? config.n8nacCommand;
        }
        catch {
            return undefined;
        }
    }
    return undefined;
}
//# sourceMappingURL=cli-command-resolver.js.map