import { createZapierApi } from "../../api";
import { definePlugin } from "../../utils/plugin-utils";
import { resolveCredentials } from "../../credentials";
import { ZAPIER_BASE_URL, ZAPIER_MAX_CONCURRENT_REQUESTS, ZAPIER_MAX_NETWORK_RETRIES, ZAPIER_MAX_NETWORK_RETRY_DELAY_MS, } from "../../constants";
export const apiPlugin = definePlugin((sdk) => {
    // Extract all options - everything passed to the plugin
    const { fetch: customFetch = globalThis.fetch, baseUrl = ZAPIER_BASE_URL, credentials, token, onEvent, debug = false, maxNetworkRetries = ZAPIER_MAX_NETWORK_RETRIES, maxNetworkRetryDelayMs = ZAPIER_MAX_NETWORK_RETRY_DELAY_MS, maxConcurrentRequests = ZAPIER_MAX_CONCURRENT_REQUESTS, approvalTimeoutMs, maxApprovalRetries, approvalMode, callerPackage, } = sdk.context.options;
    // Create the API client - it will handle token resolution internally
    const api = createZapierApi({
        baseUrl,
        credentials,
        token,
        debug,
        fetch: customFetch,
        onEvent,
        maxNetworkRetries,
        maxNetworkRetryDelayMs,
        maxConcurrentRequests,
        approvalTimeoutMs,
        maxApprovalRetries,
        approvalMode,
        callerPackage,
    });
    return {
        context: {
            api,
            resolveCredentials: () => resolveCredentials({
                credentials,
                token,
                baseUrl,
            }),
        },
    };
});
