"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.reverseAction = void 0;
const C_1 = require("./actions/C");
const D_1 = require("./actions/D");
const G_1 = require("./actions/G");
const U_1 = require("./actions/U");
const reverseSelection_1 = require("./reverseSelection");
const reverseAction = ({ type, data }) => {
    switch (type) {
        case G_1.ADD_UNIT_SPEC:
            return (0, G_1.makeRemoveUnitAction)(data.unitId, data.bundle, data.merges, data.plugs, data.parentId, data.parentSlot, data.parentIndex, data.children, data.childrenSlot, data.position, data.pinPosition, data.layoutPosition);
        case G_1.REMOVE_UNIT:
            return (0, G_1.makeAddUnitAction)(data.unitId, data.bundle, data.merges, data.plugs, data.parentId, data.parentSlot, data.parentIndex, data.children, data.childrenSlot, data.position, data.pinPosition, data.layoutPosition);
        case G_1.ADD_MERGE:
            return (0, G_1.makeRemoveMergeAction)(data.mergeId, data.mergeSpec, data.position);
        case G_1.ADD_PIN_TO_MERGE:
            return (0, G_1.makeRemovePinFromMergeAction)(data.mergeId, data.unitId, data.type, data.pinId);
        case G_1.REMOVE_MERGE:
            return (0, G_1.makeAddMergeAction)(data.mergeId, data.mergeSpec, data.position);
        case G_1.REMOVE_PIN_FROM_MERGE:
            return (0, G_1.makeAddPinToMergeAction)(data.mergeId, data.unitId, data.type, data.pinId);
        case G_1.EXPOSE_PIN_SET:
            return (0, G_1.makeCoverPinSetAction)(data.type, data.pinId, data.pinSpec);
        case G_1.EXPOSE_PIN:
            return (0, G_1.makeCoverPinAction)(data.type, data.pinId, data.subPinId, data.subPinSpec, data.pinSpec);
        case G_1.COVER_PIN_SET:
            return (0, G_1.makeExposePinSetAction)(data.type, data.pinId, data.pinSpec);
        case G_1.COVER_PIN:
            return (0, G_1.makeExposePinAction)(data.type, data.pinId, data.subPinId, data.subPinSpec);
        case G_1.PLUG_PIN:
            return (0, G_1.makeUnplugPinAction)(data.type, data.pinId, data.subPinId, data.subPinSpec);
        case G_1.UNPLUG_PIN:
            return (0, G_1.makePlugPinAction)(data.type, data.pinId, data.subPinId, data.subPinSpec);
        case G_1.SET_UNIT_PIN_CONSTANT:
            return (0, G_1.makeSetUnitPinConstantAction)(data.unitId, data.type, data.pinId, !data.constant);
        case G_1.SET_UNIT_PIN_DATA:
            return (0, G_1.makeRemoveUnitPinDataAction)(data.unitId, data.type, data.pinId, data.data);
        case G_1.REMOVE_UNIT_PIN_DATA:
            return (0, G_1.makeSetUnitPinDataAction)(data.unitId, data.type, data.pinId, data.data);
        case G_1.SET_UNIT_PIN_IGNORED:
            return (0, G_1.makeSetUnitPinIgnoredAction)(data.unitId, data.type, data.pinId, !data.ignored);
        case G_1.EXPOSE_UNIT_PIN_SET:
            return (0, G_1.makeCoverUnitPinSetAction)(data.unitId, data.type, data.pinId, data.pinSpec);
        case G_1.COVER_UNIT_PIN_SET:
            return (0, G_1.makeExposeUnitPinSetAction)(data.unitId, data.type, data.pinId, data.pinSpec);
        case C_1.MOVE_SUB_COMPONENT_ROOT: {
            const data_ = data;
            const parentId = data_.prevParentIdMap[data_.children[0]] ?? null;
            const prevParentIdMap = {};
            const slotMap = data_.prevSlotMap;
            const prevSlotMap = data_.slotMap;
            const index = data_.index;
            const children = data_.children;
            for (const childId of data.children) {
                prevParentIdMap[childId] = data_.parentId;
            }
            return (0, C_1.makeMoveSubComponentRootAction)(parentId, prevParentIdMap, children, index, slotMap, prevSlotMap);
        }
        case C_1.REORDER_SUB_COMPONENT:
            return (0, C_1.makeReorderSubComponentAction)(data.parentId, data.childId, data.to, data.from);
        case G_1.MOVE_SUBGRAPH_INTO: {
            const data_ = data;
            const { selection, mapping } = (0, reverseSelection_1.reverseSelection)(data_.selection, data_.mapping);
            const moves = [...data_.moves].reverse().map((move) => ({
                in: !move.in,
                action: (0, exports.reverseAction)(move.action),
            }));
            return (0, G_1.makeMoveSubgraphOutOfAction)(data_.graphId, data_.spec, selection, mapping, moves);
        }
        case G_1.MOVE_SUBGRAPH_OUT_OF: {
            const data_ = data;
            const { selection, mapping } = (0, reverseSelection_1.reverseSelection)(data_.selection, data_.mapping);
            const moves = [...data_.moves].reverse().map((move) => ({
                in: !move.in,
                action: (0, exports.reverseAction)(move.action),
            }));
            return (0, G_1.makeMoveSubgraphIntoAction)(data_.graphId, data_.spec, selection, mapping, moves);
        }
        case G_1.BULK_EDIT:
            return (0, G_1.makeBulkEditAction)([...data.actions].reverse().map(exports.reverseAction));
        case G_1.SET_COMPONENT_SIZE:
            return (0, G_1.makeSetComponentSizeAction)(data.prevWidth, data.prevHeight, data.width, data.height);
        case G_1.SET_SUB_COMPONENT_SIZE:
            return (0, G_1.makeSetSubComponentSizeAction)(data.unitId, data.prevWidth, data.prevHeight, data.width, data.height);
        case G_1.SET_UNIT_SIZE:
            return (0, G_1.makeSetUnitSizeAction)(data.unitId, data.prevWidth, data.prevHeight, data.width, data.height);
        case D_1.ADD_DATUM:
            return (0, D_1.makeRemoveDatumAction)(data.id, data.value);
        case D_1.SET_DATUM:
            return (0, D_1.makeSetDatumAction)(data.id, data.value, data.prevValue);
        case D_1.REMOVE_DATUM:
            return (0, D_1.makeAddDatumAction)(data.id, data.value);
        case D_1.ADD_DATUM_LINK:
            return (0, D_1.makeRemoveDatumLinkAction)(data.id, data.value, data.nodeSpec);
        case D_1.REMOVE_DATUM_LINK:
            return (0, D_1.makeAddDatumLinkAction)(data.id, data.value, data.nodeSpec);
        case G_1.SET_MERGE_DATA:
            return (0, G_1.makeRemoveMergeDataAction)(data.mergeId, data.data);
        case G_1.REMOVE_MERGE_DATA:
            return (0, G_1.makeSetMergeDataAction)(data.mergeId, data.data);
        case G_1.SET_PLUG_DATA:
            return (0, G_1.makeRemovePlugDataAction)(data.type, data.pinId, data.subPinId, data.data);
        case G_1.REMOVE_PLUG_DATA:
            return (0, G_1.makeSetPlugDataAction)(data.type, data.pinId, data.subPinId, data.data, undefined);
        case G_1.SET_PIN_SET_ID:
            return (0, G_1.makeSetPinSetIdAction)(data.type, data.newPinId, data.pinId);
        case G_1.SET_PIN_SET_FUNCTIONAL:
            return (0, G_1.makeSetPinSetFunctionalAction)(data.type, data.pinId, !data.functional);
        case G_1.SET_PIN_SET_DEFAULT_IGNORED:
            return (0, G_1.makeSetPinSetFunctionalAction)(data.type, data.pinId, !data.defaultIgnored);
        case G_1.SET_UNIT_ID:
            return (0, G_1.makeSetUnitIdAction)(data.newUnitId, data.unitId, data.lastName, data.name, data.lastSpecId, data.specId);
        case U_1.SET_PIN_DATA:
            return (0, U_1.makeSetPinDataAction)(data.type, data.pinId, data.lastData, data.data);
        default:
            throw new Error('irreversible');
    }
};
exports.reverseAction = reverseAction;
//# sourceMappingURL=reverseAction.js.map