"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNewBundleOperators = exports.getNewOperators = void 0;
const getOperatorDependencies = (operatorId, preflightRequirements, dependencies = new Set()) => {
    var _a, _b;
    const originalSize = dependencies.size;
    const deps = ((_b = (_a = preflightRequirements === null || preflightRequirements === void 0 ? void 0 : preflightRequirements.operators) === null || _a === void 0 ? void 0 : _a.find(({ operatorName }) => operatorName === operatorId)) === null || _b === void 0 ? void 0 : _b.dependencies) || [];
    deps.forEach((dep) => dependencies.add(dep));
    if (originalSize !== dependencies.size) {
        // add deps of new deps
        deps.forEach((op) => {
            getOperatorDependencies(op, preflightRequirements, dependencies);
        });
    }
    return [...dependencies];
};
const getNewOperators = (currentOperators, operatorId, preflightRequirements, add, opSpecs) => {
    const dependencies = getOperatorDependencies(operatorId, preflightRequirements);
    if (add) {
        const uniqueOps = new Set([...currentOperators, ...dependencies, operatorId]);
        return Array.from(uniqueOps);
    }
    const newOperators = currentOperators.filter((op) => op !== operatorId);
    const notStandaloneDeps = dependencies
        .filter((dep) => { var _a; return (_a = opSpecs[dep]) === null || _a === void 0 ? void 0 : _a.notStandalone; })
        .filter((dep) => {
        const hasDependency = newOperators.some((op) => getOperatorDependencies(op, preflightRequirements).includes(dep));
        if (!hasDependency) {
            newOperators.splice(newOperators.indexOf(dep), 1);
        }
        return !hasDependency;
    });
    return currentOperators.filter((op) => op !== operatorId && !notStandaloneDeps.includes(op));
};
exports.getNewOperators = getNewOperators;
const getBundleOperators = (bundle, preflightRequirements) => {
    var _a;
    const bundleOperators = new Set(bundle.operators || []);
    (_a = bundle.operators) === null || _a === void 0 ? void 0 : _a.forEach((op) => {
        getOperatorDependencies(op, preflightRequirements).forEach((dep) => bundleOperators.add(dep));
    });
    return Array.from(bundleOperators);
};
const getNewBundleOperators = (currentOperators, currentBundles, allBundles, newBundle, preflightRequirements, add) => {
    const newBundleOperators = getBundleOperators(newBundle, preflightRequirements);
    if (add) {
        const combined = [...currentOperators, ...newBundleOperators];
        return Array.from(new Set(combined));
    }
    const newBundles = currentBundles.filter((b) => b !== newBundle.id);
    const operatorsToKeep = [
        ...newBundles.reduce((acc, bundleId) => {
            const bundle = allBundles.find(({ id }) => id === bundleId);
            if (bundle) {
                const bundleOperators = getBundleOperators(bundle, preflightRequirements);
                bundleOperators.forEach((op) => {
                    acc.add(op);
                });
            }
            return acc;
        }, new Set()),
    ];
    const operatorsToRemove = newBundleOperators.filter((op) => !operatorsToKeep.includes(op));
    return currentOperators.filter((op) => !operatorsToRemove.includes(op));
};
exports.getNewBundleOperators = getNewBundleOperators;
//# sourceMappingURL=utils.js.map