"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidMcpServerName = isValidMcpServerName;
exports.isValidMcpToolName = isValidMcpToolName;
exports.parseRequiredMcpRef = parseRequiredMcpRef;
exports.isValidRequiredMcpRef = isValidRequiredMcpRef;
exports.dedupeRequiredMcpRefs = dedupeRequiredMcpRefs;
exports.requiredMcpRefCanRunOnWorker = requiredMcpRefCanRunOnWorker;
const MCP_SERVER_NAME_PATTERN = /^(?!-)(?=.{1,128}$)[^\s:/]+$/u;
const MCP_TOOL_NAME_PATTERN = /^(?=.{1,128}$)[^\s:/]+$/u;
const WORKER_NAME_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/u;
function isValidMcpServerName(serverName) {
    return MCP_SERVER_NAME_PATTERN.test(serverName);
}
function isValidMcpToolName(toolName) {
    return MCP_TOOL_NAME_PATTERN.test(toolName);
}
function parseRequiredMcpRef(ref, configuredWorkers) {
    const trimmed = ref.trim();
    if (trimmed.length === 0) {
        return null;
    }
    const providerSplit = trimmed.split(":");
    let worker = null;
    let body = trimmed;
    if (providerSplit.length === 2) {
        const [provider, remainder] = providerSplit;
        if (provider === undefined || remainder === undefined) {
            return null;
        }
        if (!isValidRequiredMcpWorkerName(provider)) {
            return null;
        }
        if (configuredWorkers !== undefined &&
            !configuredWorkers.includes(provider)) {
            return null;
        }
        worker = provider;
        body = remainder ?? "";
    }
    else if (providerSplit.length > 2) {
        return null;
    }
    const parts = body.split("/");
    if (parts.length > 2) {
        return null;
    }
    const [serverName, toolName = null] = parts;
    if (!serverName || !isValidMcpServerName(serverName)) {
        return null;
    }
    if (toolName !== null && !isValidMcpToolName(toolName)) {
        return null;
    }
    return {
        ref: trimmed,
        worker,
        serverName,
        toolName,
    };
}
function isValidRequiredMcpWorkerName(worker) {
    return (WORKER_NAME_PATTERN.test(worker) && worker !== "all" && worker !== "default");
}
function isValidRequiredMcpRef(ref, configuredWorkers) {
    return parseRequiredMcpRef(ref, configuredWorkers) !== null;
}
function dedupeRequiredMcpRefs(refs) {
    return [...new Set(refs)];
}
function requiredMcpRefCanRunOnWorker(ref, worker) {
    const parsed = parseRequiredMcpRef(ref);
    return (parsed !== null && (parsed.worker === null || parsed.worker === worker));
}
