"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _faFileCode = require("@fortawesome/free-solid-svg-icons/faFileCode");
var _hooks = require("../hooks");
var _ = require(".");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// 'Click elsewhere' helper
const clickElsewhereHelper = (parentRef, callBack) => {
  function handleClickElsewhere(e) {
    // Execute callback and remove event listener
    if (callBack && parentRef && parentRef.current && !parentRef.current.contains(e.target)) {
      callBack(e);
      removeClickListener();
    }
  }
  function addClickListener() {
    document.body.addEventListener('click', handleClickElsewhere);
  }
  function removeClickListener() {
    document.body.removeEventListener('click', handleClickElsewhere);
  }
  return {
    addClickListener,
    removeClickListener
  };
};
const FileTreeFileComponent = ({
  item,
  FileActions,
  actions,
  activateOnMouseOver = true,
  testId = 'file-tree-file',
  itemTestId = 'file-tree-item-wrapper',
  ItemContentComponent = _.FileTreeItemContent,
  disabled = false,
  itemContentComponentProps,
  rowSize
}) => {
  const [isActive, setIsActive] = (0, _react.useState)(false);
  const itemRef = (0, _react.useRef)();
  const isMouseEvent = (0, _react.useRef)(false);
  const deActivateItem = () => setIsActive(false);
  const onMouseOver = mouseEnter => {
    // Apply value only if it has changed
    if (mouseEnter && !isActive || !mouseEnter && isActive) {
      setIsActive(mouseEnter);
    }
  };
  const {
    addClickListener,
    removeClickListener
  } = clickElsewhereHelper(itemRef, deActivateItem);
  const {
    treeItemAttrs,
    node,
    ref
  } = (0, _hooks.useTreeNodeNavigation)({
    id: item.id,
    isSelected: isActive,
    onToggleFocused: value => {
      if (!isMouseEvent.current) {
        setIsActive(value);
      }
      // setting isMouseEvent to false here, as it already served its' purpose
      // covers the edge case when user does mousedown on element then moves cursor
      // and does mouseup elsewhere.
      isMouseEvent.current = false;
    }
  });
  const currentLevel = node.parents.length + 1;
  const onClick = () => {
    if (isActive) {
      deActivateItem();
      removeClickListener();
      return;
    }
    addClickListener();
    setIsActive(true);
  };

  // we need isMouseEvent flag not to set active state in onToggleFocused above in case
  // activateOnMouseOver is set to false, cause it will lead to buggy behaviour described
  // in this ticket: https://datarobot.atlassian.net/browse/UIUX-11811
  const onMouseDown = (0, _react.useCallback)(() => {
    isMouseEvent.current = true;
  }, []);
  (0, _react.useEffect)(() => removeClickListener, []);
  const setRef = (0, _react.useCallback)(r => {
    ref(r);
    itemRef.current = r;
  }, [ref, itemRef]);
  return (
    /*#__PURE__*/
    // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
    (0, _jsxRuntime.jsx)("li", {
      ref: setRef,
      className: "branch-item",
      "data-id": `file-tree-item-${item.id}`,
      "test-id": `${itemTestId}-${currentLevel}`,
      onMouseDown: onMouseDown,
      ...treeItemAttrs,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_.FileTreeItem, {
        item: item,
        ItemActions: FileActions,
        actions: actions,
        isActive: isActive,
        onClick: !disabled && !activateOnMouseOver && onClick || undefined,
        onMouseOver: !disabled && activateOnMouseOver && onMouseOver || undefined,
        testId: testId,
        disabled: disabled,
        rowSize: rowSize,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ItemContentComponent, {
          item: item,
          isActive: isActive,
          defaultIcon: _faFileCode.faFileCode,
          disabled: disabled,
          ...itemContentComponentProps
        })
      })
    })
  );
};
var _default = exports.default = FileTreeFileComponent;