(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./lean-node-type", "./search-engine"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.NodeTypeParser = void 0;
    const lean_node_type_1 = require("./lean-node-type");
    const search_engine_1 = require("./search-engine");
    class NodeTypeParser {
        constructor(nodeTypes) {
            this.nodeTypes = nodeTypes.map(lean_node_type_1.toLeanNodeType);
            this.searchEngine = new search_engine_1.CodeBuilderNodeSearchEngine(this.nodeTypes);
            this.nodeTypeIndex = this.buildIndex();
        }
        buildIndex() {
            const index = new Map();
            for (const nodeType of this.nodeTypes) {
                const existing = index.get(nodeType.name) ?? [];
                existing.push(nodeType);
                index.set(nodeType.name, existing);
            }
            return index;
        }
        isTriggerNode(nodeType) {
            if (nodeType.group.includes('trigger')) {
                return true;
            }
            return (nodeType.name.toLowerCase().includes('trigger') ||
                nodeType.name.toLowerCase().includes('webhook'));
        }
        searchNodeTypes(query, limit = 5, nodeFilter) {
            const results = this.searchEngine.searchByName(query, limit, nodeFilter);
            return results.map((result) => {
                const nodeType = this.getLeanNodeType(result.name, result.version);
                return {
                    id: result.name,
                    displayName: result.displayName,
                    description: result.description,
                    version: result.version,
                    isTrigger: nodeType ? this.isTriggerNode(nodeType) : false,
                };
            });
        }
        getLeanNodeType(nodeId, version) {
            const versions = this.nodeTypeIndex.get(nodeId);
            if (!versions || versions.length === 0) {
                return null;
            }
            if (version !== undefined) {
                const match = versions.find((v) => {
                    const nodeVersions = Array.isArray(v.version) ? v.version : [v.version];
                    return nodeVersions.includes(version);
                });
                return match ?? null;
            }
            return versions.reduce((latest, current) => {
                const latestMax = Array.isArray(latest.version)
                    ? Math.max(...latest.version)
                    : latest.version;
                const currentMax = Array.isArray(current.version)
                    ? Math.max(...current.version)
                    : current.version;
                return currentMax > latestMax ? current : latest;
            });
        }
        getVersionDiscriminators(nodeId, version) {
            const node = this.getLeanNodeType(nodeId, version);
            return node?.discriminatorsByVersion.get(version) ?? null;
        }
    }
    exports.NodeTypeParser = NodeTypeParser;
});
//# sourceMappingURL=node-type-parser.js.map