// engine/deploy/providers/cloudflare-pages.ts — SPEC-478: Cloudflare Pages deploy via API
const CF_API = 'https://api.cloudflare.com/client/v4';
/** Trigger a Cloudflare Pages deployment by calling the deployments API. */
export async function deployToCloudflarePages(config, options) {
    if (!config.team) {
        return {
            id: `cf-error-${String(Date.now())}`,
            provider: 'cloudflare-pages',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: 'Cloudflare account ID (team) is required for Cloudflare Pages deploys.',
        };
    }
    const projectName = config.projectId ?? options.name ?? 'planu-deploy';
    const url = `${CF_API}/accounts/${config.team}/pages/projects/${projectName}/deployments`;
    const body = {};
    if (options.gitBranch !== undefined && options.gitBranch.length > 0) {
        body.branch = options.gitBranch;
    }
    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: `cf-error-${String(Date.now())}`,
                provider: 'cloudflare-pages',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Cloudflare Pages API ${String(response.status)}: ${text.slice(0, 200)}`,
            };
        }
        const wrapper = (await response.json());
        const data = wrapper.result;
        return {
            id: data.id,
            provider: 'cloudflare-pages',
            url: data.url,
            status: mapCloudflarePagesState(data),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: `cf-error-${String(Date.now())}`,
            provider: 'cloudflare-pages',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
/** Check the status of a Cloudflare Pages deployment. */
export async function getCloudflarePagesStatus(config, deploymentId) {
    if (!config.team) {
        return {
            id: deploymentId,
            provider: 'cloudflare-pages',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: 'Cloudflare account ID (team) is required.',
        };
    }
    const projectName = config.projectId ?? 'planu-deploy';
    const url = `${CF_API}/accounts/${config.team}/pages/projects/${projectName}/deployments/${deploymentId}`;
    try {
        const response = await fetch(url, {
            headers: {
                Authorization: `Bearer ${config.token}`,
            },
        });
        if (!response.ok) {
            return {
                id: deploymentId,
                provider: 'cloudflare-pages',
                status: 'error',
                createdAt: new Date().toISOString(),
                errorMessage: `Cloudflare Pages API ${String(response.status)}`,
            };
        }
        const wrapper = (await response.json());
        const data = wrapper.result;
        return {
            id: data.id,
            provider: 'cloudflare-pages',
            url: data.url,
            status: mapCloudflarePagesState(data),
            createdAt: new Date().toISOString(),
        };
    }
    catch (err) {
        return {
            id: deploymentId,
            provider: 'cloudflare-pages',
            status: 'error',
            createdAt: new Date().toISOString(),
            errorMessage: err instanceof Error ? err.message : String(err),
        };
    }
}
function mapCloudflarePagesState(data) {
    const stage = data.latest_stage;
    if (!stage) {
        return 'pending';
    }
    return mapStageStatus(stage.name, stage.status);
}
function mapStageStatus(name, status) {
    const s = status.toLowerCase();
    const n = name.toLowerCase();
    if (s === 'success' || (n === 'deploy' && s === 'active')) {
        return 'ready';
    }
    if (s === 'failure' || s === 'failed' || s === 'error') {
        return 'error';
    }
    if (s === 'active' || s === 'running') {
        return 'building';
    }
    if (s === 'idle' || s === 'queued') {
        return 'pending';
    }
    return 'pending';
}
//# sourceMappingURL=cloudflare-pages.js.map