"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@jsonforms/core");
var _ = require("lodash");
var util_1 = require("../helpers/util");
exports.Types = {
    TREE_DND: 'tree-master-detail-DnD'
};
/**
 * The delay in milliseconds before D&D related CSS (e.g. highlighting valid drop targets) is
 * applied.
 */
exports.CSS_DELAY = 30;
/**
 * Moves the given data from the oldPath to the newPath by deleting the data object at the old path
 * and inserting the data at the new path.
 *
 * Note: an item at the index of the new path is pushed behind the inserted item
 *
 * @param dispatch the redux dispatcher used to execute the remove and add actions
 * @param data the data to move (insert at new path)
 * @param oldPath the data at this path will be deleted
 * @param newPath the given data will be inserted at this path
 */
exports.moveListItem = function (dispatch) {
    return function (data, oldPath, newPath) {
        if (newPath === oldPath) {
            // nothing needs to be moved
            return false;
        }
        var oldParentPath = util_1.parentPath(oldPath);
        var oldIndex = util_1.indexFromPath(oldPath);
        var newParentPath = util_1.parentPath(newPath);
        var newIndex = util_1.indexFromPath(newPath);
        // Remove moved data from source array
        dispatch(core_1.update(oldParentPath, function (array) {
            // TODO clone necessary?
            var clone = _.clone(array);
            clone.splice(oldIndex, 1);
            console.log("remove from " + oldParentPath + ", index: " + oldIndex);
            return clone;
        }));
        // Add moved data to target array
        dispatch(core_1.update(newParentPath, function (array) {
            if (array === undefined || array === null || array.length === 0) {
                return [data];
            }
            // TODO clone necessary?
            var clone = _.clone(array);
            clone.splice(newIndex, 0, data);
            console.log("add to " + newParentPath + ", index: " + newIndex);
            return clone;
        }));
        return true;
    };
};
exports.mapDispatchToTreeListProps = function (dispatch) { return ({
    moveListItem: exports.moveListItem(dispatch)
}); };
/**
 * Returns whether the dragged item can be dropped in a list.
 *
 * @param containerProps The ContainmentProperties that the list can supports
 * @param dragInfo The DragInfo describing the dragged item
 */
exports.canDropDraggedItem = function (containerProps, dragInfo) {
    var matchingProps = containerProps
        .filter(function (prop) { return _.isEqual(prop.schema, dragInfo.schema); });
    return matchingProps.length > 0;
};
//# sourceMappingURL=dnd.util.js.map