import { CatalogIndex } from "./build.js";
import { DecomposedCatalog } from "./decomposed-catalog.js";
import { chunkSurvivorKeyNative, loadCatalogNative, removedChunksNative, retrieveCoreNative, retrieveToolsNative, } from "./native.js";
import { isJsonRecord } from "./types.js";
export { DecomposedCatalog };
export function loadCatalog(dirPath) {
    return loadCatalogNative(dirPath);
}
/** Normalized identity for a catalog chunk (`json` or `md` array item). */
export function chunkSurvivorKey(item, section) {
    return chunkSurvivorKeyNative(item, section);
}
/**
 * Decomposed chunks in `fullCatalog` that are not in `surviving` (same shape as survivors.json).
 */
export function removedChunks(fullCatalog, surviving, options = {}) {
    const { applyDecomposedScoreFilter = false } = options;
    return removedChunksNative(fullCatalog, surviving, applyDecomposedScoreFilter);
}
function catalogToNative(catalog) {
    if (catalog instanceof CatalogIndex) {
        return { tools: catalog.tools, files: catalog.files };
    }
    if (catalog instanceof DecomposedCatalog) {
        return catalog.toJsonFiles();
    }
    return catalog;
}
function storeJsonFilesFromCatalog(catalog) {
    if (catalog instanceof CatalogIndex) {
        return catalog.files;
    }
    if (catalog instanceof DecomposedCatalog) {
        return catalog.toJsonFiles();
    }
    return {};
}
function retrieveToolsViaPolicyOptions(catalogDict, catalog, applyDecomposedScoreFilter, policyOptions) {
    const survivor = DecomposedCatalog.fromCatalogDict(catalogDict);
    return retrieveCoreNative(catalogDict, storeJsonFilesFromCatalog(catalog), survivor.toJsonFiles(), applyDecomposedScoreFilter, policyOptions);
}
function retrieveToolsViaContext(catalogDict, catalog, applyDecomposedScoreFilter, preserveValues, ctx) {
    const preserveList = preserveValues == null ? undefined : [...preserveValues].sort();
    return retrieveToolsNative(catalogDict, catalogToNative(catalog), applyDecomposedScoreFilter, preserveList, ctx ?? undefined);
}
export function retrieveTools(data, options) {
    const { catalog, applyDecomposedScoreFilter = false, preserveValues, ctx = null, policyOptions = null, } = options;
    const catalogDict = isJsonRecord(data) ? data : {};
    if (policyOptions != null && ctx == null) {
        return retrieveToolsViaPolicyOptions(catalogDict, catalog, applyDecomposedScoreFilter, policyOptions);
    }
    return retrieveToolsViaContext(catalogDict, catalog, applyDecomposedScoreFilter, preserveValues, ctx);
}
//# sourceMappingURL=retrieve.js.map