"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultProviderFilePath = exports.DEFAULT_CUSTOM_PROVIDER_EXECUTION_TIMEOUT_SECONDS = exports.AZURE_CODE_INTERPRETER_MAX_RUNTIME_SECONDS = exports.DEFAULT_AZURE_SESSIONS_API_VERSION = void 0;
exports.getAzureSessionsConfig = getAzureSessionsConfig;
exports.isAzureSessionsConfigured = isAzureSessionsConfigured;
exports.clampExecutionTimeoutSeconds = clampExecutionTimeoutSeconds;
exports.getSessionPoolName = getSessionPoolName;
exports.buildSessionPoolBaseUrl = buildSessionPoolBaseUrl;
exports.getSessionPoolManagementEndpoint = getSessionPoolManagementEndpoint;
exports.getDefaultExampleBundleDirectory = getDefaultExampleBundleDirectory;
/* eslint-disable @typescript-eslint/consistent-type-imports */
const node_path_1 = __importDefault(require("node:path"));
const SESSIONS_AUDIENCE = 'https://dynamicsessions.io';
/** Documented in Microsoft Learn for code interpreter POST /executions. */
exports.DEFAULT_AZURE_SESSIONS_API_VERSION = '2025-10-02-preview';
/** Maximum runtime per code execution (Azure code interpreter sessions). */
exports.AZURE_CODE_INTERPRETER_MAX_RUNTIME_SECONDS = 220;
function readEnvExecutionTimeoutSeconds() {
    const raw = process.env.AZURE_SESSIONS_EXECUTION_TIMEOUT_SECONDS?.trim();
    if (!raw) {
        return undefined;
    }
    const parsed = Number(raw);
    if (!Number.isFinite(parsed) || parsed <= 0) {
        return undefined;
    }
    return Math.min(Math.floor(parsed), exports.AZURE_CODE_INTERPRETER_MAX_RUNTIME_SECONDS);
}
/** Default timeout for custom provider bootstrap (sync /executions). */
exports.DEFAULT_CUSTOM_PROVIDER_EXECUTION_TIMEOUT_SECONDS = readEnvExecutionTimeoutSeconds() ?? exports.AZURE_CODE_INTERPRETER_MAX_RUNTIME_SECONDS;
function getAzureSessionsConfig() {
    const subscriptionId = process.env.AZURE_SESSIONS_SUBSCRIPTION_ID?.trim();
    const resourceGroup = process.env.AZURE_SESSIONS_RESOURCE_GROUP?.trim();
    const region = process.env.AZURE_SESSIONS_REGION?.trim();
    const pythonPoolName = process.env.AZURE_SESSIONS_PYTHON_POOL_NAME?.trim();
    const nodePoolName = process.env.AZURE_SESSIONS_NODE_POOL_NAME?.trim();
    if (!subscriptionId || !resourceGroup || !region || !pythonPoolName || !nodePoolName) {
        return null;
    }
    return {
        subscriptionId,
        resourceGroup,
        region,
        apiVersion: process.env.AZURE_SESSIONS_API_VERSION?.trim() || exports.DEFAULT_AZURE_SESSIONS_API_VERSION,
        pythonPoolName,
        nodePoolName,
        tokenAudience: SESSIONS_AUDIENCE,
    };
}
function isAzureSessionsConfigured() {
    return getAzureSessionsConfig() !== null;
}
/** Enforce Azure's per-execution maximum (600s). */
function clampExecutionTimeoutSeconds(requested) {
    const value = requested ?? exports.DEFAULT_CUSTOM_PROVIDER_EXECUTION_TIMEOUT_SECONDS;
    if (!Number.isFinite(value) || value <= 0) {
        return exports.DEFAULT_CUSTOM_PROVIDER_EXECUTION_TIMEOUT_SECONDS;
    }
    return Math.min(Math.floor(value), exports.AZURE_CODE_INTERPRETER_MAX_RUNTIME_SECONDS);
}
function getSessionPoolName(language) {
    const config = getAzureSessionsConfig();
    if (!config) {
        throw new Error('Azure Sessions is not configured');
    }
    return language === 'py' ? config.pythonPoolName : config.nodePoolName;
}
function buildSessionPoolBaseUrl(config, poolName) {
    return `https://${config.region}.dynamicsessions.io/subscriptions/${config.subscriptionId}/resourceGroups/${config.resourceGroup}/sessionPools/${poolName}`;
}
/** Optional override from Portal / `az containerapp sessionpool show --query properties.poolManagementEndpoint` */
function getSessionPoolManagementEndpoint(language) {
    const config = getAzureSessionsConfig();
    if (!config) {
        throw new Error('Azure Sessions is not configured');
    }
    const override = language === 'py'
        ? process.env.AZURE_SESSIONS_PYTHON_POOL_ENDPOINT?.trim()
        : process.env.AZURE_SESSIONS_NODE_POOL_ENDPOINT?.trim();
    if (override) {
        return override.replace(/\/$/, '');
    }
    return buildSessionPoolBaseUrl(config, getSessionPoolName(language));
}
/** Bundled example providers used by the sandbox invoke API. */
function getDefaultExampleBundleDirectory(language) {
    const subdir = language === 'py' ? 'python' : 'javascript';
    return node_path_1.default.resolve(process.cwd(), 'examples', 'azure-custom-provider', subdir);
}
var parseProviderFilePath_1 = require("./parseProviderFilePath");
Object.defineProperty(exports, "getDefaultProviderFilePath", { enumerable: true, get: function () { return parseProviderFilePath_1.getDefaultProviderFilePath; } });
//# sourceMappingURL=config.js.map