var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { AutoSizer, List } from 'react-virtualized';
import { equals, append, when, always, gt, uniqWith, eqBy, pipe } from 'ramda';
import withScrolling, { createScrollingComponent, createVerticalStrength, createHorizontalStrength } from 'frontend-collective-react-dnd-scrollzone';
import { DndContext } from 'react-dnd';
import { polyfill } from 'react-lifecycles-compat';
import withStyles from '@mui/styles/withStyles';
import { TreeNode } from './components/TreeNode';
import { NodeRendererDefault } from './components/NodeRendererDefault';
import { TreePlaceholder } from './components/TreePlaceholder';
import { PlaceholderRendererDefault } from './components/PlaceholderRendererDefault';
import { walk, changeNodeAtPath, removeNode, insertNode, find, toggleExpandedForAll } from './helpers/tree';
import { memoizedInsertNode, memoizedGetFlatDataFromTree, memoizedGetDescendantCount } from './helpers/memoizedTreeData';
import { slideRows } from './helpers/generic';
import { defaultGetNodeKey, defaultSearchMethod } from './helpers/defaultHandlers';
import DndManager from './helpers/dndManager';
import { styles } from './styles';
var treeIdCounter = 1;
var mergeTheme = function (props) {
    var merged = __assign(__assign({}, props), { style: __assign(__assign({}, props.theme.style), props.style), innerStyle: __assign(__assign({}, props.theme.innerStyle), props.innerStyle), reactVirtualizedListProps: __assign(__assign({}, props.theme.reactVirtualizedListProps), props.reactVirtualizedListProps) });
    var overridableDefaults = {
        nodeContentRenderer: NodeRendererDefault,
        placeholderRenderer: PlaceholderRendererDefault,
        rowHeight: 62,
        scaffoldBlockPxWidth: 44,
        slideRegionSize: 100,
        treeNodeRenderer: TreeNode
    };
    Object.keys(overridableDefaults).forEach(function (propKey) {
        // If prop has been specified, do not change it
        // If prop is specified in theme, use the theme setting
        // If all else fails, fall back to the default
        if (props[propKey] === null) {
            merged[propKey] =
                typeof props.theme[propKey] !== 'undefined' ? props.theme[propKey] : overridableDefaults[propKey];
        }
    });
    return merged;
};
var ReactSortableTree = /** @class */ (function (_super) {
    __extends(ReactSortableTree, _super);
    function ReactSortableTree(props) {
        var _this = _super.call(this, props) || this;
        var _a = mergeTheme(props), dndType = _a.dndType, nodeContentRenderer = _a.nodeContentRenderer, treeNodeRenderer = _a.treeNodeRenderer, isVirtualized = _a.isVirtualized, slideRegionSize = _a.slideRegionSize;
        _this.dndManager = new DndManager(_this);
        // Wrapping classes for use with react-dnd
        _this.treeId = "rst__".concat(treeIdCounter);
        treeIdCounter += 1;
        _this.dndType = dndType || _this.treeId;
        _this.nodeContentRenderer = _this.dndManager.wrapSource(nodeContentRenderer);
        _this.treePlaceholderRenderer = _this.dndManager.wrapPlaceholder(TreePlaceholder);
        _this.treeNodeRenderer = _this.dndManager.wrapTarget(treeNodeRenderer);
        // Prepare scroll-on-drag options for this list
        if (isVirtualized) {
            _this.scrollZoneVirtualList = (createScrollingComponent || withScrolling)(List);
            _this.vStrength = createVerticalStrength(slideRegionSize);
            _this.hStrength = createHorizontalStrength(slideRegionSize);
        }
        _this.state = {
            draggingTreeData: null,
            draggedNode: null,
            draggedMinimumTreeIndex: null,
            draggedDepth: null,
            searchMatches: [],
            searchFocusTreeIndex: null,
            dragging: false,
            draggingExpandedPaths: [],
            ignoreOneTreeUpdate: false,
            // props that need to be used in gDSFP or static functions will be stored here
            instanceProps: {
                treeData: _this.props.treeData || [],
                searchQuery: null,
                searchFocusOffset: null,
                ignoreTreeChanges: false
            }
        };
        _this.toggleChildrenVisibility = _this.toggleChildrenVisibility.bind(_this);
        _this.moveNode = _this.moveNode.bind(_this);
        _this.startDrag = _this.startDrag.bind(_this);
        _this.dragHover = _this.dragHover.bind(_this);
        _this.endDrag = _this.endDrag.bind(_this);
        _this.drop = _this.drop.bind(_this);
        _this.handleDndMonitorChange = _this.handleDndMonitorChange.bind(_this);
        return _this;
    }
    ReactSortableTree.prototype.componentDidMount = function () {
        ReactSortableTree.loadLazyChildren(this.props);
        var stateUpdate = ReactSortableTree.search(this.props, this.state, true, true, false);
        this.setState(stateUpdate);
        // Hook into react-dnd state changes to detect when the drag ends
        // TODO: This is very brittle, so it needs to be replaced if react-dnd
        // offers a more official way to detect when a drag ends
        this.clearMonitorSubscription = this.props.dragDropManager
            .getMonitor()
            .subscribeToStateChange(this.handleDndMonitorChange);
    };
    ReactSortableTree.getDerivedStateFromProps = function (nextProps, prevState) {
        var instanceProps = prevState.instanceProps;
        var newState = {};
        if (!equals(instanceProps.searchQuery, nextProps.searchQuery)) {
            Object.assign(newState, ReactSortableTree.search(nextProps, prevState, true, true, false));
        }
        else if (instanceProps.searchFocusOffset !== nextProps.searchFocusOffset) {
            Object.assign(newState, ReactSortableTree.search(nextProps, prevState, true, true, true));
        }
        instanceProps.searchQuery = nextProps.searchQuery;
        instanceProps.searchFocusOffset = nextProps.searchFocusOffset;
        newState.instanceProps = instanceProps;
        return newState;
    };
    // listen to dragging
    ReactSortableTree.prototype.componentDidUpdate = function (prevProps, prevState) {
        var _this = this;
        // if it is not the same then call the onDragStateChanged
        if (this.state.dragging !== prevState.dragging) {
            if (this.props.onDragStateChanged) {
                this.props.onDragStateChanged({
                    isDragging: this.state.dragging,
                    draggedNode: this.state.draggedNode
                });
            }
        }
        if (this.state.dragging) {
            var isTreeDataEqual = equals(this.props.treeData, prevProps.treeData);
            if (!isTreeDataEqual) {
                var draggingTreeData_1 = removeNode({
                    treeData: this.props.treeData,
                    path: prevState.draggedPath,
                    getNodeKey: this.props.getNodeKey
                }).treeData;
                this.setState(function (_a) {
                    var draggingExpandedPaths = _a.draggingExpandedPaths;
                    return ({
                        draggingTreeData: draggingExpandedPaths.reduce(function (treeData, path) {
                            var newTree;
                            try {
                                newTree = changeNodeAtPath({
                                    treeData: treeData,
                                    path: path,
                                    newNode: function (_a) {
                                        var node = _a.node;
                                        return (__assign(__assign({}, node), { expanded: true }));
                                    },
                                    getNodeKey: _this.props.getNodeKey
                                });
                            }
                            catch (_a) {
                                newTree = treeData;
                            }
                            return newTree;
                        }, draggingTreeData_1)
                    });
                });
            }
            else if (this.state.draggingTreeData && prevState.draggingTreeData !== this.state.draggingTreeData) {
                walk({
                    treeData: this.state.draggingTreeData,
                    getNodeKey: this.props.getNodeKey,
                    callback: function (_a) {
                        var node = _a.node, path = _a.path, lowerSiblingCounts = _a.lowerSiblingCounts, treeIndex = _a.treeIndex;
                        if (node.pending && node.expanded) {
                            _this.props.onLazyLoadChildren({
                                node: node,
                                path: path,
                                lowerSiblingCounts: lowerSiblingCounts,
                                treeIndex: treeIndex
                            });
                        }
                    }
                });
            }
        }
        else if (this.props.treeData !== prevProps.treeData) {
            var isTreeDataEqual = equals(this.props.treeData, prevProps.treeData);
            var _a = this.state, ignoreOneTreeUpdate = _a.ignoreOneTreeUpdate, instanceProps = _a.instanceProps;
            var newState = {};
            if (!isTreeDataEqual) {
                if (ignoreOneTreeUpdate) {
                    newState.ignoreOneTreeUpdate = false;
                }
                else {
                    newState.searchFocusTreeIndex = null;
                    ReactSortableTree.loadLazyChildren(this.props);
                    Object.assign(newState, ReactSortableTree.search(this.props, this.state, false, false, false));
                }
                newState.draggingTreeData = null;
                newState.draggedNode = null;
                newState.draggedMinimumTreeIndex = null;
                newState.draggedDepth = null;
                newState.dragging = false;
                newState.draggingExpandedPaths = [];
                newState.instanceProps = __assign(__assign({}, instanceProps), { treeData: this.props.treeData });
                this.setState(newState);
            }
        }
    };
    ReactSortableTree.prototype.componentWillUnmount = function () {
        this.clearMonitorSubscription();
        this.dndManager.resetDragVariables();
    };
    ReactSortableTree.prototype.getRows = function (treeData) {
        return memoizedGetFlatDataFromTree({
            ignoreCollapsed: true,
            getNodeKey: this.props.getNodeKey,
            treeData: treeData
        });
    };
    ReactSortableTree.prototype.handleDndMonitorChange = function () {
        var monitor = this.props.dragDropManager.getMonitor();
        // If the drag ends and the tree is still in a mid-drag state,
        // it means that the drag was canceled or the dragSource dropped
        // elsewhere, and we should reset the state of this tree
        if (!monitor.isDragging() && this.state.draggingTreeData) {
            this.endDrag();
        }
    };
    ReactSortableTree.prototype.toggleChildrenVisibility = function (_a) {
        var targetNode = _a.node, path = _a.path;
        var instanceProps = this.state.instanceProps;
        var treeData = changeNodeAtPath({
            treeData: instanceProps.treeData,
            path: path,
            newNode: function (_a) {
                var node = _a.node;
                return (__assign(__assign({}, node), { expanded: !node.expanded }));
            },
            getNodeKey: this.props.getNodeKey
        });
        this.props.onChange(treeData);
        this.props.onVisibilityToggle({
            treeData: treeData,
            node: targetNode,
            expanded: !targetNode.expanded,
            path: path
        });
    };
    ReactSortableTree.prototype.moveNode = function (_a) {
        var _this = this;
        var node = _a.node, prevPath = _a.path, prevTreeIndex = _a.treeIndex, depth = _a.depth, minimumTreeIndex = _a.minimumTreeIndex;
        var _b = insertNode({
            treeData: this.state.draggingTreeData,
            newNode: node,
            depth: depth,
            minimumTreeIndex: minimumTreeIndex,
            expandParent: true,
            getNodeKey: this.props.getNodeKey
        }), treeData = _b.treeData, treeIndex = _b.treeIndex, path = _b.path, nextParentNode = _b.parentNode;
        this.setState({
            draggingTreeData: null,
            draggedNode: null,
            draggedMinimumTreeIndex: null,
            draggedDepth: null,
            dragging: false,
            draggingExpandedPaths: []
        }, function () {
            _this.props.onChange(treeData);
            _this.props.onMoveNode({
                treeData: treeData,
                node: node,
                treeIndex: treeIndex,
                path: path,
                nextPath: path,
                nextTreeIndex: treeIndex,
                prevPath: prevPath,
                prevTreeIndex: prevTreeIndex,
                nextParentNode: nextParentNode
            });
        });
    };
    // returns the new state after search
    ReactSortableTree.search = function (props, state, seekIndex, expand, singleSearch) {
        var onChange = props.onChange, getNodeKey = props.getNodeKey, searchFinishCallback = props.searchFinishCallback, searchQuery = props.searchQuery, searchMethod = props.searchMethod, searchFocusOffset = props.searchFocusOffset, onlyExpandSearchedNodes = props.onlyExpandSearchedNodes;
        var instanceProps = state.instanceProps;
        // Skip search if no conditions are specified
        if (!searchQuery && !searchMethod) {
            if (searchFinishCallback) {
                searchFinishCallback([]);
            }
            return { searchMatches: [] };
        }
        var newState = {};
        // if onlyExpandSearchedNodes collapse the tree and search
        var _a = find({
            getNodeKey: getNodeKey,
            treeData: onlyExpandSearchedNodes
                ? toggleExpandedForAll({
                    treeData: instanceProps.treeData,
                    expanded: false
                })
                : instanceProps.treeData,
            searchQuery: searchQuery,
            searchMethod: searchMethod || defaultSearchMethod,
            searchFocusOffset: searchFocusOffset,
            expandAllMatchPaths: expand && !singleSearch,
            expandFocusMatchPaths: !!expand
        }), expandedTreeData = _a.treeData, searchMatches = _a.matches;
        // Update the tree with data leaving all paths leading to matching nodes open
        if (expand) {
            newState.ignoreOneTreeUpdate = true; // Prevents infinite loop
            onChange(expandedTreeData);
        }
        if (searchFinishCallback) {
            searchFinishCallback(searchMatches);
        }
        var searchFocusTreeIndex = null;
        if (seekIndex && searchFocusOffset !== null && searchFocusOffset < searchMatches.length) {
            searchFocusTreeIndex = searchMatches[searchFocusOffset].treeIndex;
        }
        newState.searchMatches = searchMatches;
        newState.searchFocusTreeIndex = searchFocusTreeIndex;
        return newState;
    };
    ReactSortableTree.prototype.startDrag = function (_a) {
        var _this = this;
        var path = _a.path;
        this.setState(function (prevState) {
            var _a = removeNode({
                treeData: prevState.instanceProps.treeData,
                path: path,
                getNodeKey: _this.props.getNodeKey
            }), draggingTreeData = _a.treeData, draggedNode = _a.node, draggedMinimumTreeIndex = _a.treeIndex;
            return {
                draggingTreeData: draggingTreeData,
                draggedNode: draggedNode,
                draggedDepth: path.length - 1,
                draggedMinimumTreeIndex: draggedMinimumTreeIndex,
                draggedPath: path,
                dragging: true
            };
        });
    };
    ReactSortableTree.prototype.dragHover = function (_a) {
        var _this = this;
        var draggedNode = _a.node, draggedDepth = _a.depth, draggedMinimumTreeIndex = _a.minimumTreeIndex;
        // Ignore this hover if it is at the same position as the last hover
        if (this.state.draggedDepth === draggedDepth &&
            this.state.draggedMinimumTreeIndex === draggedMinimumTreeIndex) {
            return;
        }
        this.setState(function (_a) {
            var draggingTreeData = _a.draggingTreeData, instanceProps = _a.instanceProps, draggingExpandedPaths = _a.draggingExpandedPaths;
            // Fall back to the tree data if something is being dragged in from
            //  an external element
            var newDraggingTreeData = draggingTreeData || instanceProps.treeData;
            var addedResult = memoizedInsertNode({
                treeData: newDraggingTreeData,
                newNode: draggedNode,
                depth: draggedDepth,
                minimumTreeIndex: draggedMinimumTreeIndex,
                expandParent: true,
                getNodeKey: _this.props.getNodeKey
            });
            var rows = _this.getRows(addedResult.treeData);
            var expandedParentPath = rows[addedResult.treeIndex].path;
            var nodePath = expandedParentPath.slice(0, -1);
            var newDraggingExpandedPaths = pipe(when(always(gt(nodePath.length, 0)), append(nodePath)), uniqWith(eqBy(String)))(draggingExpandedPaths);
            return {
                draggedNode: draggedNode,
                draggedDepth: draggedDepth,
                draggedMinimumTreeIndex: draggedMinimumTreeIndex,
                draggingTreeData: changeNodeAtPath({
                    treeData: newDraggingTreeData,
                    path: nodePath,
                    newNode: function (_a) {
                        var node = _a.node;
                        return (__assign(__assign({}, node), { expanded: true }));
                    },
                    getNodeKey: _this.props.getNodeKey
                }),
                // reset the scroll focus so it doesn't jump back
                // to a search result while dragging
                searchFocusTreeIndex: null,
                dragging: true,
                draggingExpandedPaths: newDraggingExpandedPaths
            };
        });
    };
    ReactSortableTree.prototype.endDrag = function (dropResult) {
        var _this = this;
        var instanceProps = this.state.instanceProps;
        var resetTree = function () {
            return _this.setState({
                draggingTreeData: null,
                draggedNode: null,
                draggedMinimumTreeIndex: null,
                draggedDepth: null,
                dragging: false
            });
        };
        // Drop was cancelled
        if (!dropResult) {
            resetTree();
        }
        else if (dropResult.treeId !== this.treeId) {
            // The node was dropped in an external drop target or tree
            var node = dropResult.node, path = dropResult.path, treeIndex = dropResult.treeIndex;
            var shouldCopy = this.props.shouldCopyOnOutsideDrop;
            if (typeof shouldCopy === 'function') {
                shouldCopy = shouldCopy({
                    node: node,
                    prevTreeIndex: treeIndex,
                    prevPath: path
                });
            }
            var treeData = this.state.draggingTreeData || instanceProps.treeData;
            // If copying is enabled, a drop outside leaves behind a copy in the
            //  source tree
            if (shouldCopy) {
                treeData = changeNodeAtPath({
                    treeData: instanceProps.treeData, // use treeData unaltered by the drag operation
                    path: path,
                    newNode: function (_a) {
                        var copyNode = _a.node;
                        return (__assign({}, copyNode));
                    }, // create a shallow copy of the node
                    getNodeKey: this.props.getNodeKey
                });
            }
            this.props.onChange(treeData);
            this.props.onMoveNode({
                treeData: treeData,
                node: node,
                treeIndex: null,
                path: null,
                nextPath: null,
                nextTreeIndex: null,
                prevPath: path,
                prevTreeIndex: treeIndex
            });
        }
    };
    ReactSortableTree.prototype.drop = function (dropResult) {
        this.moveNode(dropResult);
    };
    ReactSortableTree.prototype.canNodeHaveChildren = function (node) {
        var canNodeHaveChildren = this.props.canNodeHaveChildren;
        if (canNodeHaveChildren) {
            return canNodeHaveChildren(node);
        }
        return true;
    };
    // Load any children in the tree that are given by a function
    // calls the onChange callback on the new treeData
    ReactSortableTree.loadLazyChildren = function (props) {
        walk({
            treeData: props.treeData,
            getNodeKey: props.getNodeKey,
            callback: function (_a) {
                var node = _a.node, path = _a.path, lowerSiblingCounts = _a.lowerSiblingCounts, treeIndex = _a.treeIndex;
                // If the node has children defined by a function, and is either expanded
                //  or set to load even before expansion, run the function.
                if (node.pending && (node.expanded || props.loadCollapsedLazyChildren)) {
                    // Call the children fetching function
                    props.onLazyLoadChildren({
                        node: node,
                        path: path,
                        lowerSiblingCounts: lowerSiblingCounts,
                        treeIndex: treeIndex,
                        // Provide a helper to append the new data when it is received
                        done: function (childrenArray) {
                            return props.onChange(changeNodeAtPath({
                                treeData: props.treeData,
                                path: path,
                                newNode: function (_a) {
                                    var oldNode = _a.node;
                                    // Only replace the old node if it's the one we set off to find children
                                    //  for in the first place
                                    return oldNode === node
                                        ? __assign(__assign({}, oldNode), { children: childrenArray }) : oldNode;
                                },
                                getNodeKey: props.getNodeKey
                            }), true);
                        }
                    });
                }
            }
        });
    };
    ReactSortableTree.prototype.renderRow = function (row, _a) {
        var listIndex = _a.listIndex, style = _a.style, getPrevRow = _a.getPrevRow, matchKeys = _a.matchKeys, swapFrom = _a.swapFrom, swapDepth = _a.swapDepth, swapLength = _a.swapLength;
        var node = row.node, parentNode = row.parentNode, path = row.path, lowerSiblingCounts = row.lowerSiblingCounts, treeIndex = row.treeIndex;
        var _b = mergeTheme(this.props), canDrag = _b.canDrag, generateNodeProps = _b.generateNodeProps, scaffoldBlockPxWidth = _b.scaffoldBlockPxWidth, searchFocusOffset = _b.searchFocusOffset, rowDirection = _b.rowDirection;
        var TreeNodeRenderer = this.treeNodeRenderer;
        var NodeContentRenderer = this.nodeContentRenderer;
        var nodeKey = path[path.length - 1];
        var isSearchMatch = nodeKey in matchKeys;
        var isSearchFocus = isSearchMatch && matchKeys[nodeKey] === searchFocusOffset;
        var callbackParams = {
            node: node,
            parentNode: parentNode,
            path: path,
            lowerSiblingCounts: lowerSiblingCounts,
            treeIndex: treeIndex,
            isSearchMatch: isSearchMatch,
            isSearchFocus: isSearchFocus
        };
        var nodeProps = !generateNodeProps ? {} : generateNodeProps(callbackParams);
        var rowCanDrag = typeof canDrag !== 'function' ? canDrag : canDrag(callbackParams);
        var sharedProps = {
            treeIndex: treeIndex,
            scaffoldBlockPxWidth: scaffoldBlockPxWidth,
            node: node,
            path: path,
            treeId: this.treeId,
            rowDirection: rowDirection
        };
        return (React.createElement(TreeNodeRenderer, __assign({ style: style, key: nodeKey, listIndex: listIndex, getPrevRow: getPrevRow, lowerSiblingCounts: lowerSiblingCounts, swapFrom: swapFrom, swapLength: swapLength, swapDepth: swapDepth }, sharedProps),
            React.createElement(NodeContentRenderer, __assign({ parentNode: parentNode, isSearchMatch: isSearchMatch, isSearchFocus: isSearchFocus, canDrag: rowCanDrag, toggleChildrenVisibility: this.toggleChildrenVisibility }, sharedProps, nodeProps))));
    };
    ReactSortableTree.prototype.render = function () {
        var _this = this;
        var _a = mergeTheme(this.props), dragDropManager = _a.dragDropManager, style = _a.style, className = _a.className, innerStyle = _a.innerStyle, rowHeight = _a.rowHeight, isVirtualized = _a.isVirtualized, placeholderRenderer = _a.placeholderRenderer, reactVirtualizedListProps = _a.reactVirtualizedListProps, getNodeKey = _a.getNodeKey, rowDirection = _a.rowDirection;
        var _b = this.state, searchMatches = _b.searchMatches, searchFocusTreeIndex = _b.searchFocusTreeIndex, draggedNode = _b.draggedNode, draggedDepth = _b.draggedDepth, draggedMinimumTreeIndex = _b.draggedMinimumTreeIndex;
        var treeData = this.state.draggingTreeData || this.props.treeData;
        var rowDirectionClass = rowDirection === 'rtl' ? 'rst__rtl' : null;
        var rows;
        var swapFrom = null;
        var swapLength = null;
        if (draggedNode && draggedMinimumTreeIndex !== null) {
            var addedResult = memoizedInsertNode({
                treeData: treeData,
                newNode: draggedNode,
                depth: draggedDepth,
                minimumTreeIndex: draggedMinimumTreeIndex,
                expandParent: true,
                getNodeKey: getNodeKey
            });
            var swapTo = draggedMinimumTreeIndex;
            swapFrom = addedResult.treeIndex;
            swapLength = 1 + memoizedGetDescendantCount({ node: draggedNode });
            rows = slideRows(this.getRows(addedResult.treeData), swapFrom, swapTo, swapLength);
        }
        else {
            rows = this.getRows(treeData);
        }
        // Get indices for rows that match the search conditions
        var matchKeys = {};
        searchMatches.forEach(function (_a, i) {
            var path = _a.path;
            matchKeys[path[path.length - 1]] = i;
        });
        // Seek to the focused search result if there is one specified
        var scrollToInfo = searchFocusTreeIndex !== null ? { scrollToIndex: searchFocusTreeIndex } : {};
        var containerStyle = style;
        var list;
        if (rows.length < 1) {
            var Placeholder = this.treePlaceholderRenderer;
            var PlaceholderContent = placeholderRenderer;
            list = (React.createElement(Placeholder, { treeId: this.treeId, drop: this.drop },
                React.createElement(PlaceholderContent, null)));
        }
        else if (isVirtualized) {
            containerStyle = __assign({ height: '100%' }, containerStyle);
            var ScrollZoneVirtualList_1 = this.scrollZoneVirtualList;
            // Render list with react-virtualized
            list = (React.createElement(AutoSizer, null, function (_a) {
                var height = _a.height, width = _a.width;
                return (React.createElement(ScrollZoneVirtualList_1, __assign({}, scrollToInfo, { dragDropManager: dragDropManager, verticalStrength: _this.vStrength, horizontalStrength: _this.hStrength, speed: 30, scrollToAlignment: "start", className: _this.props.classes.rst__virtualScrollOverride, width: width, onScroll: function (_a) {
                        var scrollTop = _a.scrollTop;
                        _this.scrollTop = scrollTop;
                    }, height: height, style: innerStyle, rowCount: rows.length, estimatedRowSize: typeof rowHeight !== 'function' ? rowHeight : undefined, rowHeight: typeof rowHeight !== 'function'
                        ? rowHeight
                        : function (_a) {
                            var index = _a.index;
                            return rowHeight({
                                index: index,
                                treeIndex: index,
                                node: rows[index].node,
                                path: rows[index].path
                            });
                        }, rowRenderer: function (_a) {
                        var index = _a.index, rowStyle = _a.style;
                        return _this.renderRow(rows[index], {
                            listIndex: index,
                            style: rowStyle,
                            getPrevRow: function () { return rows[index - 1] || null; },
                            matchKeys: matchKeys,
                            swapFrom: swapFrom,
                            swapDepth: draggedDepth,
                            swapLength: swapLength
                        });
                    } }, reactVirtualizedListProps)));
            }));
        }
        else {
            // Render list without react-virtualized
            list = rows.map(function (row, index) {
                return _this.renderRow(row, {
                    listIndex: index,
                    style: {
                        height: typeof rowHeight !== 'function'
                            ? rowHeight
                            : rowHeight({
                                index: index,
                                treeIndex: index,
                                node: row.node,
                                path: row.path
                            })
                    },
                    getPrevRow: function () { return rows[index - 1] || null; },
                    matchKeys: matchKeys,
                    swapFrom: swapFrom,
                    swapDepth: draggedDepth,
                    swapLength: swapLength
                });
            });
        }
        return (React.createElement("div", { className: classnames('rst__tree', className, rowDirectionClass), style: containerStyle }, list));
    };
    return ReactSortableTree;
}(Component));
ReactSortableTree.propTypes = {
    classes: PropTypes.object,
    dragDropManager: PropTypes.shape({
        getMonitor: PropTypes.func
    }).isRequired,
    // Tree data in the following format:
    // [{title: 'main', subtitle: 'sub'}, { title: 'value2', expanded: true, children: [{ title: 'value3') }] }]
    // `title` is the primary label for the node
    // `subtitle` is a secondary label for the node
    // `expanded` shows children of the node if true, or hides them if false. Defaults to false.
    // `children` is an array of child nodes belonging to the node.
    treeData: PropTypes.arrayOf(PropTypes.object).isRequired,
    // Style applied to the container wrapping the tree (style defaults to {height: '100%'})
    style: PropTypes.shape({}),
    // Class name for the container wrapping the tree
    className: PropTypes.string,
    // Style applied to the inner, scrollable container (for padding, etc.)
    innerStyle: PropTypes.shape({}),
    // Used by react-virtualized
    // Either a fixed row height (number) or a function that returns the
    // height of a row given its index: `({ index: number }): number`
    rowHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]),
    // Size in px of the region near the edges that initiates scrolling on dragover
    slideRegionSize: PropTypes.number,
    // Custom properties to hand to the react-virtualized list
    // https://github.com/bvaughn/react-virtualized/blob/master/docs/List.md#prop-types
    reactVirtualizedListProps: PropTypes.shape({}),
    // The width of the blocks containing the lines representing the structure of the tree.
    scaffoldBlockPxWidth: PropTypes.number,
    // Maximum depth nodes can be inserted at. Defaults to infinite.
    maxDepth: PropTypes.number,
    // The method used to search nodes.
    // Defaults to a function that uses the `searchQuery` string to search for nodes with
    // matching `title` or `subtitle` values.
    // NOTE: Changing `searchMethod` will not update the search, but changing the `searchQuery` will.
    searchMethod: PropTypes.func,
    // Used by the `searchMethod` to highlight and scroll to matched nodes.
    // Should be a string for the default `searchMethod`, but can be anything when using a custom search.
    searchQuery: PropTypes.any,
    // Outline the <`searchFocusOffset`>th node and scroll to it.
    searchFocusOffset: PropTypes.number,
    // Get the nodes that match the search criteria. Used for counting total matches, etc.
    searchFinishCallback: PropTypes.func,
    // Generate an object with additional props to be passed to the node renderer.
    // Use this for adding buttons via the `buttons` key,
    // or additional `style` / `className` settings.
    generateNodeProps: PropTypes.func,
    // Set to false to disable virtualization.
    // NOTE: Auto-scrolling while dragging, and scrolling to the `searchFocusOffset` will be disabled.
    isVirtualized: PropTypes.bool,
    treeNodeRenderer: PropTypes.func,
    // Override the default component for rendering nodes (but keep the scaffolding generator)
    // This is an advanced option for complete customization of the appearance.
    // It is best to copy the component in `node-renderer-default.js` to use as a base, and customize as needed.
    nodeContentRenderer: PropTypes.func,
    // Override the default component for rendering an empty tree
    // This is an advanced option for complete customization of the appearance.
    // It is best to copy the component in `placeholder-renderer-default.js` to use as a base,
    // and customize as needed.
    placeholderRenderer: PropTypes.func,
    theme: PropTypes.shape({
        style: PropTypes.shape({}),
        innerStyle: PropTypes.shape({}),
        reactVirtualizedListProps: PropTypes.shape({}),
        scaffoldBlockPxWidth: PropTypes.number,
        slideRegionSize: PropTypes.number,
        rowHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]),
        treeNodeRenderer: PropTypes.func,
        nodeContentRenderer: PropTypes.func,
        placeholderRenderer: PropTypes.func
    }),
    // Determine the unique key used to identify each node and
    // generate the `path` array passed in callbacks.
    // By default, returns the index in the tree (omitting hidden nodes).
    getNodeKey: PropTypes.func,
    // Called whenever tree data changed.
    // Just like with React input elements, you have to update your
    // own component's data to see the changes reflected.
    onChange: PropTypes.func.isRequired,
    // Called after node move operation.
    onMoveNode: PropTypes.func,
    // Determine whether a node can be dragged. Set to false to disable dragging on all nodes.
    canDrag: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
    // Determine whether a node can be dropped based on its path and parents'.
    canDrop: PropTypes.func,
    // Determine whether a node can have children
    canNodeHaveChildren: PropTypes.func,
    // When true, or a callback returning true, dropping nodes to react-dnd
    // drop targets outside of this tree will not remove them from this tree
    shouldCopyOnOutsideDrop: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
    // Called after children nodes collapsed or expanded.
    onVisibilityToggle: PropTypes.func,
    dndType: PropTypes.string,
    // Called to track between dropped and dragging
    onDragStateChanged: PropTypes.func,
    // Specify that nodes that do not match search will be collapsed
    onlyExpandSearchedNodes: PropTypes.bool,
    // rtl support
    rowDirection: PropTypes.string,
    onLazyLoadChildren: PropTypes.func,
    ignoreTreeChanges: PropTypes.bool
};
ReactSortableTree.defaultProps = {
    canDrag: true,
    canDrop: null,
    canNodeHaveChildren: function () { return true; },
    className: '',
    dndType: null,
    generateNodeProps: null,
    getNodeKey: defaultGetNodeKey,
    innerStyle: {},
    isVirtualized: true,
    maxDepth: null,
    treeNodeRenderer: null,
    nodeContentRenderer: null,
    onMoveNode: function () { },
    onVisibilityToggle: function () { },
    placeholderRenderer: null,
    reactVirtualizedListProps: {},
    rowHeight: null,
    scaffoldBlockPxWidth: null,
    searchFinishCallback: null,
    searchFocusOffset: null,
    searchMethod: null,
    searchQuery: null,
    shouldCopyOnOutsideDrop: false,
    slideRegionSize: null,
    style: {},
    theme: {},
    onDragStateChanged: function () { },
    onlyExpandSearchedNodes: false,
    rowDirection: 'ltr',
    onLazyLoadChildren: function () { },
    ignoreTreeChanges: false
};
polyfill(ReactSortableTree);
var StyledReactSortableTree = withStyles(styles)(ReactSortableTree);
var SortableTree = function (props) { return (React.createElement(DndContext.Consumer, null, function (_a) {
    var dragDropManager = _a.dragDropManager;
    return dragDropManager === undefined ? null : (React.createElement(StyledReactSortableTree, __assign({}, props, { dragDropManager: dragDropManager })));
})); };
export default SortableTree;
