"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 __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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeRendererDefault = void 0;
var react_1 = __importDefault(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var prop_types_1 = __importDefault(require("prop-types"));
var tree_1 = require("../../helpers/tree");
//import {useStyles} from './nodeRendererDefaultStyle';
var EMPTY_ARRAY = [];
var EMPTY_OBJECT = {};
var NodeRendererDefault = function (_a) {
    var scaffoldBlockPxWidth = _a.scaffoldBlockPxWidth, _b = _a.toggleChildrenVisibility, toggleChildrenVisibility = _b === void 0 ? null : _b, connectDragPreview = _a.connectDragPreview, connectDragSource = _a.connectDragSource, isDragging = _a.isDragging, _c = _a.canDrop, canDrop = _c === void 0 ? false : _c, _d = _a.canDrag, canDrag = _d === void 0 ? false : _d, node = _a.node, _e = _a.title, title = _e === void 0 ? null : _e, _f = _a.subtitle, subtitle = _f === void 0 ? null : _f, _g = _a.draggedNode, draggedNode = _g === void 0 ? null : _g, path = _a.path, treeIndex = _a.treeIndex, _h = _a.isSearchMatch, isSearchMatch = _h === void 0 ? false : _h, _j = _a.isSearchFocus, isSearchFocus = _j === void 0 ? false : _j, _k = _a.buttons, buttons = _k === void 0 ? EMPTY_ARRAY : _k, _l = _a.className, className = _l === void 0 ? '' : _l, _m = _a.style, style = _m === void 0 ? EMPTY_OBJECT : _m, didDrop = _a.didDrop, 
    /* eslint-disable */
    treeId = _a.treeId, isOver = _a.isOver, // Not needed, but preserved for other renderers
    _o = _a.parentNode, // Not needed, but preserved for other renderers
    parentNode = _o === void 0 ? null : _o, // Needed for dndManager
    /* eslint-enable */
    _p = _a.rowDirection, // Needed for dndManager
    /* eslint-enable */
    rowDirection = _p === void 0 ? 'ltr' : _p, otherProps = __rest(_a, ["scaffoldBlockPxWidth", "toggleChildrenVisibility", "connectDragPreview", "connectDragSource", "isDragging", "canDrop", "canDrag", "node", "title", "subtitle", "draggedNode", "path", "treeIndex", "isSearchMatch", "isSearchFocus", "buttons", "className", "style", "didDrop", "treeId", "isOver", "parentNode", "rowDirection"]);
    //useStyles();
    var nodeTitle = title || node.title;
    var nodeSubtitle = subtitle || node.subtitle;
    var rowDirectionClass = rowDirection === 'rtl' ? 'rst__rtl' : null;
    var handle;
    if (canDrag) {
        if (typeof node.children === 'function' && node.expanded) {
            // Show a loading symbol on the handle when the children are expanded
            //  and yet still defined by a function (a callback to fetch the children)
            handle = (react_1.default.createElement("div", { className: "rst__loadingHandle" },
                react_1.default.createElement("div", { className: "rst__loadingCircle" }, __spreadArray([], new Array(12), true).map(function (_, index) { return (react_1.default.createElement("div", { key: index, className: (0, classnames_1.default)('rst__loadingCirclePoint', rowDirectionClass) })); }))));
        }
        else {
            // Show the handle used to initiate a drag-and-drop
            handle = connectDragSource(react_1.default.createElement("div", { className: "rst__moveHandle" }), {
                dropEffect: 'copy'
            });
        }
    }
    var isDraggedDescendant = draggedNode && (0, tree_1.isDescendant)(draggedNode, node);
    var isLandingPadActive = !didDrop && isDragging;
    var buttonStyle = { left: -0.5 * scaffoldBlockPxWidth };
    if (rowDirection === 'rtl') {
        buttonStyle = { right: -0.5 * scaffoldBlockPxWidth };
    }
    return (react_1.default.createElement("div", __assign({ style: { height: '100%' } }, otherProps),
        toggleChildrenVisibility &&
            node.children &&
            (node.children.length > 0 || typeof node.children === 'function') && (react_1.default.createElement("div", null,
            react_1.default.createElement("button", { type: "button", "aria-label": node.expanded ? 'Collapse' : 'Expand', className: (0, classnames_1.default)(node.expanded ? 'rst__collapseButton' : 'rst__expandButton', rowDirectionClass), style: buttonStyle, onClick: function () {
                    return toggleChildrenVisibility({
                        node: node,
                        path: path,
                        treeIndex: treeIndex
                    });
                } }),
            node.expanded && !isDragging && (react_1.default.createElement("div", { style: { width: scaffoldBlockPxWidth }, className: (0, classnames_1.default)('rst__lineChildren', rowDirectionClass) })))),
        react_1.default.createElement("div", { className: (0, classnames_1.default)('rst__rowWrapper', rowDirectionClass) }, connectDragPreview(react_1.default.createElement("div", { className: (0, classnames_1.default)('rst__row', isLandingPadActive && 'rst__rowLandingPad', isLandingPadActive && !canDrop && 'rst__rowCancelPad', isSearchMatch && 'rst__rowSearchMatch', isSearchFocus && 'rst__rowSearchFocus', rowDirectionClass, className), style: __assign({ opacity: isDraggedDescendant ? 0.5 : 1 }, style) },
            handle,
            react_1.default.createElement("div", { className: (0, classnames_1.default)('rst__rowContents', !canDrag && 'rst__rowContentsDragDisabled', rowDirectionClass) },
                react_1.default.createElement("div", { className: (0, classnames_1.default)('rst__rowLabel', rowDirectionClass) },
                    react_1.default.createElement("span", { className: (0, classnames_1.default)('rst__rowTitle', node.subtitle && 'rst__rowTitleWithSubtitle') }, typeof nodeTitle === 'function'
                        ? nodeTitle({
                            node: node,
                            path: path,
                            treeIndex: treeIndex
                        })
                        : nodeTitle),
                    nodeSubtitle && (react_1.default.createElement("span", { className: "rst__rowSubtitle" }, typeof nodeSubtitle === 'function'
                        ? nodeSubtitle({
                            node: node,
                            path: path,
                            treeIndex: treeIndex
                        })
                        : nodeSubtitle))),
                react_1.default.createElement("div", { className: "rst__rowToolbar" }, buttons.map(function (btn, index) { return (react_1.default.createElement("div", { key: index, className: "rst__toolbarButton" }, btn)); }))))))));
};
exports.NodeRendererDefault = NodeRendererDefault;
exports.NodeRendererDefault.propTypes = {
    node: prop_types_1.default.shape({}).isRequired,
    title: prop_types_1.default.oneOfType([prop_types_1.default.func, prop_types_1.default.node]),
    subtitle: prop_types_1.default.oneOfType([prop_types_1.default.func, prop_types_1.default.node]),
    path: prop_types_1.default.arrayOf(prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number])).isRequired,
    treeIndex: prop_types_1.default.number.isRequired,
    treeId: prop_types_1.default.string.isRequired,
    isSearchMatch: prop_types_1.default.bool,
    isSearchFocus: prop_types_1.default.bool,
    canDrag: prop_types_1.default.bool,
    scaffoldBlockPxWidth: prop_types_1.default.number.isRequired,
    toggleChildrenVisibility: prop_types_1.default.func,
    buttons: prop_types_1.default.arrayOf(prop_types_1.default.node),
    className: prop_types_1.default.string,
    style: prop_types_1.default.shape({}),
    // Drag and drop API functions
    // Drag source
    connectDragPreview: prop_types_1.default.func.isRequired,
    connectDragSource: prop_types_1.default.func.isRequired,
    parentNode: prop_types_1.default.shape({}), // Needed for dndManager
    isDragging: prop_types_1.default.bool.isRequired,
    didDrop: prop_types_1.default.bool.isRequired,
    draggedNode: prop_types_1.default.shape({}),
    // Drop target
    isOver: prop_types_1.default.bool.isRequired,
    canDrop: prop_types_1.default.bool,
    // rtl support
    rowDirection: prop_types_1.default.string
};
