/**
 * Fetches the current license status from the Parhelia backend
 */
export async function getLicenseStatus() {
    const response = await fetch("/parhelia/license-status", {
        method: "GET",
        headers: {
            Accept: "application/json",
        },
        credentials: "include",
    });
    if (!response.ok) {
        throw new Error(`Failed to get license status: ${response.status}`);
    }
    return response.json();
}
/**
 * Requests a license from the license server (proxied through Sitecore)
 */
export async function requestLicense(request) {
    // Proxy through Sitecore to avoid CORS issues and hide license server URL
    const response = await fetch("/parhelia/license-request", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        },
        credentials: "include",
        body: JSON.stringify(request),
    });
    if (response.status === 429) {
        const data = await response.json();
        return {
            status: "rate_limited",
            message: data.message || "Too many requests",
            retryAfterSeconds: data.retryAfterSeconds,
        };
    }
    if (!response.ok) {
        return {
            status: "error",
            message: `Request failed: ${response.status}`,
        };
    }
    return response.json();
}
/**
 * Verifies an activation code with the license server (proxied through Sitecore)
 */
export async function verifyActivationCode(request) {
    // Proxy through Sitecore to avoid CORS issues and hide license server URL
    const response = await fetch("/parhelia/license-verify-code", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        },
        credentials: "include",
        body: JSON.stringify(request),
    });
    if (response.status === 429) {
        const data = await response.json();
        return {
            status: "rate_limited",
            message: data.message || "Too many incorrect activation code attempts",
            retryAfterSeconds: data.retryAfterSeconds,
        };
    }
    if (!response.ok) {
        return {
            status: "error",
            message: `Verification failed: ${response.status}`,
        };
    }
    return response.json();
}
/**
 * JSON body for Sitecore License Data: v1 flat {@link LicenseData}, or v2 envelope `{ license, signature }`.
 */
export function serializeLicenseForStore(license, v2OuterSignature) {
    if (v2OuterSignature != null && v2OuterSignature !== "") {
        return JSON.stringify({ license, signature: v2OuterSignature });
    }
    return JSON.stringify(license);
}
/**
 * Stores a license in the Parhelia backend (v1 flat or v2 envelope).
 */
export async function storeLicense(license, v2OuterSignature) {
    const response = await fetch("/parhelia/license-store", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        },
        credentials: "include",
        body: serializeLicenseForStore(license, v2OuterSignature),
    });
    if (!response.ok) {
        const errorText = await response.text();
        return {
            success: false,
            message: errorText || `Failed to store license: ${response.status}`,
        };
    }
    return response.json();
}
/**
 * Clears the license cache in the Parhelia backend
 */
export async function clearLicenseCache() {
    await fetch("/parhelia/license-clear-cache", {
        method: "POST",
        credentials: "include",
    });
}
/**
 * Checks if a specific feature is available
 */
export function hasFeature(features, feature) {
    if (!features || features.length === 0) {
        return false;
    }
    return features.some((f) => f.toLowerCase() === feature.toLowerCase() ||
        f.toLowerCase() === "full");
}
/**
 * Format retry-after seconds into human-readable format (e.g., "46m 42s", "1h 15m", "45s")
 */
export function formatRetryTime(seconds) {
    if (seconds < 60) {
        return `${seconds}s`;
    }
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = seconds % 60;
    if (minutes < 60) {
        return remainingSeconds > 0 ? `${minutes}m ${remainingSeconds}s` : `${minutes}m`;
    }
    const hours = Math.floor(minutes / 60);
    const remainingMinutes = minutes % 60;
    return remainingMinutes > 0 ? `${hours}h ${remainingMinutes}m` : `${hours}h`;
}
/**
 * Format retry-after seconds into simple countdown format:
 * - If >= 60 seconds: show minutes only (e.g., "22m")
 * - If < 60 seconds: show seconds only (e.g., "45s")
 */
export function formatRetryTimeSimple(seconds) {
    if (seconds < 60) {
        return `${seconds}s`;
    }
    const minutes = Math.floor(seconds / 60);
    return `${minutes}m`;
}
//# sourceMappingURL=licenseService.js.map