/**
 * sinks/webhook/index.ts
 *
 * Generic HTTP webhook sink — POSTs the full report JSON to any endpoint.
 * This is the universal adapter for integrations that don't have a
 * dedicated sink (Airbyte, Zapier, custom services, etc.).
 *
 * @see docs/design-docs/report-store/sink-architecture.md
 */
export class WebhookSink {
    url;
    headers;
    name;
    constructor(url, headers = {}) {
        this.url = url;
        this.headers = headers;
        try {
            this.name = `webhook:${new URL(url).hostname}`;
        }
        catch {
            this.name = "webhook:invalid-url";
        }
    }
    healthCheck() {
        try {
            new URL(this.url);
            return Promise.resolve({ healthy: true });
        }
        catch {
            return Promise.resolve({
                healthy: false,
                reason: `Invalid URL: ${this.url}`,
            });
        }
    }
    async publish(report) {
        const response = await fetch(this.url, {
            body: JSON.stringify(report),
            headers: { "Content-Type": "application/json", ...this.headers },
            method: "POST",
        });
        if (!response.ok) {
            return {
                error: `HTTP ${response.status}: ${response.statusText}`,
                status: "failed",
            };
        }
        return { detail: `HTTP ${response.status}`, status: "success" };
    }
}
