"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assert = require("assert");
const Graph_1 = require("../../Class/Graph");
const debug_1 = require("../../debug");
const _ids_1 = require("../../system/_ids");
const id_1 = require("../../util/id");
const system_1 = require("../util/system");
const UNIT_ID_EMTPY = 'empty';
const UNIT_ID_IDENTITY = 'identity';
const UNIT_ID_IDENTITY_0 = 'identity0';
const UNIT_ID_IDENTITY_1 = 'identity1';
const UNIT_ID_RANDOM_COLOR_BOX = 'randomcolorbox';
const spec0 = system_1.system.newSpec({});
const composition0 = new Graph_1.Graph(spec0, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition0);
false && (0, debug_1.watchGraphAndLog)(composition0);
composition0.addUnitSpec(UNIT_ID_RANDOM_COLOR_BOX, {
    unit: { id: _ids_1.ID_RANDOM_COLOR_BOX },
});
composition0.moveSubgraphOutOf(UNIT_ID_RANDOM_COLOR_BOX, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: ['box'],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {
        ['box']: 'box',
    },
    plug: {},
}, {}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition0.getUnitCount(), 2);
const spec1 = system_1.system.newSpec({
    units: {
        [UNIT_ID_EMTPY]: {
            id: _ids_1.ID_EMPTY,
        },
        [UNIT_ID_IDENTITY]: {
            id: _ids_1.ID_IDENTITY,
        },
        [UNIT_ID_IDENTITY_0]: {
            id: _ids_1.ID_IDENTITY,
        },
    },
    merges: {
        0: {
            [UNIT_ID_IDENTITY]: {
                output: {
                    a: true,
                },
            },
            [UNIT_ID_IDENTITY_0]: {
                input: {
                    a: true,
                },
            },
        },
    },
});
const composition1 = new Graph_1.Graph(spec1, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition1);
false && (0, debug_1.watchGraphAndLog)(composition1);
composition1.moveSubgraphInto(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [UNIT_ID_IDENTITY_0],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {
        [UNIT_ID_IDENTITY_0]: UNIT_ID_IDENTITY_0,
    },
    plug: {},
}, {
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: {
                pinId: 'a',
                subPinId: '0',
                mergeId: '0',
                merge: { [UNIT_ID_IDENTITY]: { output: { a: true } } },
            },
        },
        output: { a: { pinId: 'a', subPinId: '0' } },
    },
}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition1.getUnitCount(), 2);
assert.equal(composition1.getMergeCount(), 1);
assert.deepEqual(composition1.getMergeSpec('0'), {
    [UNIT_ID_EMTPY]: {
        input: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
});
composition1.moveSubgraphOutOf(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [UNIT_ID_IDENTITY_0],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {
        [UNIT_ID_IDENTITY_0]: UNIT_ID_IDENTITY_0,
    },
    plug: {},
}, {
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: {
                pinId: 'a',
                subPinId: '0',
                mergeId: '0',
                merge: { [UNIT_ID_IDENTITY]: { output: { a: true } } },
            },
        },
        output: { a: { pinId: 'a', subPinId: '0' } },
    },
}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition1.getUnitCount(), 3);
assert.equal(composition1.getMergeCount(), 1);
assert.deepEqual(composition1.getMergeSpec('0'), {
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: true,
        },
    },
});
const spec2 = system_1.system.newSpec({
    units: {
        [UNIT_ID_EMTPY]: {
            id: _ids_1.ID_EMPTY,
        },
        [UNIT_ID_IDENTITY]: {
            id: _ids_1.ID_IDENTITY,
        },
        [UNIT_ID_IDENTITY_0]: {
            id: _ids_1.ID_IDENTITY,
        },
    },
    merges: {
        0: {
            [UNIT_ID_IDENTITY]: {
                output: {
                    a: true,
                },
            },
            [UNIT_ID_IDENTITY_0]: {
                input: {
                    a: true,
                },
            },
        },
    },
});
const composition2 = new Graph_1.Graph(spec2, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition2);
false && (0, debug_1.watchGraphAndLog)(composition2);
composition2.moveSubgraphInto(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [{ unitId: UNIT_ID_IDENTITY, type: 'input', pinId: 'a' }],
    unit: [],
    plug: [],
}, {
    merge: {},
    link: {
        [UNIT_ID_IDENTITY]: {
            input: {
                a: {
                    mergeId: '1',
                    oppositePinId: 'a',
                },
            },
            output: {
                a: {
                    mergeId: null,
                    oppositePinId: null,
                },
            },
        },
    },
    unit: {},
    plug: {},
}, {}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition2.getUnitCount(), 3);
assert.equal(composition2.getMergeCount(), 2);
assert.deepEqual(composition2.getMergeSpec('1'), {
    [UNIT_ID_EMTPY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY]: {
        input: {
            a: true,
        },
    },
});
composition2.moveSubgraphOutOf(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [{ unitId: UNIT_ID_IDENTITY, type: 'input', pinId: 'a' }],
    unit: [],
    plug: [],
}, {
    merge: {},
    link: {
        [UNIT_ID_IDENTITY]: {
            input: {
                a: {
                    mergeId: '1',
                    oppositePinId: 'a',
                },
            },
            output: {
                a: {
                    mergeId: null,
                    oppositePinId: null,
                },
            },
        },
    },
    unit: {},
    plug: {},
}, {}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition2.getUnitCount(), 3);
assert.equal(composition2.getMergeCount(), 1);
const spec3 = system_1.system.newSpec({
    units: {
        [UNIT_ID_EMTPY]: {
            id: _ids_1.ID_EMPTY,
        },
        [UNIT_ID_IDENTITY]: {
            id: _ids_1.ID_IDENTITY,
        },
        [UNIT_ID_IDENTITY_0]: {
            id: _ids_1.ID_IDENTITY,
        },
    },
    merges: {
        0: {
            [UNIT_ID_IDENTITY]: {
                output: {
                    a: true,
                },
            },
            [UNIT_ID_IDENTITY_0]: {
                input: {
                    a: true,
                },
            },
        },
    },
});
const composition3 = new Graph_1.Graph(spec3, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition3);
false && (0, debug_1.watchGraphAndLog)(composition3);
composition3.moveSubgraphInto(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: ['0'],
    link: [],
    unit: [],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {}, {
    '0': {
        input: {
            mergeId: '2',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
        output: {
            mergeId: '3',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
    },
}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition3.getUnitCount(), 3);
assert.equal(composition3.getMergeCount(), 2);
assert.deepEqual(composition3.getMergeSpec('2'), {
    [UNIT_ID_EMTPY]: {
        input: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
});
assert.deepEqual(composition3.getMergeSpec('3'), {
    [UNIT_ID_EMTPY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: true,
        },
    },
});
const empty4 = composition3.getGraph(UNIT_ID_EMTPY);
assert.deepEqual(empty4.getMergeSpec('0'), {});
assert.deepEqual(empty4.getPlugSpec('input', 'a', '0'), {
    mergeId: '0',
});
assert.deepEqual(empty4.getPlugSpec('output', 'a', '0'), {
    mergeId: '0',
});
composition3.moveSubgraphOutOf(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: ['0'],
    link: [],
    unit: [],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {}, {
    '0': {
        input: {
            mergeId: '2',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
        output: {
            mergeId: '3',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
    },
}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition3.getUnitCount(), 3);
assert.equal(composition3.getMergeCount(), 1);
assert.deepEqual(composition3.getMergeSpec('0'), {
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: true,
        },
    },
});
const spec4 = system_1.system.newSpec({
    units: {
        [UNIT_ID_EMTPY]: {
            id: _ids_1.ID_EMPTY,
        },
        [UNIT_ID_IDENTITY]: {
            id: _ids_1.ID_IDENTITY,
        },
        [UNIT_ID_IDENTITY_0]: {
            id: _ids_1.ID_IDENTITY,
        },
    },
    merges: {
        0: {
            [UNIT_ID_IDENTITY]: {
                output: {
                    a: true,
                },
            },
            [UNIT_ID_IDENTITY_0]: {
                input: {
                    a: true,
                },
            },
        },
    },
});
const composition4 = new Graph_1.Graph(spec4, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition4);
false && (0, debug_1.watchGraphAndLog)(composition4);
composition4.moveSubgraphInto(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: ['0'],
    link: [],
    unit: [],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {}, {
    '0': {
        input: {
            mergeId: '2',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
        output: {
            mergeId: '3',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
    },
}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition4.getUnitCount(), 3);
assert.equal(composition4.getMergeCount(), 2);
assert.deepEqual(composition4.getMergeSpec('2'), {
    [UNIT_ID_EMTPY]: {
        input: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
});
assert.deepEqual(composition4.getMergeSpec('3'), {
    [UNIT_ID_EMTPY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: true,
        },
    },
});
const empty3 = composition4.getGraph(UNIT_ID_EMTPY);
assert.deepEqual(empty3.getMergeSpec('0'), {});
assert.deepEqual(empty3.getPlugSpec('input', 'a', '0'), {
    mergeId: '0',
});
assert.deepEqual(empty3.getPlugSpec('output', 'a', '0'), {
    mergeId: '0',
});
composition4.moveSubgraphOutOf(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: ['0'],
    link: [],
    unit: [],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {}, {
    '0': {
        input: {
            mergeId: '2',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
        output: {
            mergeId: '3',
            pinId: 'a',
            subPinSpec: {
                mergeId: '0',
            },
        },
    },
}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition4.getUnitCount(), 3);
assert.equal(composition4.getMergeCount(), 1);
assert.deepEqual(composition4.getMergeSpec('0'), {
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: true,
        },
    },
});
const spec5 = system_1.system.newSpec({
    units: {
        [UNIT_ID_EMTPY]: {
            id: _ids_1.ID_EMPTY,
        },
        [UNIT_ID_IDENTITY]: {
            id: _ids_1.ID_IDENTITY,
        },
        [UNIT_ID_IDENTITY_0]: {
            id: _ids_1.ID_IDENTITY,
        },
    },
    merges: {
        0: {
            [UNIT_ID_IDENTITY]: {
                output: {
                    a: true,
                },
            },
            [UNIT_ID_IDENTITY_0]: {
                input: {
                    a: true,
                },
            },
        },
    },
    inputs: {
        a: {
            plug: {
                0: {
                    unitId: UNIT_ID_IDENTITY,
                    pinId: 'a',
                },
            },
        },
    },
});
const composition5 = new Graph_1.Graph(spec5, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition5);
false && (0, debug_1.watchGraphAndLog)(composition5);
composition5.moveSubgraphInto(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [UNIT_ID_IDENTITY, UNIT_ID_IDENTITY_0],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {
    [UNIT_ID_IDENTITY]: {
        input: { a: { pinId: 'a', subPinId: '0' } },
        output: { a: { pinId: 'a0', subPinId: '0', mergeId: '0' } },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: { a: { pinId: 'a0', subPinId: '0', mergeId: '0' } },
        output: { a: { pinId: 'a', subPinId: '0' } },
    },
}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition5.getUnitCount(), 1);
assert.equal(composition5.getMergeCount(), 1);
assert.deepEqual(composition5.getPlugSpec('input', 'a', '0'), {
    unitId: 'empty',
    pinId: 'a',
});
const empty5 = composition5.getGraph(UNIT_ID_EMTPY);
composition5.moveSubgraphOutOf(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [UNIT_ID_IDENTITY, UNIT_ID_IDENTITY_0],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {
    [UNIT_ID_IDENTITY]: {
        input: { a: { pinId: 'a', subPinId: '0' } },
        output: { a: { pinId: 'a0', subPinId: '0', mergeId: '0' } },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: { a: { pinId: 'a0', subPinId: '0', mergeId: '0' } },
        output: { a: { pinId: 'a', subPinId: '0' } },
    },
}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition5.getUnitCount(), 3);
assert.equal(composition5.getMergeCount(), 1);
assert.deepEqual(composition5.getMergeSpec('0'), {
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: true,
        },
    },
});
assert.deepEqual(composition5.getPlugSpec('input', 'a', '0'), {
    unitId: 'identity',
    pinId: 'a',
});
const spec6 = system_1.system.newSpec({
    units: {
        [UNIT_ID_EMTPY]: {
            id: _ids_1.ID_EMPTY,
        },
        [UNIT_ID_IDENTITY]: {
            id: _ids_1.ID_IDENTITY,
        },
        [UNIT_ID_IDENTITY_0]: {
            id: _ids_1.ID_IDENTITY,
        },
        [UNIT_ID_IDENTITY_1]: {
            id: _ids_1.ID_IDENTITY,
        },
    },
    merges: {
        0: {
            [UNIT_ID_IDENTITY]: {
                output: {
                    a: true,
                },
            },
            [UNIT_ID_IDENTITY_0]: {
                input: {
                    a: true,
                },
            },
        },
        1: {
            [UNIT_ID_IDENTITY_0]: {
                output: {
                    a: true,
                },
            },
            [UNIT_ID_IDENTITY_1]: {
                input: {
                    a: true,
                },
            },
        },
    },
    inputs: {
        a: {
            plug: {
                0: {
                    unitId: UNIT_ID_IDENTITY,
                    pinId: 'a',
                },
            },
        },
    },
});
const composition6 = new Graph_1.Graph(spec6, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition6);
false && (0, debug_1.watchGraphAndLog)(composition6);
composition6.moveSubgraphInto(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [UNIT_ID_IDENTITY_0],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {
    [UNIT_ID_IDENTITY_0]: {
        input: { a: { pinId: 'a', subPinId: '0', mergeId: '0' } },
        output: { a: { pinId: 'a', subPinId: '0', mergeId: '1' } },
    },
}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition6.getUnitCount(), 3);
assert.equal(composition6.getMergeCount(), 2);
const empty6 = composition6.getGraph(UNIT_ID_EMTPY);
composition6.moveSubgraphOutOf(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [UNIT_ID_IDENTITY_0],
    plug: [],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: {
                pinId: 'a',
                subPinId: '0',
                mergeId: '0',
                merge: { [UNIT_ID_IDENTITY]: { output: { a: true } } },
            },
        },
        output: {
            a: {
                pinId: 'a',
                subPinId: '0',
                mergeId: '1',
                merge: { [UNIT_ID_IDENTITY_1]: { input: { a: true } } },
            },
        },
    },
}, {}, {
    input: {},
    output: {},
}, {}, {});
assert.equal(composition6.getUnitCount(), 4);
assert.equal(composition6.getMergeCount(), 2);
assert.deepEqual(composition6.getMergeSpec('0'), {
    [UNIT_ID_IDENTITY]: {
        output: {
            a: true,
        },
    },
    [UNIT_ID_IDENTITY_0]: {
        input: {
            a: true,
        },
    },
});
const spec7 = system_1.system.newSpec({
    units: {
        [UNIT_ID_EMTPY]: {
            id: _ids_1.ID_EMPTY,
        },
    },
    merges: {},
    inputs: {
        a: {
            plug: {
                0: {},
            },
        },
    },
});
const composition7 = new Graph_1.Graph(spec7, {}, system_1.system);
false && (0, debug_1.watchUnitAndLog)(composition7);
false && (0, debug_1.watchGraphAndLog)(composition7);
composition7.moveSubgraphInto(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [],
    plug: [{ type: 'input', pinId: 'a', subPinId: '0' }],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {}, {}, {
    input: {
        a: {
            '0': {},
        },
    },
}, {}, {});
assert.equal(composition7.getUnitCount(), 1);
assert.equal(composition7.getMergeCount(), 0);
const empty7 = composition7.getGraph(UNIT_ID_EMTPY);
assert.equal(empty7.getInputCount(), 1);
composition7.moveSubgraphOutOf(UNIT_ID_EMTPY, null, (0, id_1.uuid)(), {
    merge: [],
    link: [],
    unit: [],
    plug: [{ type: 'input', pinId: 'a', subPinId: '0' }],
}, {
    merge: {},
    link: {},
    unit: {},
    plug: {},
}, {}, {}, {
    input: {
        a: {
            '0': {},
        },
    },
}, {}, {});
assert.equal(empty7.getInputCount(), 0);
assert.equal(composition7.getInputCount(), 1);
assert.equal(composition7.getUnitCount(), 1);
assert.equal(composition7.getMergeCount(), 0);
//# sourceMappingURL=moveSubgraphOutOf.js.map