// engine/deploy/providers/netlify.ts — SPEC-478: Netlify deploy via API
/** Trigger a Netlify deployment by calling the deploys API. */
export async function deployToNetlify(config, options) {
    const siteId = config.projectId ?? '';
    const url = `https://api.netlify.com/api/v1/sites/${siteId}/deploys`;
    const body = {
        branch: options.gitBranch,
        deploy_tag: options.name,
        production: (options.environment ?? config.defaultEnvironment ?? 'preview') === 'production',
    };
    try {
        const response = await fetch(url, {
            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: `netlify-error-${String(Date.now())}`,
                provider: 'netlify',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Netlify API ${String(response.status)}: ${text.slice(0, 200)}`,
            };
        }
        const data = (await response.json());
        return {
            id: data.id,
            provider: 'netlify',
            url: data.ssl_url ?? data.url,
            status: mapNetlifyState(data.state ?? 'unknown'),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: `netlify-error-${String(Date.now())}`,
            provider: 'netlify',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
/** Check the status of a Netlify deployment. */
export async function getNetlifyDeploymentStatus(config, deploymentId) {
    const url = `https://api.netlify.com/api/v1/deploys/${deploymentId}`;
    try {
        const response = await fetch(url, {
            headers: {
                Authorization: `Bearer ${config.token}`,
            },
        });
        if (!response.ok) {
            return {
                id: deploymentId,
                provider: 'netlify',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Netlify API ${String(response.status)}`,
            };
        }
        const data = (await response.json());
        return {
            id: data.id,
            provider: 'netlify',
            url: data.ssl_url ?? data.url,
            status: mapNetlifyState(data.state ?? 'unknown'),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: deploymentId,
            provider: 'netlify',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
function mapNetlifyState(state) {
    switch (state.toLowerCase()) {
        case 'ready':
            return 'ready';
        case 'building':
        case 'uploading':
        case 'uploaded':
        case 'processing':
        case 'prepared':
        case 'preparing':
            return 'building';
        case 'error':
        case 'failed':
            return 'error';
        case 'cancelled':
            return 'cancelled';
        default:
            return 'pending';
    }
}
//# sourceMappingURL=netlify.js.map