import { execFileSync } from 'node:child_process';
const defaultRunner = (args) => execFileSync('git', args, { encoding: 'utf8', stdio: 'pipe' });
export function triggerDeploy(options) {
    const { dryRun, environment, runner = defaultRunner, source } = options;
    const ref = `environments/${environment}`;
    const refspec = `${source}:${ref}`;
    if (dryRun) {
        return {
            environment,
            message: `git push origin ${refspec}`,
            ref,
            source,
            status: 'skipped',
        };
    }
    try {
        runner(['push', 'origin', refspec]);
    }
    catch (error) {
        const stderr = error instanceof Error && 'stderr' in error
            ? String(error.stderr)
            : String(error);
        throw new Error(`Deploy failed for environment "${environment}": ${stderr}`);
    }
    return { environment, ref, source, status: 'pushed' };
}
