/**
 * SDK Constants
 *
 * This module contains shared constants used throughout the SDK.
 */
/**
 * Base URL for Zapier API endpoints
 */
export const ZAPIER_BASE_URL = globalThis.process?.env?.ZAPIER_BASE_URL || "https://zapier.com";
/**
 * Calling service name for telemetry headers.
 *
 * Read lazily so tests can stub the env var after module import.
 */
export function getZapierSdkService() {
    return globalThis.process?.env?.ZAPIER_SDK_SERVICE;
}
/**
 * Maximum number of items that can be requested per page across all paginated functions
 */
export const MAX_PAGE_LIMIT = 10000;
/**
 * Default number of items per page for paginated functions
 */
export const DEFAULT_PAGE_SIZE = 100;
/**
 * Default timeout for action execution (in milliseconds)
 */
export const DEFAULT_ACTION_TIMEOUT_MS = 180000;
function parseIntEnvVar(name) {
    const value = globalThis.process?.env?.[name];
    if (value === undefined)
        return undefined;
    const parsed = parseInt(value, 10);
    if (isNaN(parsed)) {
        console.warn(`[zapier-sdk] Invalid value for ${name}: "${value}" (expected integer)`);
        return undefined;
    }
    return parsed;
}
/**
 * Network retry configuration from environment variables
 */
export const ZAPIER_MAX_NETWORK_RETRIES = parseIntEnvVar("ZAPIER_MAX_NETWORK_RETRIES") ?? 3;
export const ZAPIER_MAX_NETWORK_RETRY_DELAY_MS = parseIntEnvVar("ZAPIER_MAX_NETWORK_RETRY_DELAY_MS") ?? 60000;
/**
 * Upper bound on the concurrency cap. Anything beyond this is almost
 * certainly a configuration mistake and would also bypass IEEE 754 safe
 * integer range for inputs like "9".repeat(309), which would silently
 * become Infinity and disable throttling.
 */
export const MAX_CONCURRENCY_LIMIT = 10000;
export function parseConcurrencyEnvVar(name) {
    const value = globalThis.process?.env?.[name];
    if (!value)
        return undefined;
    if (value === "Infinity")
        return Infinity;
    if (/^[1-9]\d*$/.test(value)) {
        const parsed = parseInt(value, 10);
        if (parsed <= MAX_CONCURRENCY_LIMIT)
            return parsed;
    }
    console.warn(`[zapier-sdk] Invalid value for ${name}: "${value}" (expected positive integer 1-${MAX_CONCURRENCY_LIMIT} or "Infinity")`);
    return undefined;
}
/**
 * Default cap on concurrent in-flight HTTP requests per SDK instance.
 * Beyond this, requests queue (FIFO) until a slot frees up. Sized for
 * production workloads that fan out hundreds of action runs in parallel;
 * lower it when running against a smaller backend or to be more
 * conservative. Pass `Infinity` to disable.
 */
export const ZAPIER_MAX_CONCURRENT_REQUESTS = parseConcurrencyEnvVar("ZAPIER_MAX_CONCURRENT_REQUESTS") ?? 200;
/**
 * Approval flow behavior from environment variable.
 * - "disabled": (default when env var is unset) Throw a ZapierApprovalError on
 *   approval-required responses without creating an approval. Callers must
 *   explicitly opt in to approvals via "poll" or "throw".
 * - "poll": Create the approval, open the URL in a browser, poll until
 *   resolved, and retry the original request on success.
 * - "throw": Create the approval and throw a ZapierApprovalError immediately
 *   with the approval URL so the caller can surface it.
 *
 * Read lazily so tests can set the env var after module import.
 */
export function getZapierApprovalMode() {
    const value = globalThis.process?.env?.ZAPIER_APPROVAL_MODE;
    if (value === "disabled" || value === "poll" || value === "throw")
        return value;
    return undefined;
}
/**
 * Default timeout for approval polling (10 minutes)
 */
export const DEFAULT_APPROVAL_TIMEOUT_MS = 10 * 60 * 1000;
/**
 * Default cap on chained approval retries for a single request. Multiple
 * sequential approvals can occur when more than one policy gates the same
 * action; this cap prevents a runaway loop if the server keeps returning
 * approval_required after a successful approval.
 */
export const DEFAULT_MAX_APPROVAL_RETRIES = 2;
