
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="c82e946f-138c-5615-aafc-3cee90341761")}catch(e){}}();
import { ApifyClient } from '../apify_client.js';
import { buildPaymentRequiredResponse, registerPaymentRequiredInterceptor } from '../utils/payment_errors.js';
/**
 * Prepares tool-call context before validation and execution.
 *
 * This helper centralizes all payment processing:
 * 1. Validates payment credentials (for tools with `paymentRequired: true`)
 * 2. Strips payment fields from args (for clean ajv validation and Actor input)
 * 3. Redacts sensitive fields for logging
 * 4. Creates an ApifyClient with payment headers or standard token
 *
 * Call this BEFORE ajv validation so `toolArgs` can be validated without
 * the `additionalProperties: true` hack.
 *
 * TODO: This function has mixed responsibilities. It should be split into separate concerns:
 * - validatePaymentCredentials (returns error or void)
 * - sanitizeToolArgs (returns toolArgs and logSafeArgs)
 * - createApifyClient (returns apifyClient)
 */
export function prepareToolCallContext(input) {
    const { provider, tool, args, apifyToken, meta, requestHeaders } = input;
    if (!provider) {
        const apifyClient = new ApifyClient({ token: apifyToken });
        registerPaymentRequiredInterceptor(apifyClient);
        return {
            toolArgs: args,
            logSafeArgs: args,
            apifyClient,
        };
    }
    const error = tool.paymentRequired ? provider.validatePayment(args, meta, requestHeaders) : null;
    const errorData = error && provider.getPaymentRequiredData ? provider.getPaymentRequiredData() : undefined;
    const toolArgs = provider.removePaymentFields(args);
    const logSafeArgs = provider.redactForLogging(args);
    const paymentHeaders = provider.getPaymentHeaders(args, meta, requestHeaders);
    const apifyClient = Object.keys(paymentHeaders).length > 0
        ? new ApifyClient({ paymentHeaders })
        : new ApifyClient({ token: apifyToken });
    registerPaymentRequiredInterceptor(apifyClient);
    return {
        paymentRequiredResult: error ? buildPaymentRequiredResponse(error, errorData) : undefined,
        toolArgs,
        logSafeArgs,
        apifyClient,
    };
}
//# sourceMappingURL=helpers.js.map
//# debugId=c82e946f-138c-5615-aafc-3cee90341761
