"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _dropdownItemButton = _interopRequireDefault(require("./dropdown-item-button"));
var _dropdownItemCheckbox = _interopRequireDefault(require("./dropdown-item-checkbox"));
var _dropdownItemLink = _interopRequireDefault(require("./dropdown-item-link"));
var _dropdownItemFileupload = _interopRequireDefault(require("./dropdown-item-fileupload"));
var _dropdownItemRadioButton = require("./dropdown-item-radio-button");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
const DropdownItemContent = ({
  option,
  onSelect,
  onMouseEnter = () => {},
  onMouseLeave = () => {},
  onCloseDropdown = () => {},
  isMultiLevelHandler,
  LinkComponent
}) => {
  const {
    isChecked,
    isFileUpload,
    isFolderUpload,
    link,
    radioOptions
  } = option;
  const closeDropdownTimoutRef = (0, _react.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);
  }
  (0, _react.useEffect)(() => () => {
    if (closeDropdownTimoutRef.current) {
      clearTimeout(closeDropdownTimoutRef.current);
    }
  }, []);
  if (radioOptions) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
      role: "group",
      className: "radio-group",
      children: radioOptions.map(o => /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemRadioButton.DropdownItemRadioButton, {
        groupIdentifier: option.groupIdentifier,
        option: {
          ...o,
          activeItemId: option.activeItemId
        },
        onChange: () => {
          onClick(null, o.id);
        },
        onCloseDropdown: onCloseDropdown
      }, o.id))
    });
  }
  if (link) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemLink.default, {
      option: option,
      onClick: onClick,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      onCloseDropdown: onCloseDropdown,
      LinkComponent: LinkComponent
    });
  }
  if (isChecked !== undefined) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemCheckbox.default, {
      option: option,
      onChange: onClick,
      onCloseDropdown: onCloseDropdown
    });
  }
  if (isFileUpload || isFolderUpload) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemFileupload.default, {
      option: option,
      onSelect: onClick,
      onCloseDropdown: onCloseDropdown
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownItemButton.default, {
    option: option,
    onClick: onClick,
    isMultiLevelHandler: isMultiLevelHandler,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    onCloseDropdown: onCloseDropdown
  });
};
var _default = exports.default = DropdownItemContent;