"use strict";

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

var _edgeLabel = _interopRequireDefault(require("./edgeLabel"));

var _node = _interopRequireDefault(require("./node"));

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

var _reactCraftAiTooltip = _interopRequireDefault(require("react-craft-ai-tooltip"));

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

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { keys.push.apply(keys, Object.getOwnPropertySymbols(object)); } if (enumerableOnly) keys = keys.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { if (i % 2) { var source = arguments[i] != null ? arguments[i] : {}; ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(arguments[i])); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(arguments[i], key)); }); } } return target; }

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; }

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; }

var NodesEdgesLabels = _react.default.memo(function (_ref) {
  var links = _ref.links,
      configuration = _ref.configuration,
      onShowTooltip = _ref.onShowTooltip,
      onHideTooltip = _ref.onHideTooltip;
  return _react.default.createElement(_react.default.Fragment, null, links.map(function (hLink, index) {
    return _react.default.createElement(_edgeLabel.default, {
      key: index,
      hLink: hLink,
      configuration: configuration,
      onShowTooltip: onShowTooltip,
      onHideTooltip: onHideTooltip
    });
  }));
});

NodesEdgesLabels.displayName = 'NodesEdgesLabels';
NodesEdgesLabels.propTypes = {
  configuration: _propTypes.default.object.isRequired,
  links: _propTypes.default.array.isRequired,
  onShowTooltip: _propTypes.default.func.isRequired,
  onHideTooltip: _propTypes.default.func.isRequired
};

var NodesNodes = _react.default.memo(function (_ref2) {
  var descendants = _ref2.descendants,
      selectedNodePath = _ref2.selectedNodePath,
      lastFoldedNode = _ref2.lastFoldedNode,
      onSelectNode = _ref2.onSelectNode,
      _onToggleSubtreeFold = _ref2.onToggleSubtreeFold,
      onShowTooltip = _ref2.onShowTooltip,
      onHideTooltip = _ref2.onHideTooltip,
      interpreter = _ref2.interpreter;
  return _react.default.createElement(_react.default.Fragment, null, descendants.map(function (hNode, index) {
    return _react.default.createElement(_node.default, {
      key: index,
      hNode: hNode,
      selected: hNode.path === selectedNodePath,
      showButton: hNode.path == lastFoldedNode // eslint-disable-next-line react/jsx-no-bind
      ,
      onSelectNode: onSelectNode ? function () {
        return onSelectNode(hNode.path);
      } : undefined // eslint-disable-next-line react/jsx-no-bind
      ,
      onToggleSubtreeFold: function onToggleSubtreeFold() {
        return _onToggleSubtreeFold(hNode);
      },
      onShowTooltip: onShowTooltip,
      onHideTooltip: onHideTooltip,
      interpreter: interpreter
    });
  }));
});

NodesNodes.displayName = 'NodesNodes';
NodesNodes.propTypes = {
  descendants: _propTypes.default.array.isRequired,
  selectedNodePath: _propTypes.default.string,
  lastFoldedNode: _propTypes.default.string,
  onSelectNode: _propTypes.default.func,
  onToggleSubtreeFold: _propTypes.default.func.isRequired,
  onShowTooltip: _propTypes.default.func.isRequired,
  onHideTooltip: _propTypes.default.func.isRequired,
  interpreter: _propTypes.default.object.isRequired
};

var Nodes = function Nodes(_ref3) {
  var interpreter = _ref3.interpreter,
      hierarchy = _ref3.hierarchy,
      selectedNodePath = _ref3.selectedNodePath,
      lastFoldedNode = _ref3.lastFoldedNode,
      selectable = _ref3.selectable,
      updateSelectedNode = _ref3.updateSelectedNode,
      onToggleSubtreeFold = _ref3.onToggleSubtreeFold,
      configuration = _ref3.configuration;

  var _useState = (0, _react.useState)({
    show: false,
    text: '',
    ref: null,
    placement: 'bottom'
  }),
      _useState2 = _slicedToArray(_useState, 2),
      tooltip = _useState2[0],
      setTooltip = _useState2[1];

  var hideTooltip = (0, _react.useCallback)(function () {
    return setTooltip(function (tooltip) {
      return _objectSpread({}, tooltip, {
        show: false,
        placement: 'bottom'
      });
    });
  }, []);
  var showTooltip = (0, _react.useCallback)(function (ref, text) {
    setTooltip(function (tooltip) {
      return _objectSpread({}, tooltip, {
        show: true,
        text: text,
        ref: ref
      });
    });
  }, []);
  var updateTooltipPlacement = (0, _react.useCallback)(function (placeTooltipTop) {
    var newPlacement = placeTooltipTop ? 'top' : 'bottom';
    setTooltip(function (tooltip) {
      if (tooltip.placement !== newPlacement) {
        return _objectSpread({}, tooltip, {
          placement: newPlacement
        });
      }

      return tooltip;
    });
  }, []);
  var selectNode = (0, _react.useCallback)(updateSelectedNode ? function (selectedNodeTreePath) {
    updateSelectedNode(selectedNodeTreePath);
  } : null, [updateSelectedNode]);
  var toggleSubtreeFold = (0, _react.useCallback)(function (hNode) {
    if (onToggleSubtreeFold) {
      onToggleSubtreeFold(hNode);
    }
  }, [onToggleSubtreeFold]);
  var descendants = (0, _react.useMemo)(function () {
    return hierarchy.descendants();
  }, [hierarchy]);
  var links = (0, _react.useMemo)(function () {
    return hierarchy.links();
  }, [hierarchy]);
  return _react.default.createElement("div", {
    style: {
      position: 'relative'
    }
  }, _react.default.createElement(NodesNodes, {
    descendants: descendants,
    selectedNodePath: selectedNodePath,
    lastFoldedNode: lastFoldedNode,
    onSelectNode: selectNode,
    onToggleSubtreeFold: toggleSubtreeFold,
    onShowTooltip: showTooltip,
    onHideTooltip: hideTooltip,
    interpreter: interpreter
  }), _react.default.createElement(NodesEdgesLabels, {
    links: links,
    configuration: configuration,
    onShowTooltip: showTooltip,
    onHideTooltip: hideTooltip
  }), _react.default.createElement(_reactCraftAiTooltip.default, {
    style: {
      pointerEvents: 'none'
    } // disable click on tooltip
    ,
    show: selectable && tooltip.show,
    placement: tooltip.placement,
    target: tooltip.ref,
    onPlacementUpdated: updateTooltipPlacement
  }, tooltip.text));
};

Nodes.propTypes = {
  selectable: _propTypes.default.bool.isRequired,
  updateSelectedNode: _propTypes.default.func,
  configuration: _propTypes.default.object.isRequired,
  hierarchy: _propTypes.default.object.isRequired,
  interpreter: _propTypes.default.object.isRequired,
  selectedNodePath: _propTypes.default.string,
  lastFoldedNode: _propTypes.default.string,
  onToggleSubtreeFold: _propTypes.default.func.isRequired
};
var _default = Nodes;
exports.default = _default;