export function resolveEnvironments(config, envFlag) {
    if (envFlag) {
        if (!config.environments.has(envFlag)) {
            const available = [...config.environments.keys()].join(', ');
            throw new Error(`Environment "${envFlag}" not found. Available: ${available}`);
        }
        return [envFlag];
    }
    return [...config.environments.keys()];
}
function resolveTargets(env, targetFlag) {
    const available = [];
    if (env.back)
        available.push('back');
    if (env.front)
        available.push('front');
    if (targetFlag) {
        if (targetFlag !== 'back' && targetFlag !== 'front') {
            throw new Error(`Invalid target "${targetFlag}". Must be "back" or "front".`);
        }
        if (!available.includes(targetFlag)) {
            throw new Error(`Target "${targetFlag}" not configured in this environment.`);
        }
        return [targetFlag];
    }
    return available;
}
function buildSecretPrefix(envName, target) {
    return `${envName.toUpperCase()}_${target.toUpperCase()}_`;
}
const nodeEnvMapping = {
    dev: 'development',
    prod: 'production',
    qa: 'qa',
};
export function deriveNodeEnv(envName) {
    return nodeEnvMapping[envName] ?? envName;
}
export function buildPlans(config, environments, flags, targetFlag) {
    const plans = [];
    for (const envName of environments) {
        const env = config.environments.get(envName);
        const targets = resolveTargets(env, targetFlag);
        for (const target of targets) {
            const targetConfig = env[target];
            const defaultDir = target === 'back' ? 'backend' : 'frontend';
            const defaultDotEnvEnc = target === 'back' ? `.env.${envName}.enc` : undefined;
            const ftpPassword = flags.envFtpPasswords?.[envName] ?? flags.ftpPassword;
            if (!flags.skipHosting && !ftpPassword) {
                throw new Error(`FTP password required for environment "${envName}". Set ${envName.toUpperCase()}_FTP_PASSWORD or FTP_PASSWORD.`);
            }
            const plan = {
                dir: targetConfig.dir ?? defaultDir,
                dnsRecordType: targetConfig.dnsRecordType,
                dnsValue: targetConfig.dnsValue,
                dnsZone: targetConfig.dnsZone,
                domain: targetConfig.domain,
                dotEnvEnc: targetConfig.dotEnvEnc ?? defaultDotEnvEnc,
                envConfiguration: target === 'front'
                    ? (targetConfig.envConfiguration ?? envName)
                    : undefined,
                environment: envName,
                ftpLogin: targetConfig.ftpLogin ?? config.project.name,
                ftpPassword: ftpPassword ?? '',
                pleskSshUser: flags.pleskSshUser,
                project: config.project,
                secretPrefix: buildSecretPrefix(envName, target),
                server: env.server,
                skipCicd: flags.skipCicd,
                skipDns: flags.skipDns,
                skipHosting: flags.skipHosting,
                skipSsh: flags.skipSsh,
                startupFile: targetConfig.startupFile ?? 'dist/src/main.js',
                target,
            };
            if (targetConfig.database) {
                const dbPassword = flags.envDbPasswords?.[envName] ?? flags.dbPassword;
                if (!dbPassword) {
                    throw new Error(`DB password required for environment "${envName}", target "${target}". Set ${envName.toUpperCase()}_DB_PASSWORD or DB_PASSWORD.`);
                }
                plan.database = {
                    ...targetConfig.database,
                    password: dbPassword,
                };
            }
            plans.push(plan);
        }
    }
    return plans;
}
