"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
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;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.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.icons = void 0;
var React = __importStar(require("react"));
var styled_components_1 = __importDefault(require("@xstyled/styled-components"));
var system_1 = require("@xstyled/system");
var Icon_1 = __importDefault(require("../Icon/Icon"));
var tree_mid_png_1 = __importDefault(require("../shared/assets/tree-mid.png"));
var tree_last_png_1 = __importDefault(require("../shared/assets/tree-last.png"));
var tree_node_children_png_1 = __importDefault(require("../shared/assets/tree-node-children.png"));
var FOLDER_CLOSED = 'folder';
var FOLDER_OPENED = 'folder_open';
exports.icons = {
    FILE_MEDIA: 'media_cd',
    FILE_TEXT: 'file_text',
    FILE_UNKNOWN: 'bat',
    FILE_FONT: 'file_font_2',
    FILE_PEN: 'file_pen',
    FILE_SETTINGS: 'file_settings',
    FILE_TEXT_SETTINGS: 'file_text_settings',
    FILE_EXECUTABLE: 'bat_exec',
};
var NodeItem = styled_components_1.default.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n  list-style-type: none;\n  background-repeat: no-repeat;\n  background-image: url(", ");\n\n  &:last-child {\n    background-image: url(", ");\n  }\n"], ["\n  list-style-type: none;\n  background-repeat: no-repeat;\n  background-image: url(", ");\n\n  &:last-child {\n    background-image: url(",
    ");\n  }\n"])), tree_mid_png_1.default, function (_a) {
    var isOpen = _a.isOpen;
    return isOpen ? tree_mid_png_1.default : tree_last_png_1.default;
});
var NodeInfo = styled_components_1.default.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n  display: flex;\n  align-items: center;\n  user-select: none;\n  -webkit-user-select: none;\n  -khtml-user-select: none;\n  -moz-user-select: none;\n  -ms-user-select: none;\n"], ["\n  display: flex;\n  align-items: center;\n  user-select: none;\n  -webkit-user-select: none;\n  -khtml-user-select: none;\n  -moz-user-select: none;\n  -ms-user-select: none;\n"])));
var FolderStatus = styled_components_1.default.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  width: 10px;\n  height: 10px;\n  border: 1;\n  border-color: ", ";\n  background-color: ", ";\n  font-size: 11px;\n"], ["\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  width: 10px;\n  height: 10px;\n  border: 1;\n  border-color: ", ";\n  background-color: ", ";\n  font-size: 11px;\n"])), system_1.th('colors.black'), system_1.th('colors.white'));
var IconContainer = styled_components_1.default.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  width: 20px;\n  height: 20px;\n  margin-right: 6;\n  margin-left: ", "px;\n"], ["\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  width: 20px;\n  height: 20px;\n  margin-right: 6;\n  margin-left: ", "px;\n"])), function (_a) {
    var hasChildren = _a.hasChildren;
    return (hasChildren ? 8 : 18);
});
var NodeChildren = styled_components_1.default.ul(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n  padding: 0 0 0 22;\n  background-image: url(", ");\n  background-repeat: repeat-y;\n"], ["\n  padding: 0 0 0 22;\n  background-image: url(", ");\n  background-repeat: repeat-y;\n"])), tree_node_children_png_1.default);
var Label = styled_components_1.default.span(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n  outline: none;\n  padding: 1;\n\n  :focus {\n    border-width: 1;\n    border-style: dotted;\n    padding: 0;\n  }\n"], ["\n  outline: none;\n  padding: 1;\n\n  :focus {\n    border-width: 1;\n    border-style: dotted;\n    padding: 0;\n  }\n"])));
var Node = function (_a) {
    var _b = _a.children, children = _b === void 0 ? [] : _b, id = _a.id, _c = _a.iconName, iconName = _c === void 0 ? 'bat' : _c, label = _a.label, _d = _a.onClick, onClick = _d === void 0 ? function () { } : _d, rest = __rest(_a, ["children", "id", "iconName", "label", "onClick"]);
    var _e = React.useState(false), isOpen = _e[0], setIsOpen = _e[1];
    var hasChildren = children.length > 0;
    function getIconName() {
        if (!hasChildren) {
            return iconName || exports.icons.FILE_UNKNOWN;
        }
        if (isOpen) {
            return FOLDER_OPENED;
        }
        return FOLDER_CLOSED;
    }
    var onClickHandler = function (event) {
        onClick(event, {
            id: id,
            iconName: iconName,
            label: label,
            children: children,
        });
    };
    var onKeyDownHandler = function (event) {
        if (event.key === ' ') {
            setIsOpen(!isOpen);
            onClickHandler(event);
        }
    };
    return (React.createElement(NodeItem, __assign({ isOpen: isOpen }, rest),
        React.createElement(NodeInfo, null,
            hasChildren && (React.createElement(FolderStatus, { onClick: function () { return setIsOpen(!isOpen); } }, isOpen ? '-' : '+')),
            React.createElement(IconContainer, { hasChildren: hasChildren },
                React.createElement(Icon_1.default, { name: getIconName(), width: 14, height: 14 })),
            React.createElement(Label, { tabIndex: 0, onDoubleClick: function () { return setIsOpen(!isOpen); }, onClick: onClickHandler, onKeyDown: onKeyDownHandler }, label)),
        hasChildren && isOpen && (React.createElement(NodeChildren, null, children === null || children === void 0 ? void 0 : children.map(function (dataNode) { return (React.createElement(Node, __assign({ key: dataNode.id }, dataNode))); })))));
};
exports.default = Node;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6;
//# sourceMappingURL=Node.js.map