
!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]="35720b7a-2b3d-5e5b-956f-1a75a99589c1")}catch(e){}}();
import { ApifyClient as _ApifyClient } from 'apify-client';
import { USER_AGENT_ORIGIN } from './const.js';
/**
 * Adds a User-Agent header to the request config.
 * @param config
 * @private
 */
function addUserAgent(config) {
    var _a, _b;
    const updatedConfig = { ...config };
    updatedConfig.headers = (_a = updatedConfig.headers) !== null && _a !== void 0 ? _a : {};
    updatedConfig.headers['User-Agent'] = `${(_b = updatedConfig.headers['User-Agent']) !== null && _b !== void 0 ? _b : ''}; ${USER_AGENT_ORIGIN}`;
    return updatedConfig;
}
export function getApifyAPIBaseUrl() {
    // Workaround for Actor server where the platform APIFY_API_BASE_URL did not work with getActorDefinition from actors.ts
    if (process.env.APIFY_IS_AT_HOME)
        return 'https://api.apify.com';
    return process.env.APIFY_API_BASE_URL || 'https://api.apify.com';
}
export class ApifyClient extends _ApifyClient {
    constructor(options) {
        var _a;
        /**
         * In order to publish to DockerHub, we need to run their build task to validate our MCP server.
         * This was failing since we were sending this dummy token to Apify in order to build the Actor tools.
         * So if we encounter this dummy value, we remove it to use Apify client as unauthenticated, which is sufficient
         * for server start and listing of tools.
         */
        if (((_a = options.token) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'your-apify-token' || options.token === null) {
            // eslint-disable-next-line no-param-reassign
            delete options.token;
        }
        const { skyfirePayId, ...clientOptions } = options;
        const requestInterceptors = [addUserAgent];
        /**
         * Add skyfire-pay-id header if provided.
         */
        if (skyfirePayId) {
            requestInterceptors.push((config) => {
                var _a;
                const updatedConfig = { ...config };
                updatedConfig.headers = (_a = updatedConfig.headers) !== null && _a !== void 0 ? _a : {};
                updatedConfig.headers['skyfire-pay-id'] = skyfirePayId;
                return updatedConfig;
            });
        }
        super({
            // token null case is handled, we can assert type here
            ...clientOptions,
            baseUrl: getApifyAPIBaseUrl(),
            requestInterceptors,
        });
    }
}
/**
 * Creates ApifyClient with appropriate credentials based on Skyfire mode.
 * In Skyfire mode, uses skyfire-pay-id from args; otherwise uses apifyToken.
 *
 * @param apifyMcpServer - The MCP server instance with configuration options
 * @param args - Tool arguments that may contain skyfire-pay-id
 * @param apifyToken - Standard Apify token for non-Skyfire mode
 * @returns ApifyClient instance configured for the appropriate mode
 */
export function createApifyClientWithSkyfireSupport(apifyMcpServer, args, apifyToken) {
    return apifyMcpServer.options.skyfireMode && typeof args['skyfire-pay-id'] === 'string'
        ? new ApifyClient({ skyfirePayId: args['skyfire-pay-id'] })
        : new ApifyClient({ token: apifyToken });
}
//# sourceMappingURL=apify_client.js.map
//# debugId=35720b7a-2b3d-5e5b-956f-1a75a99589c1
