
!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]="9e5340b8-c0a4-5158-a9f4-5d43e74860cd")}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 `toolArgsWithoutPayment` can be validated
 * without provider-specific fields.
 *
 * TODO: This function has mixed responsibilities. It should be split into separate concerns:
 * - validatePaymentCredentials (returns error or void)
 * - sanitizeToolArgs (returns toolArgsWithoutPayment and toolArgsRedacted)
 * - 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 {
            toolArgsWithoutPayment: { ...args },
            toolArgsRedacted: args,
            apifyClient,
        };
    }
    const error = tool.paymentRequired ? provider.validatePayment(args, meta, requestHeaders) : null;
    const errorData = error && provider.getPaymentRequiredData ? provider.getPaymentRequiredData() : undefined;
    const toolArgsWithoutPayment = provider.removePaymentFields(args);
    const toolArgsRedacted = 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,
        toolArgsWithoutPayment,
        toolArgsRedacted,
        apifyClient,
    };
}
//# sourceMappingURL=helpers.js.map
//# debugId=9e5340b8-c0a4-5158-a9f4-5d43e74860cd
