"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.icons = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _Icon = _interopRequireDefault(require("../Icon"));

var _treeMid = _interopRequireDefault(require("../shared/assets/tree-mid.png"));

var _treeLast = _interopRequireDefault(require("../shared/assets/tree-last.png"));

var _treeNodeChildren = _interopRequireDefault(require("../shared/assets/tree-node-children.png"));

var _icons;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var FOLDER_CLOSED = 'folder';
var FOLDER_OPENED = 'folder_open';
var icons = (_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'
}, _defineProperty(_icons, "FILE_TEXT", 'file_text'), _defineProperty(_icons, "FILE_EXECUTABLE", 'bat_exec'), _icons);
exports.icons = icons;

var NodeItem = _styledComponents.default.div.withConfig({
  displayName: "Node__NodeItem",
  componentId: "sc-2mja1h-0"
})(["list-style-type:none;background-repeat:no-repeat;background-image:url(", ");&:last-child{background-image:url(", ");}"], _treeMid.default, function (props) {
  return props.isOpen ? _treeMid.default : _treeLast.default;
});

var NodeInfo = _styledComponents.default.div.withConfig({
  displayName: "Node__NodeInfo",
  componentId: "sc-2mja1h-1"
})(["display:flex;align-items:center;user-select:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;"]);

var FolderStatus = _styledComponents.default.div.withConfig({
  displayName: "Node__FolderStatus",
  componentId: "sc-2mja1h-2"
})(["display:flex;align-items:center;justify-content:center;width:10px;height:10px;border:1px solid black;background-color:white;font-size:11px;"]);

var IconContainer = _styledComponents.default.div.withConfig({
  displayName: "Node__IconContainer",
  componentId: "sc-2mja1h-3"
})(["display:flex;align-items:center;justify-content:center;width:20px;height:20px;margin-right:6px;margin-left:", "px;"], function (props) {
  return props.hasChildren ? 8 : 18;
});

var NodeChildren = _styledComponents.default.ul.withConfig({
  displayName: "Node__NodeChildren",
  componentId: "sc-2mja1h-4"
})(["padding:0px 0px 0px 22px;background-image:url(", ");background-repeat:repeat-y;"], _treeNodeChildren.default);

var Node = function Node(_ref) {
  var children = _ref.children,
      id = _ref.id,
      iconName = _ref.iconName,
      label = _ref.label,
      _onClick = _ref.onClick;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      isOpen = _useState2[0],
      setIsOpen = _useState2[1];

  var hasChildren = children.length > 0;

  function getIconName() {
    return !hasChildren ? iconName || icons.FILE_UNKNOWN : isOpen ? FOLDER_OPENED : FOLDER_CLOSED;
  }

  return _react.default.createElement(NodeItem, {
    isOpen: isOpen
  }, _react.default.createElement(NodeInfo, null, hasChildren && _react.default.createElement(FolderStatus, {
    onClick: function onClick() {
      return setIsOpen(!isOpen);
    }
  }, isOpen ? '-' : '+'), _react.default.createElement(IconContainer, {
    hasChildren: hasChildren
  }, _react.default.createElement(_Icon.default, {
    name: getIconName(),
    width: 14,
    height: 14
  })), _react.default.createElement("span", {
    onDoubleClick: function onDoubleClick() {
      return setIsOpen(!isOpen);
    },
    onClick: function onClick(event) {
      return _onClick(event, {
        id: id,
        iconName: iconName,
        label: label,
        children: children
      });
    }
  }, label)), hasChildren && isOpen && _react.default.createElement(NodeChildren, null, children.map(function (dataNode) {
    return _react.default.createElement(Node, _extends({
      key: dataNode.id
    }, dataNode));
  })));
};

Node.defaultProps = {
  label: '',
  iconName: null,
  children: [],
  onClick: function onClick() {}
};
Node.propTypes = {
  label: _propTypes.default.string,
  iconName: _propTypes.default.string,
  children: _propTypes.default.array,
  onClick: _propTypes.default.func
};
var _default = Node;
exports.default = _default;