import React, { useState, useRef, useEffect, useCallback } from 'react';
import { faFileCode } from '@fortawesome/free-solid-svg-icons/faFileCode';
import { useTreeNodeNavigation } from '../hooks';
import { FileTreeItem, FileTreeItemContent } from '.';

// 'Click elsewhere' helper
import { jsx as _jsx } from "react/jsx-runtime";
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] = useState(false);
  const itemRef = useRef();
  const isMouseEvent = 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
  } = 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 = useCallback(() => {
    isMouseEvent.current = true;
  }, []);
  useEffect(() => removeClickListener, []);
  const setRef = useCallback(r => {
    ref(r);
    itemRef.current = r;
  }, [ref, itemRef]);
  return (
    /*#__PURE__*/
    // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
    _jsx("li", {
      ref: setRef,
      className: "branch-item",
      "data-id": `file-tree-item-${item.id}`,
      "test-id": `${itemTestId}-${currentLevel}`,
      onMouseDown: onMouseDown,
      ...treeItemAttrs,
      children: /*#__PURE__*/_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__*/_jsx(ItemContentComponent, {
          item: item,
          isActive: isActive,
          defaultIcon: faFileCode,
          disabled: disabled,
          ...itemContentComponentProps
        })
      })
    })
  );
};
export default FileTreeFileComponent;