/** Skills pageindex (markdown tree indexing and retrieval). */
import { SkillsBuilderNative, buildSkillsIndexNative, getSkillContentRetrieveResultNative, getSkillDocumentNative, getSkillLineContentFromSpecNative, getSkillLineContentNative, getSkillStructureNative, loadSkillsIndexFromDirNative, mdToTreeNative, parseSkillChunkIdsNative, parseSkillNodeIdsNative, reconstructSkillMarkdownNative, skillsIndexFromDecomposedDirNative, writeReconstructedSkillNative, writeSkillsIndexNative, } from "./native.js";
import { cohesionConfigToNative, defaultBm25CohesionConfig, } from "./bm25Cohesion.js";
const BM25_FLAT_KEYS = new Set([
    "window_mode",
    "threshold",
    "merge_threshold",
    "chunk_size",
    "token_counter",
    "similarity_window",
    "next_unit_size",
    "skip_window",
    "min_units_per_chunk",
    "minimum_words",
    "minimum_sentences",
    "min_characters_per_sentence",
    "min_characters_per_word",
    "delimiters",
    "include_delim",
    "use_stopwords",
    "filter_window",
    "filter_polyorder",
    "filter_tolerance",
    "stem_language",
]);
export function defaultPageIndexConfig() {
    return {
        ifAddNodeId: true,
        ifAddNodeText: false,
        enableBm25Chunking: true,
        bm25Cohesion: defaultBm25CohesionConfig(),
    };
}
/** Pageindex config with one full-text chunk per node (no BM25 splitting). */
export function pageIndexConfigWithoutChunking() {
    return {
        ...defaultPageIndexConfig(),
        enableBm25Chunking: false,
    };
}
/** Partial pageindex settings from app config; Rust merges unset keys with SDK defaults. */
export function pageIndexConfigFromMapping(mapping) {
    if (mapping == null)
        return undefined;
    if (isSnakeCasePageIndexDict(mapping))
        return mapping;
    return pageIndexConfigToNative(pageIndexConfigFromPartial(mapping));
}
export function pageIndexConfigFromPartial(partial) {
    const cfg = defaultPageIndexConfig();
    if (partial.ifAddNodeId !== undefined)
        cfg.ifAddNodeId = partial.ifAddNodeId;
    if (partial.ifAddNodeText !== undefined)
        cfg.ifAddNodeText = partial.ifAddNodeText;
    if (partial.enableBm25Chunking !== undefined)
        cfg.enableBm25Chunking = partial.enableBm25Chunking;
    if (partial.bm25Cohesion !== undefined) {
        cfg.bm25Cohesion = {
            ...defaultBm25CohesionConfig(),
            ...partial.bm25Cohesion,
        };
    }
    return cfg;
}
export function pageIndexConfigToNative(config) {
    const out = {
        if_add_node_id: config.ifAddNodeId ?? true,
        if_add_node_text: config.ifAddNodeText ?? false,
        enable_bm25_chunking: config.enableBm25Chunking ?? true,
    };
    const cohesion = cohesionConfigToNative(config.bm25Cohesion ?? defaultBm25CohesionConfig());
    if (cohesion !== undefined) {
        out.bm25_cohesion = cohesion;
    }
    return out;
}
function isSnakeCasePageIndexDict(config) {
    return ("if_add_node_id" in config ||
        "if_add_node_text" in config ||
        "enable_bm25_chunking" in config ||
        "bm25_cohesion" in config ||
        "chunk_size" in config ||
        [...BM25_FLAT_KEYS].some((key) => key in config));
}
function resolveNativeConfig(config) {
    if (config == null)
        return undefined;
    if (isSnakeCasePageIndexDict(config)) {
        return config;
    }
    return pageIndexConfigToNative(config);
}
export function buildSkillsIndex(skillDirs, config) {
    return buildSkillsIndexNative(skillDirs, resolveNativeConfig(config));
}
export function writeSkillsIndex(index, outputDir) {
    writeSkillsIndexNative(index, outputDir);
}
export function loadSkillsIndexFromDir(catalogDir) {
    return loadSkillsIndexFromDirNative(catalogDir);
}
export function skillsIndexFromDecomposedDir(dir) {
    return skillsIndexFromDecomposedDirNative(dir);
}
export function mdToTree(markdownContent, sourcePath, config) {
    return mdToTreeNative(markdownContent, sourcePath, resolveNativeConfig(config));
}
export function getSkillDocument(documents, docId) {
    return getSkillDocumentNative(documents, docId);
}
export function getSkillStructure(documents, docId) {
    return getSkillStructureNative(documents, docId);
}
export function getSkillLineContentFromSpec(index, docId, lineNumSpec) {
    return getSkillLineContentFromSpecNative(index, docId, lineNumSpec);
}
function toNativeReconstructOptions(options) {
    if (!options)
        return undefined;
    return { keepAllHeaders: options.keepAllHeaders ?? false };
}
export function getSkillLineContent(index, docId, opts) {
    return getSkillLineContentNative(index, docId, opts?.lineNumSpecs, opts?.nodeIdSpecs, opts?.chunkIdSpecs);
}
export function getSkillContentRetrieveResult(index, docId, opts) {
    return getSkillContentRetrieveResultNative(index, docId, opts?.lineNumSpecs, opts?.nodeIdSpecs, opts?.chunkIdSpecs, toNativeReconstructOptions(opts?.options));
}
export function reconstructSkillMarkdown(index, docId, opts) {
    return reconstructSkillMarkdownNative(index, docId, opts?.lineNumSpecs, opts?.nodeIdSpecs, opts?.chunkIdSpecs, toNativeReconstructOptions(opts?.options));
}
export function writeReconstructedSkill(catalogDir, index, docId, opts) {
    return writeReconstructedSkillNative(catalogDir, index, docId, opts?.lineNumSpecs, opts?.nodeIdSpecs, opts?.chunkIdSpecs, toNativeReconstructOptions(opts?.options));
}
export function parseSkillNodeIds(spec) {
    return parseSkillNodeIdsNative(spec);
}
export function parseSkillChunkIds(spec) {
    return parseSkillChunkIdsNative(spec);
}
export class SkillsBuilder {
    inner;
    constructor(options) {
        this.inner = new SkillsBuilderNative(options?.memoryOnly ?? true, options?.outputDir);
    }
    buildFromDirs(skillDirs, config) {
        return this.inner.buildFromDirs(skillDirs, resolveNativeConfig(config));
    }
    writeCatalog() {
        return this.inner.writeCatalog();
    }
    toSkillsIndexJson() {
        return this.inner.toSkillsIndexJson();
    }
    toSkillsDict() {
        return this.inner.toSkillsDict();
    }
}
//# sourceMappingURL=pageindex.js.map