"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveSubgraph = moveSubgraph;
const G_1 = require("../../spec/actions/G");
const clone_1 = require("../../util/clone");
const object_1 = require("../../util/object");
function moveSubgraph(source, target, map, moves, reverse) {
    let units = {};
    const actions = [];
    for (const move of moves) {
        if (move.in) {
            if (!reverse) {
                actions.push((0, clone_1.clone)(move.action));
            }
            if (move.action.type === G_1.ADD_UNIT_SPEC) {
                const { unitId, bundle } = move.action.data;
                const unit_ = units[unitId];
                target.addUnit(unitId, unit_, bundle, undefined, false, false, false);
            }
            else {
                target.act(move.action, false, false, false);
            }
        }
        else {
            if (reverse) {
                actions.push((0, clone_1.clone)(move.action));
            }
            if (move.action.type === G_1.REMOVE_UNIT) {
                const { unitId } = move.action.data;
                const nextUnitId = (0, object_1.deepGetOrDefault)(map, ['unit', unitId, 'in', 'unit', 'unitId'], unitId);
                const unit = source.removeUnit(unitId, false, false, false, false, false);
                units[nextUnitId] = unit;
            }
            else {
                source.act(move.action, false, false, false);
            }
        }
    }
    if (reverse) {
        source.emit('bulk_edit', actions, []);
    }
    else {
        target.emit('bulk_edit', actions, []);
    }
}
//# sourceMappingURL=moveSubgraph.js.map