"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResolverService = void 0;
const inversify_1 = require("inversify");
const core_js_1 = require("../core.js");
const engine_js_1 = require("../engine.js");
const index_js_1 = require("../model/index.js");
const index_js_2 = require("../util/index.js");
let ResolverService = class ResolverService {
    constructor(engine) {
        this.engine = engine;
        this.extensions = [];
        this.actionTypeMap = null;
        this.actionCategories = null;
        this.pipeTypeMap = null;
        this.pipeCategories = null;
        core_js_1.coreExtension.init(this.engine);
    }
    getExtensions() {
        return [...this.extensions];
    }
    addExtension(ext) {
        this.removeExtension(ext);
        this.extensions.push(ext);
        ext.init(this.engine);
        this.invalidate();
    }
    removeExtension(ext) {
        const existing = this.extensions.filter(_ => _.spec.name === ext.spec.name);
        for (const ext of existing) {
            ext.destroy();
        }
        this.extensions = this.extensions.filter(_ => !existing.includes(_));
        this.invalidate();
    }
    purgeExtensions() {
        for (const ext of this.extensions) {
            ext.destroy();
        }
        this.extensions = [];
        this.invalidate();
    }
    getActionClass(type) {
        const index = this.getActionIndex();
        return index.get(type) || index_js_1.UnresolvedAction;
    }
    getPipeClass(type) {
        const index = this.getPipeIndex();
        return index.get(type) || index_js_1.UnresolvedPipe;
    }
    getActionIndex() {
        if (!this.actionTypeMap) {
            this.reindex();
        }
        return this.actionTypeMap;
    }
    getActionCategories() {
        if (!this.actionCategories) {
            this.reindex();
        }
        return this.actionCategories;
    }
    getPipeIndex() {
        if (!this.pipeTypeMap) {
            this.reindex();
        }
        return this.pipeTypeMap;
    }
    getPipeCategories() {
        if (!this.pipeCategories) {
            this.reindex();
        }
        return this.pipeCategories;
    }
    getInspectionClasses() {
        return this.getAllExtensions().flatMap(ext => ext.inspectionClasses);
    }
    *unmetDependencies(requirements) {
        for (const required of requirements) {
            const ext = this.extensions.find(_ => _.spec.name === required.name);
            const satisfied = ext?.isVersionWithinRange(required.version);
            if (!satisfied) {
                yield {
                    name: required.name,
                    version: required.version,
                    existingVersion: ext?.spec.version ?? null,
                };
            }
        }
    }
    invalidate() {
        this.actionTypeMap = null;
        this.pipeTypeMap = null;
        this.actionCategories = null;
        this.pipeCategories = null;
    }
    reindex() {
        this.actionTypeMap = new Map();
        this.pipeTypeMap = new Map();
        for (const extension of this.getAllExtensions()) {
            for (const ActionClass of extension.actionClasses) {
                ActionClass.$extension = extension;
                this.actionTypeMap.set(ActionClass.$type, ActionClass);
            }
            for (const PipeClass of extension.pipeClasses) {
                PipeClass.$extension = extension;
                this.pipeTypeMap.set(PipeClass.$type, PipeClass);
            }
        }
        this.actionCategories = this.buildCategories([...this.actionTypeMap.values()]);
        this.pipeCategories = this.buildCategories([...this.pipeTypeMap.values()]);
    }
    getAllExtensions() {
        return [core_js_1.coreExtension, ...this.extensions];
    }
    buildCategories(items) {
        return (0, index_js_2.groupBy)(items, cl => cl.$metadata.ns)
            .map(_ => {
            return {
                name: _[0],
                items: _[1].sort(by(_ => _.$type)),
            };
        })
            .sort(by(_ => _.name));
    }
};
ResolverService = __decorate([
    (0, inversify_1.injectable)(),
    __param(0, (0, inversify_1.inject)(engine_js_1.Engine)),
    __metadata("design:paramtypes", [engine_js_1.Engine])
], ResolverService);
exports.ResolverService = ResolverService;
function by(fn) {
    return (a, b) => (fn(a) > fn(b) ? 1 : -1);
}
//# sourceMappingURL=resolver.js.map