"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reverseSelection = void 0;
const deepSet_1 = require("../deepSet");
const object_1 = require("../util/object");
const reverseSelection = (selection, mapping) => {
    const selection_ = {
        unit: [],
        link: [],
        plug: [],
        merge: [],
    };
    const mapping_ = {
        unit: {},
        merge: {},
        link: {},
        plug: {},
    };
    for (const unitId of selection.unit ?? []) {
        const nextUnitId = (0, object_1.deepGetOrDefault)(mapping, ['unit', unitId, 'in', 'unit', 'unitId'], unitId);
        if (nextUnitId) {
            selection_.unit.push(nextUnitId);
            (0, deepSet_1.deepSet_)(mapping_, ['unit', nextUnitId, 'in', 'unit'], { unitId });
        }
    }
    for (const mergeId of selection.merge ?? []) {
        const nextMergeId = (0, object_1.deepGetOrDefault)(mapping, ['merge', mergeId, 'in', 'merge', 'mergeId'], undefined);
        if (nextMergeId) {
            selection_.merge.push(nextMergeId);
            (0, deepSet_1.deepSet_)(mapping_, ['merge', nextMergeId, 'in', 'merge', 'mergeId'], mergeId);
        }
    }
    for (const { unitId, type, pinId } of selection.link ?? []) {
        const nextUnitId = (0, object_1.deepGetOrDefault)(mapping, ['unit', unitId, 'in', 'unit', 'unitId'], unitId);
        const nextMergeId = (0, object_1.deepGetOrDefault)(mapping, ['link', unitId, type, pinId, 'in', 'merge', 'mergeId'], undefined);
        const template = (0, object_1.deepGetOrDefault)(mapping, ['link', unitId, type, pinId, 'in', 'link', 'template'], false);
        if (nextMergeId || template) {
            //
        }
        else {
            selection_.link.push({ unitId: nextUnitId, type, pinId });
        }
    }
    for (const { type, pinId, subPinId } of selection.plug ?? []) {
        const nextPlug = (0, object_1.deepGetOrDefault)(mapping, ['plug', type, pinId, subPinId, 'in', 'plug', type], undefined);
        const pin = (0, object_1.deepGetOrDefault)(mapping, ['plug', type, pinId, subPinId, 'in', 'link'], undefined);
        const mergeId = (0, object_1.deepGetOrDefault)(mapping, ['plug', type, pinId, subPinId, 'in', 'merge', 'mergeId'], undefined);
        if (nextPlug) {
            selection_.plug.push(nextPlug);
            (0, deepSet_1.deepSet_)(mapping_, [
                'plug',
                nextPlug.type,
                nextPlug.pinId,
                nextPlug.subPinId,
                'in',
                'plug',
                nextPlug.type,
            ], { type, pinId, subPinId });
        }
        if (pin) {
            selection_.link.push(pin);
        }
        if (mergeId) {
            if (!selection_.merge.includes(mergeId)) {
                selection_.merge.push(mergeId);
            }
        }
    }
    return { selection: selection_, mapping: mapping_ };
};
exports.reverseSelection = reverseSelection;
//# sourceMappingURL=reverseSelection.js.map