"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MANUAL_TOPIC_TITLES = exports.DEFAULT_MANUAL_TOPICS = exports.MANUAL_TOPICS = void 0;
exports.getManualSpecTopics = getManualSpecTopics;
exports.getManualTopicTitle = getManualTopicTitle;
exports.shouldWarnMissingSection = shouldWarnMissingSection;
exports.builtinManualTopicPaths = builtinManualTopicPaths;
exports.mcpManualTopicPaths = mcpManualTopicPaths;
exports.buildBuiltinManualSpec = buildBuiltinManualSpec;
exports.buildMcpManualSpec = buildMcpManualSpec;
exports.buildRawMcpManualSpec = buildRawMcpManualSpec;
const path = __importStar(require("path"));
exports.MANUAL_TOPICS = ['index', 'principles', 'tools', 'scenarios', 'errors'];
exports.DEFAULT_MANUAL_TOPICS = ['index', 'tools'];
exports.MANUAL_TOPIC_TITLES = {
    index: { en: 'Overview', zh: '概述' },
    principles: { en: 'Principles', zh: '原则' },
    tools: { en: 'Tools', zh: '工具' },
    scenarios: { en: 'Scenarios', zh: '场景' },
    errors: { en: 'Error Handling', zh: '错误处理' },
};
function getManualSpecTopics(spec) {
    const topics = spec?.topics;
    if (!Array.isArray(topics) || topics.length === 0) {
        return exports.MANUAL_TOPICS;
    }
    return topics;
}
function getManualTopicTitle(topic, language, spec) {
    const override = spec?.topicTitlesI18n?.[topic];
    if (override) {
        return override[language];
    }
    return exports.MANUAL_TOPIC_TITLES[topic][language];
}
function shouldWarnMissingSection(spec) {
    return spec?.warnOnMissing ?? true;
}
// ---------------------------------------------------------------------------
// Path builders (no hardcoded toolsetId / serverId strings)
// ---------------------------------------------------------------------------
/**
 * Build manual topic file paths for a built-in toolset.
 * Format: `prompts/<toolsetId>/<lang>/<topic>.md`
 *
 * No heuristics — `toolsetId` is an explicit parameter.
 */
function builtinManualTopicPaths(toolsetId, language) {
    const langDir = language;
    const baseDir = `prompts/${toolsetId}`;
    return {
        index: path.join(baseDir, langDir, 'index.md'),
        principles: path.join(baseDir, langDir, 'principles.md'),
        tools: path.join(baseDir, langDir, 'tools.md'),
        scenarios: path.join(baseDir, langDir, 'scenarios.md'),
        errors: path.join(baseDir, langDir, 'errors.md'),
    };
}
/**
 * Build manual topic file paths for an MCP toolset.
 * Format: `<contentFilePrefix>/<topic>.<lang>.md`
 *
 * `contentFilePrefix` comes directly from the MCP server's mcp.yaml
 * `manual.contentFile` field — no hardcoded serverId paths.
 */
function mcpManualTopicPaths(contentFilePrefix, language) {
    const prefix = stripTrailingSlash(contentFilePrefix);
    const suffix = language === 'en' ? '.en' : '';
    return {
        index: `${prefix}/index${suffix}.md`,
        principles: `${prefix}/principles${suffix}.md`,
        tools: `${prefix}/tools${suffix}.md`,
        scenarios: `${prefix}/scenarios${suffix}.md`,
        errors: `${prefix}/errors${suffix}.md`,
    };
}
// ---------------------------------------------------------------------------
// Factory functions (selected by call site, not by path inspection)
// ---------------------------------------------------------------------------
/**
 * Build a ManualSpec for a built-in toolset.
 * Calls `builtinManualTopicPaths` internally — no hardcoded path inspection.
 */
function buildBuiltinManualSpec(options) {
    const topics = options?.topics && options.topics.length > 0 ? [...options.topics] : [...exports.MANUAL_TOPICS];
    return {
        topics,
        warnOnMissing: options?.warnOnMissing ?? true,
        topicFilesI18n: {
            en: builtinManualTopicPaths(options?.toolsetId ?? '', 'en'),
            zh: builtinManualTopicPaths(options?.toolsetId ?? '', 'zh'),
        },
    };
}
/**
 * Build a ManualSpec for an MCP toolset.
 * Calls `mcpManualTopicPaths` internally — no hardcoded serverId paths.
 * `contentFilePrefix` is sourced from mcp.yaml `manual.contentFile`.
 */
function buildMcpManualSpec(contentFilePrefix, options) {
    const topics = options?.topics && options.topics.length > 0 ? [...options.topics] : [...exports.MANUAL_TOPICS];
    return {
        topics,
        warnOnMissing: options?.warnOnMissing ?? true,
        topicFilesI18n: {
            en: mcpManualTopicPaths(contentFilePrefix, 'en'),
            zh: mcpManualTopicPaths(contentFilePrefix, 'zh'),
        },
    };
}
function buildRawMcpManualSpec() {
    return {
        topics: ['tools'],
        toolsTopicMode: 'registered-tool-name-list',
        warnOnMissing: false,
    };
}
function stripTrailingSlash(input) {
    return input.replace(/\/+$/g, '');
}
