import React, { useRef } from 'react';
import classNames from 'classnames';
import { useDrag, useDrop } from 'react-dnd';
import { faCircleNotch } from '@fortawesome/free-solid-svg-icons/faCircleNotch';
import { faXmark } from '@fortawesome/free-solid-svg-icons/faXmark';
import { Button, ACCENT_TYPES, BUTTON_SIZES } from '../button';
import { Tooltip, TOOLTIP_PLACEMENT_TYPES } from '../tooltip';
import { FontAwesomeIcon } from '../font-awesome-icon';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const DND_ITEM_TYPE = 'DND_CLOSABLE_TAB_TYPE';
export const ClosableTab = ({
  tab,
  index,
  tooltipClassName,
  isActive = false,
  isDisabled = false,
  onClick = () => {},
  onSelect = () => {},
  onClose = () => {},
  closeTabText,
  groupIdentifier,
  canBeDragged,
  isAnyItemDragging,
  setIsAnyItemDragging,
  onItemMove,
  getIndexByKey,
  onDragEnd
}) => {
  const id = [groupIdentifier, tab.key].filter(Boolean).join('-');
  const dropRef = useRef(null);
  const getTestId = (suffix = '') => tab.testId ? [tab.testId, suffix].filter(Boolean).join('-') : undefined;
  const [, drop] = useDrop({
    accept: DND_ITEM_TYPE,
    hover(item, monitor) {
      if (!dropRef.current) {
        return;
      }
      item.index = getIndexByKey(item.key);
      const dragIndex = item.index;
      const hoverIndex = index;
      if (dragIndex === hoverIndex) {
        return;
      }
      const hoverBoundingRect = dropRef.current.getBoundingClientRect();
      const hoverMiddleX = (hoverBoundingRect.right - hoverBoundingRect.left) / 2;
      const clientOffset = monitor.getClientOffset();
      const hoverClientX = (clientOffset?.x || 0) - hoverBoundingRect.left;
      if (dragIndex < hoverIndex && hoverClientX < hoverMiddleX) {
        return;
      }
      if (dragIndex > hoverIndex && hoverClientX > hoverMiddleX) {
        return;
      }
      onItemMove(dragIndex, hoverIndex);
      item.index = hoverIndex;
    },
    canDrop: () => {
      return canBeDragged;
    }
  });
  const [, drag] = useDrag({
    type: DND_ITEM_TYPE,
    item: tab,
    collect: monitor => {
      const isDragging = !!monitor.isDragging();
      if (isDragging) {
        setIsAnyItemDragging(true);
      }
      return {
        isDragging
      };
    },
    canDrag() {
      return canBeDragged;
    },
    end(item) {
      if (onDragEnd) {
        onDragEnd(item, item.index || 0);
      }
    }
  });
  drag(dropRef);
  drop(dropRef);
  return /*#__PURE__*/ /* eslint-disable-next-line jsx-a11y/no-static-element-interactions */_jsxs("div", {
    className: classNames('closable-tab', {
      'closable-tab-active': isActive,
      'closable-tab-disabled': isDisabled || tab.isDisabled
    }, tab.className),
    ref: dropRef,
    "test-id": getTestId(),
    onMouseDown: () => {
      setIsAnyItemDragging(true);
    },
    onMouseLeave: () => {
      setIsAnyItemDragging(false);
    },
    children: [/*#__PURE__*/_jsx("input", {
      type: "radio",
      className: "closable-tab-input",
      id: id,
      checked: isActive,
      value: tab.key,
      onClick: () => onClick(tab),
      onChange: () => isActive ? null : onSelect(tab),
      disabled: isDisabled || tab.isDisabled,
      "test-id": getTestId('input')
    }, tab.key), /*#__PURE__*/_jsx(Tooltip, {
      isDisabled: isAnyItemDragging,
      content: tab.tooltip,
      className: tooltipClassName,
      placement: tab.tooltipPlacement ?? TOOLTIP_PLACEMENT_TYPES.TOP,
      children: /*#__PURE__*/_jsxs("label", {
        htmlFor: id,
        className: "closable-tab-label",
        "test-id": getTestId('label'),
        children: [tab.label, tab.isProcessing ? /*#__PURE__*/_jsx(FontAwesomeIcon, {
          icon: faCircleNotch,
          spin: true,
          className: "closable-tab-processing-icon",
          "test-id": getTestId('processing')
        }) : null]
      }, "tabgroup-label")
    }), /*#__PURE__*/_jsx(Button, {
      className: "closable-tab-close",
      accentType: ACCENT_TYPES.COMMAND,
      size: BUTTON_SIZES.SM,
      testId: getTestId('close'),
      isDisabled: isDisabled || tab.isDisabled,
      onClick: () => onClose(tab),
      "aria-label": closeTabText,
      children: /*#__PURE__*/_jsx(FontAwesomeIcon, {
        icon: faXmark
      })
    })]
  });
};