import React, { useRef, useEffect } from 'react';
import DropdownItemButton from './dropdown-item-button';
import DropdownItemCheckbox from './dropdown-item-checkbox';
import DropdownItemLink from './dropdown-item-link';
import DropdownItemFileUpload from './dropdown-item-fileupload';
import { DropdownItemRadioButton } from './dropdown-item-radio-button';
import { jsx as _jsx } from "react/jsx-runtime";
const DropdownItemContent = ({
  option,
  onSelect,
  onMouseEnter = () => {},
  onMouseLeave = () => {},
  onCloseDropdown = () => {},
  isMultiLevelHandler,
  LinkComponent
}) => {
  const {
    isChecked,
    isFileUpload,
    isFolderUpload,
    link,
    radioOptions
  } = option;
  const closeDropdownTimoutRef = useRef();
  function onClick(value, files) {
    // this is only the case for a Link and it is here to prevent double action: onKeydown + onClick
    if (closeDropdownTimoutRef.current) {
      return;
    }
    if (isMultiLevelHandler) {
      onMouseEnter(option.level);
      return;
    }
    if (onSelect) {
      if (files) {
        onSelect(option, files);
      } else {
        onSelect(option);
      }
    }
    onMouseLeave(value);
    if (option.shouldNotCloseDropdown) {
      return;
    }

    // delay the closing of the dropdown to allow for the click event to be handled
    if (option.link) {
      closeDropdownTimoutRef.current = setTimeout(() => {
        onCloseDropdown(value);
        closeDropdownTimoutRef.current = null;
      });
      return;
    }
    return onCloseDropdown(value);
  }
  useEffect(() => () => {
    if (closeDropdownTimoutRef.current) {
      clearTimeout(closeDropdownTimoutRef.current);
    }
  }, []);
  if (radioOptions) {
    return /*#__PURE__*/_jsx("ul", {
      role: "group",
      className: "radio-group",
      children: radioOptions.map(o => /*#__PURE__*/_jsx(DropdownItemRadioButton, {
        groupIdentifier: option.groupIdentifier,
        option: {
          ...o,
          activeItemId: option.activeItemId
        },
        onChange: () => {
          onClick(null, o.id);
        },
        onCloseDropdown: onCloseDropdown
      }, o.id))
    });
  }
  if (link) {
    return /*#__PURE__*/_jsx(DropdownItemLink, {
      option: option,
      onClick: onClick,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      onCloseDropdown: onCloseDropdown,
      LinkComponent: LinkComponent
    });
  }
  if (isChecked !== undefined) {
    return /*#__PURE__*/_jsx(DropdownItemCheckbox, {
      option: option,
      onChange: onClick,
      onCloseDropdown: onCloseDropdown
    });
  }
  if (isFileUpload || isFolderUpload) {
    return /*#__PURE__*/_jsx(DropdownItemFileUpload, {
      option: option,
      onSelect: onClick,
      onCloseDropdown: onCloseDropdown
    });
  }
  return /*#__PURE__*/_jsx(DropdownItemButton, {
    option: option,
    onClick: onClick,
    isMultiLevelHandler: isMultiLevelHandler,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onCloseDropdown: onCloseDropdown
  });
};
export default DropdownItemContent;