(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "@greenatom/icons", "../hooks/useStateProps", "./helpers", "./componets/TreeItem", "./styles"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.TreeContext = void 0;
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var icons_1 = require("@greenatom/icons");
    var useStateProps_1 = require("../hooks/useStateProps");
    var helpers_1 = require("./helpers");
    var TreeItem_1 = (0, tslib_1.__importDefault)(require("./componets/TreeItem"));
    var styles_1 = require("./styles");
    exports.TreeContext = (0, react_1.createContext)({});
    var Tree = function (_a) {
        var title = _a.title, icon = _a.icon, isArrow = _a.isArrow, dataSource = _a.dataSource, style = _a.style, className = _a.className, isDisabled = _a.isDisabled, width = _a.width, onClick = _a.onClick, setActive = _a.setActive, onHandleDragStart = _a.onHandleDragStart, onHandleDragFinish = _a.onHandleDragFinish, onHandleDragOver = _a.onHandleDragOver, onTitleChanged = _a.onTitleChanged, isDragAllowed = _a.isDragAllowed, isEditAllowed = _a.isEditAllowed, otherProps = (0, tslib_1.__rest)(_a, ["title", "icon", "isArrow", "dataSource", "style", "className", "isDisabled", "width", "onClick", "setActive", "onHandleDragStart", "onHandleDragFinish", "onHandleDragOver", "onTitleChanged", "isDragAllowed", "isEditAllowed"]);
        var _b = (0, tslib_1.__read)((0, react_1.useState)(dataSource), 2), treeElements = _b[0], setTreeElements = _b[1];
        var _c = (0, tslib_1.__read)((0, useStateProps_1.useStateProps)(isArrow || false), 2), isShown = _c[0], setIsShown = _c[1];
        var _d = (0, tslib_1.__read)((0, react_1.useState)(false), 2), isHover = _d[0], setIsHover = _d[1];
        var _e = (0, tslib_1.__read)((0, react_1.useState)(), 2), currentlyDraggedId = _e[0], setNowDraggingId = _e[1];
        var _f = (0, tslib_1.__read)((0, react_1.useState)(), 2), draggedOverId = _f[0], setNowDragOverId = _f[1];
        var handleDragStart = function (item) {
            setNowDraggingId(item.id);
            onHandleDragStart === null || onHandleDragStart === void 0 ? void 0 : onHandleDragStart(item);
        };
        var handleDragOver = function (item) {
            if (currentlyDraggedId) {
                if (currentlyDraggedId !== item.id) {
                    setNowDragOverId(item.id);
                }
                else {
                    setNowDragOverId(currentlyDraggedId);
                }
                onHandleDragOver === null || onHandleDragOver === void 0 ? void 0 : onHandleDragOver(item);
            }
        };
        var handleDrop = function () {
            if (currentlyDraggedId !== draggedOverId &&
                currentlyDraggedId &&
                draggedOverId) {
                var newTree = (0, helpers_1.moveNode)(treeElements, currentlyDraggedId, draggedOverId);
                setTreeElements(newTree);
                onHandleDragFinish === null || onHandleDragFinish === void 0 ? void 0 : onHandleDragFinish(newTree);
            }
            setNowDraggingId(undefined);
            setNowDragOverId(undefined);
        };
        var handleChangeTitle = function (item, newTitle) {
            if (isEditAllowed) {
                var newTree = (0, helpers_1.changeTitle)(treeElements, item.id, newTitle);
                setTreeElements(newTree);
                onTitleChanged === null || onTitleChanged === void 0 ? void 0 : onTitleChanged(item, newTitle);
            }
        };
        return (react_1.default.createElement(exports.TreeContext.Provider, { value: {
                draggedOverId: draggedOverId,
                currentlyDraggedId: currentlyDraggedId,
                isDragAllowed: isDragAllowed,
                isEditAllowed: isEditAllowed,
                handleChangeTitle: handleChangeTitle,
                handleDragStart: handleDragStart,
                handleDrop: handleDrop,
                handleDragOver: handleDragOver
            } },
            react_1.default.createElement(styles_1.TreeWrapper, (0, tslib_1.__assign)({ style: style, className: className, width: width, isDragOver: draggedOverId === "root", isAnyDragging: currentlyDraggedId !== undefined }, otherProps),
                react_1.default.createElement(styles_1.TreeHeader, { isDisabled: isDisabled, isActive: isShown, onClick: function () { return !isDisabled && setIsShown(!isShown); }, onMouseMove: function () { return !isDisabled && setIsHover(true); }, onMouseOut: function () { return !isDisabled && setIsHover(false); }, onMouseOver: function () {
                        return !isDisabled && handleDragOver({ id: "root", title: "" });
                    } },
                    react_1.default.createElement(styles_1.IconWrapper, { isShown: isShown },
                        react_1.default.createElement(icons_1.IconBrokenArrowRight3, { className: "arrow-icon", width: 16, height: 16 })),
                    react_1.default.createElement(styles_1.Title, { value: title, readOnly: true }),
                    !isDisabled && icon && (isShown || isHover) && (react_1.default.createElement("div", { className: "tree-header__right-content" }, icon && react_1.default.createElement(styles_1.IconWrapper, null, icon)))),
                isShown && (react_1.default.createElement(styles_1.TreeList, null, treeElements &&
                    (treeElements === null || treeElements === void 0 ? void 0 : treeElements.length) > 0 &&
                    treeElements.map(function (element) { return (react_1.default.createElement(TreeItem_1.default, { onClick: onClick, id: element.id, isActive: (element === null || element === void 0 ? void 0 : element.isActive) === true || (setActive === null || setActive === void 0 ? void 0 : setActive(element)), isArrow: (element === null || element === void 0 ? void 0 : element.isArrow) === true, key: element.id, title: element.title, icon: element === null || element === void 0 ? void 0 : element.icon, leadContent: element === null || element === void 0 ? void 0 : element.leadContent, elements: element === null || element === void 0 ? void 0 : element.elements, isDisabled: (element === null || element === void 0 ? void 0 : element.isDisabled) === true, setActive: setActive })); }))))));
    };
    Tree.displayName = "Tree";
    exports.default = Tree;
});
