"use strict";
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);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeNode = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var prop_types_1 = __importDefault(require("prop-types"));
var styles_1 = require("./styles");
var TreeNode = function (_a) {
    var children = _a.children, listIndex = _a.listIndex, swapFrom = _a.swapFrom, swapLength = _a.swapLength, swapDepth = _a.swapDepth, scaffoldBlockPxWidth = _a.scaffoldBlockPxWidth, lowerSiblingCounts = _a.lowerSiblingCounts, connectDropTarget = _a.connectDropTarget, isOver = _a.isOver, draggedNode = _a.draggedNode, canDrop = _a.canDrop, treeIndex = _a.treeIndex, 
    /* eslint-disable */
    treeId = _a.treeId, // Delete from otherProps
    getPrevRow = _a.getPrevRow, // Delete from otherProps
    node = _a.node, // Delete from otherProps
    path = _a.path, // Delete from otherProps
    /* eslint-enable */
    rowDirection = _a.rowDirection, otherProps = __rest(_a, ["children", "listIndex", "swapFrom", "swapLength", "swapDepth", "scaffoldBlockPxWidth", "lowerSiblingCounts", "connectDropTarget", "isOver", "draggedNode", "canDrop", "treeIndex", "treeId", "getPrevRow", "node", "path", "rowDirection"]);
    (0, styles_1.useStyles)();
    var rowDirectionClass = rowDirection === 'rtl' ? 'rst__rtl' : null;
    // Construct the scaffold representing the structure of the tree
    var scaffoldBlockCount = lowerSiblingCounts.length;
    var scaffold = [];
    lowerSiblingCounts.forEach(function (lowerSiblingCount, i) {
        var lineClass = '';
        if (lowerSiblingCount > 0) {
            // At this level in the tree, the nodes had sibling nodes further down
            if (listIndex === 0) {
                // Top-left corner of the tree
                // +-----+
                // |     |
                // |  +--+
                // |  |  |
                // +--+--+
                lineClass = 'rst__lineHalfHorizontalRight rst__lineHalfVerticalBottom';
            }
            else if (i === scaffoldBlockCount - 1) {
                // Last scaffold block in the row, right before the row content
                // +--+--+
                // |  |  |
                // |  +--+
                // |  |  |
                // +--+--+
                lineClass = 'rst__lineHalfHorizontalRight rst__lineFullVertical';
            }
            else {
                // Simply connecting the line extending down to the next sibling on this level
                // +--+--+
                // |  |  |
                // |  |  |
                // |  |  |
                // +--+--+
                lineClass = 'rst__lineFullVertical';
            }
        }
        else if (listIndex === 0) {
            // Top-left corner of the tree, but has no siblings
            // +-----+
            // |     |
            // |  +--+
            // |     |
            // +-----+
            lineClass = 'rst__lineHalfHorizontalRight';
        }
        else if (i === scaffoldBlockCount - 1) {
            // The last or only node in this level of the tree
            // +--+--+
            // |  |  |
            // |  +--+
            // |     |
            // +-----+
            lineClass = 'rst__lineHalfVerticalTop rst__lineHalfHorizontalRight';
        }
        scaffold.push(react_1.default.createElement("div", { key: "pre_".concat(1 + i), style: { width: scaffoldBlockPxWidth }, className: (0, classnames_1.default)('rst__lineBlock', lineClass, rowDirectionClass) }));
        if (treeIndex !== listIndex && i === swapDepth) {
            // This row has been shifted, and is at the depth of
            // the line pointing to the new destination
            var highlightLineClass = '';
            if (listIndex === swapFrom + swapLength - 1) {
                // This block is on the bottom (target) line
                // This block points at the target block (where the row will go when released)
                highlightLineClass = 'rst__highlightBottomLeftCorner';
            }
            else if (treeIndex === swapFrom) {
                // This block is on the top (source) line
                highlightLineClass = 'rst__highlightTopLeftCorner';
            }
            else {
                // This block is between the bottom and top
                highlightLineClass = 'rst__highlightLineVertical';
            }
            var style_1;
            if (rowDirection === 'rtl') {
                style_1 = {
                    width: scaffoldBlockPxWidth,
                    right: scaffoldBlockPxWidth * i
                };
            }
            else {
                // Default ltr
                style_1 = {
                    width: scaffoldBlockPxWidth,
                    left: scaffoldBlockPxWidth * i
                };
            }
            scaffold.push(react_1.default.createElement("div", { key: i, style: style_1, className: (0, classnames_1.default)('rst__absoluteLineBlock', highlightLineClass, rowDirectionClass) }));
        }
    });
    var style;
    if (rowDirection === 'rtl') {
        style = { right: scaffoldBlockPxWidth * scaffoldBlockCount };
    }
    else {
        // Default ltr
        style = { left: scaffoldBlockPxWidth * scaffoldBlockCount };
    }
    return connectDropTarget(react_1.default.createElement("div", __assign({}, otherProps, { className: (0, classnames_1.default)('rst__node', rowDirectionClass) }),
        scaffold,
        react_1.default.createElement("div", { className: "rst__nodeContent", style: style }, react_1.Children.map(children, function (child) {
            return (0, react_1.cloneElement)(child, {
                isOver: isOver,
                canDrop: canDrop,
                draggedNode: draggedNode
            });
        }))));
};
exports.TreeNode = TreeNode;
exports.TreeNode.defaultProps = {
    swapFrom: null,
    swapDepth: null,
    swapLength: null,
    canDrop: false,
    draggedNode: null,
    rowDirection: 'ltr'
};
exports.TreeNode.propTypes = {
    treeIndex: prop_types_1.default.number.isRequired,
    treeId: prop_types_1.default.string.isRequired,
    swapFrom: prop_types_1.default.number,
    swapDepth: prop_types_1.default.number,
    swapLength: prop_types_1.default.number,
    scaffoldBlockPxWidth: prop_types_1.default.number.isRequired,
    lowerSiblingCounts: prop_types_1.default.arrayOf(prop_types_1.default.number).isRequired,
    listIndex: prop_types_1.default.number.isRequired,
    children: prop_types_1.default.node.isRequired,
    // Drop target
    connectDropTarget: prop_types_1.default.func.isRequired,
    isOver: prop_types_1.default.bool.isRequired,
    canDrop: prop_types_1.default.bool,
    draggedNode: prop_types_1.default.shape({}),
    // used in dndManager
    getPrevRow: prop_types_1.default.func.isRequired,
    node: prop_types_1.default.shape({}).isRequired,
    path: prop_types_1.default.arrayOf(prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number])).isRequired,
    // rtl support
    rowDirection: prop_types_1.default.string
};
