// engine/deploy/providers/vercel.ts — SPEC-478: Vercel deploy via API
// SPEC-597-EXEMPT: provider-specific implementation; the literal 'vercel'
// IS the canonical id for this module.
/** Trigger a Vercel deployment by calling the deployments API. */
export async function deployToVercel(config, options) {
    const url = new URL('https://api.vercel.com/v13/deployments');
    if (config.team) {
        url.searchParams.set('teamId', config.team);
    }
    const body = {
        name: options.name ?? 'planu-deploy',
        target: options.environment ?? config.defaultEnvironment ?? 'preview',
        gitMetadata: options.gitBranch !== undefined && options.gitBranch.length > 0
            ? { commitRef: options.gitBranch }
            : undefined,
        project: config.projectId,
    };
    try {
        const response = await fetch(url.toString(), {
            method: 'POST',
            headers: {
                Authorization: `Bearer ${config.token}`,
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(body),
        });
        if (!response.ok) {
            const text = await response.text();
            return {
                id: `vercel-error-${String(Date.now())}`,
                provider: 'vercel',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Vercel API ${String(response.status)}: ${text.slice(0, 200)}`,
            };
        }
        const data = (await response.json());
        return {
            id: data.uid,
            provider: 'vercel',
            url: data.url ? `https://${data.url}` : undefined,
            status: mapVercelState(data.readyState ?? data.state ?? 'pending'),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: `vercel-error-${String(Date.now())}`,
            provider: 'vercel',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
/** Check the status of a Vercel deployment. */
export async function getVercelDeploymentStatus(config, deploymentId) {
    const url = new URL(`https://api.vercel.com/v13/deployments/${deploymentId}`);
    if (config.team) {
        url.searchParams.set('teamId', config.team);
    }
    try {
        const response = await fetch(url.toString(), {
            headers: {
                Authorization: `Bearer ${config.token}`,
            },
        });
        if (!response.ok) {
            return {
                id: deploymentId,
                provider: 'vercel',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Vercel API ${String(response.status)}`,
            };
        }
        const data = (await response.json());
        return {
            id: data.uid,
            provider: 'vercel',
            url: data.url ? `https://${data.url}` : undefined,
            status: mapVercelState(data.readyState ?? data.state ?? 'pending'),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: deploymentId,
            provider: 'vercel',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
function mapVercelState(state) {
    switch (state.toUpperCase()) {
        case 'READY':
            return 'ready';
        case 'BUILDING':
        case 'INITIALIZING':
        case 'QUEUED':
        case 'ANALYZING':
            return 'building';
        case 'ERROR':
        case 'FAILED':
            return 'error';
        case 'CANCELED':
        case 'CANCELLED':
            return 'cancelled';
        default:
            return 'pending';
    }
}
//# sourceMappingURL=vercel.js.map