// engine/license-validator/lemon-squeezy.ts — HTTP client for license API.
// Supports Planu self-hosted (Supabase Edge Function) and Lemon Squeezy as fallback.
// Uses native fetch() (Node 18+). No external dependencies.
const DEFAULT_LICENSE_API = 'https://canusqigtzldstnjddio.supabase.co/functions/v1/license-api';
const TIMEOUT_MS = 10_000;
function getBaseUrl() {
    return process.env.SDD_LICENSE_API_URL ?? DEFAULT_LICENSE_API;
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
async function post(endpoint, body) {
    const controller = new AbortController();
    const timer = setTimeout(() => {
        controller.abort();
    }, TIMEOUT_MS);
    try {
        const res = await fetch(`${getBaseUrl()}/${endpoint}`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
            body: JSON.stringify(body),
            signal: controller.signal,
        });
        if (!res.ok) {
            const text = await res.text();
            throw new Error(`License API ${endpoint} failed (${String(res.status)}): ${text}`);
        }
        return (await res.json());
    }
    finally {
        clearTimeout(timer);
    }
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
export async function activateLicense(licenseKey, instanceName) {
    return post('activate', {
        licenseKey,
        instanceName,
    });
}
export async function validateLicense(licenseKey, instanceId) {
    return post('validate', {
        licenseKey,
        instanceId,
    });
}
export async function deactivateLicense(licenseKey, instanceId) {
    return post('deactivate', {
        licenseKey,
        instanceId,
    });
}
//# sourceMappingURL=lemon-squeezy.js.map