"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserAgentHook = void 0;
const config_js_1 = require("../lib/config.js");
// Extract product name from package name part
function getProductName(packageName) {
    try {
        if (packageName) {
            // Extract package name from the package part
            // Format: "@cloudinary/asset-management"
            const match = packageName.match(/@cloudinary\/(.+)/);
            if (match && match[1]) {
                // Convert kebab-case to PascalCase: "asset-management" -> "AssetManagement"
                return match[1]
                    .split('-')
                    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
                    .join('');
            }
        }
        return "Unknown";
    }
    catch {
        return "Unknown";
    }
}
function getRuntime() {
    // Check if we can find MCP-related modules in the call stack
    try {
        const stack = new Error().stack;
        if (stack && (stack.includes('mcp-server') || stack.includes('@modelcontextprotocol'))) {
            return 'MCP';
        }
    }
    catch (e) {
        // Ignore stack trace errors
    }
    return 'JS';
}
function getSystemInfo() {
    const nodeVersion = process.version; // e.g., "v18.17.0"
    const platform = process.platform; // e.g., "darwin", "linux", "win32"
    const arch = process.arch; // e.g., "x64", "arm64"
    return `Node.js ${nodeVersion.substring(1)}; ${platform} ${arch}`;
}
function getEnvDetails() {
    return typeof caches !== 'undefined' && typeof caches.default !== 'undefined' ? ' RemoteMCP' : '';
}
function buildUserAgent(sdkVersion, genVersion, openapiDocVersion, packageName, runtime) {
    const productName = getProductName(packageName);
    const systemInfo = getSystemInfo();
    const envDetails = getEnvDetails();
    return `Cloudinary/${productName} ${runtime}/${sdkVersion} Gen/${genVersion} Schema/${openapiDocVersion} (${systemInfo}${envDetails})`;
}
class UserAgentHook {
    sdkInit(opts) {
        const originalUserAgent = config_js_1.SDK_METADATA.userAgent;
        if (originalUserAgent && originalUserAgent.startsWith("speakeasy-sdk/typescript")) {
            const parts = originalUserAgent.split(" ");
            if (parts.length >= 5) {
                const sdkVersion = parts[1];
                const genVersion = parts[2];
                const openapiDocVersion = parts[3];
                const packageName = parts[4];
                // Ensure all parts are defined before proceeding
                if (sdkVersion && genVersion && openapiDocVersion && packageName) {
                    const runtime = getRuntime();
                    opts.userAgent = buildUserAgent(sdkVersion, genVersion, openapiDocVersion, packageName, runtime);
                }
            }
        }
        return opts;
    }
}
exports.UserAgentHook = UserAgentHook;
//# sourceMappingURL=userAgentHook.js.map