import { stableDigest } from "@best-agent/foundation";
export const PLAN_UPDATE_TOOL_NAME = "__best_agent_plan_update";
export const PHASE_RESULT_TOOL_NAME = "__best_agent_phase_result";
export const DELEGATE_TASKS_TOOL_NAME = "delegate_tasks";
export const PROVIDER_TOOL_NAME_RE = /^[A-Za-z0-9_-]{1,64}$/u;
const RESERVED_PROVIDER_TOOL_NAMES = new Set([PLAN_UPDATE_TOOL_NAME, PHASE_RESULT_TOOL_NAME]);
export function isProviderToolName(value) {
    return PROVIDER_TOOL_NAME_RE.test(value);
}
export function toRuntimeToolName(value) {
    return value;
}
export function isReservedProviderToolName(value) {
    return RESERVED_PROVIDER_TOOL_NAMES.has(value);
}
export function isPersistableProviderToolName(value) {
    return isProviderToolName(value) && !isReservedProviderToolName(value);
}
export function toProviderToolName(runtimeName) {
    if (isProviderToolName(runtimeName)) {
        return runtimeName;
    }
    const slug = runtimeName
        .replace(/[^A-Za-z0-9_-]+/gu, "_")
        .replace(/^_+|_+$/gu, "")
        .slice(0, 48);
    return buildHashedProviderToolName(slug || "tool", runtimeName);
}
function buildHashedProviderToolName(slug, hashInput) {
    const hash = stableDigest(hashInput).slice(0, 8);
    const prefix = slug.slice(0, 64 - hash.length - 1) || "tool";
    return `${prefix}_${hash}`;
}
export function toUniqueProviderToolName(runtimeName, used) {
    let providerName = toProviderToolName(runtimeName);
    let collisionIndex = 1;
    while (used.has(providerName)) {
        providerName = buildHashedProviderToolName(providerName, `${runtimeName}:${collisionIndex}`);
        collisionIndex += 1;
    }
    return providerName;
}
export function prepareProviderToolNameBindings(tools) {
    return [...createProviderToolNameCodec(tools).bindings()];
}
export function createProviderToolNameCodec(tools) {
    const usedProviderNames = new Set(RESERVED_PROVIDER_TOOL_NAMES);
    const bindings = tools.map((tool) => {
        const runtimeName = toRuntimeToolName(tool.name);
        const providerName = toUniqueProviderToolName(runtimeName, usedProviderNames);
        usedProviderNames.add(providerName);
        return {
            providerName,
            runtimeName,
        };
    });
    return {
        bindings: () => bindings,
        toProvider(runtimeName) {
            return (bindings.find((binding) => binding.runtimeName === runtimeName)?.providerName ??
                toProviderToolName(runtimeName));
        },
        toRuntime(providerName) {
            return bindings.find((binding) => binding.providerName === providerName)?.runtimeName;
        },
    };
}
export function toProviderHistoryToolName(toolName, bindings) {
    const matched = bindings.find((binding) => binding.runtimeName === toolName || binding.providerName === toolName);
    if (matched) {
        return matched.providerName;
    }
    if (isProviderToolName(toolName) && !isReservedProviderToolName(toolName)) {
        return toolName;
    }
    return toUniqueProviderToolName(toolName, new Set(RESERVED_PROVIDER_TOOL_NAMES));
}
//# sourceMappingURL=provider-tool-names.js.map