var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
import * as React from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { prefixCls, ToggleableToolboxLayoutDirection } from "../index";
import NormalToolbox from "../normal-toolbox";
import classnames from 'classnames';
var toggleableToolboxGap = 5;
var toolBoxWidth = 32;
var normalToolboxStyle = {
  position: 'absolute',
  left: 0,
  top: 0
};
var ToggleableToolbox = /*#__PURE__*/React.memo(function (props) {
  var id = props.id,
      onClick = props.onClick,
      subItems = props.subItems,
      _a = props.layoutDirection,
      layoutDirection = _a === void 0 ? ToggleableToolboxLayoutDirection.right : _a,
      style = props.style,
      getTooltipContainer = props.getTooltipContainer;
  var timeIdRef = useRef();
  var enterDelayTimeIdRef = useRef();

  var _b = useState(null),
      container = _b[0],
      setContainer = _b[1];

  var _c = useState(false),
      subItemsVisible = _c[0],
      setSubItemsVisible = _c[1];

  var layoutInColumn = layoutDirection === ToggleableToolboxLayoutDirection.top || layoutDirection === ToggleableToolboxLayoutDirection.bottom;
  var handleMouseEnter = useCallback(function () {
    window.clearTimeout(timeIdRef.current);
    window.clearTimeout(enterDelayTimeIdRef.current);
    enterDelayTimeIdRef.current = window.setTimeout(function () {
      return setSubItemsVisible(true);
    }, 150);
  }, []);
  var handleMouseLeave = useCallback(function () {
    window.clearTimeout(enterDelayTimeIdRef.current);
    timeIdRef.current = window.setTimeout(function () {
      setSubItemsVisible(false);
    }, 300);
  }, []);
  var refCallback = useCallback(function (node) {
    if (node) {
      setContainer(node);
    }
  }, []);
  var initialStyle = useMemo(function () {
    if (container) {
      switch (layoutDirection) {
        case ToggleableToolboxLayoutDirection.top:
          return {
            top: "-" + (subItems.length - 1) * (toolBoxWidth + toggleableToolboxGap) + "px",
            flexDirection: 'column-reverse'
          };

        case ToggleableToolboxLayoutDirection.right:
          return {};

        case ToggleableToolboxLayoutDirection.bottom:
          return {
            flexDirection: 'column'
          };

        case ToggleableToolboxLayoutDirection.left:
          return {
            left: "-" + (subItems.length - 1) * (toolBoxWidth + toggleableToolboxGap) + "px",
            flexDirection: 'row-reverse'
          };
      }
    }

    return {};
  }, [container, layoutDirection, subItems.length]);
  var transitionStyle = useMemo(function () {
    var _transitionStyle = {
      opacity: 1,
      pointerEvents: 'auto',
      transition: 'transform 0.2s ease-in-out, opacity 0.5s'
    };

    switch (layoutDirection) {
      case ToggleableToolboxLayoutDirection.top:
        return __assign(__assign(__assign({}, initialStyle), _transitionStyle), {
          transform: "translateY(-" + (toolBoxWidth + toggleableToolboxGap) + "px)"
        });

      case ToggleableToolboxLayoutDirection.right:
        return __assign(__assign(__assign({}, initialStyle), _transitionStyle), {
          transform: "translateX(" + (toolBoxWidth + toggleableToolboxGap) + "px)"
        });

      case ToggleableToolboxLayoutDirection.bottom:
        return __assign(__assign(__assign({}, initialStyle), _transitionStyle), {
          transform: "translateY(" + (toolBoxWidth + toggleableToolboxGap) + "px)"
        });

      case ToggleableToolboxLayoutDirection.left:
        return __assign(__assign(__assign({}, initialStyle), _transitionStyle), {
          transform: "translateX(-" + (toolBoxWidth + toggleableToolboxGap) + "px)"
        });

      default:
        return {};
    }
  }, [initialStyle, layoutDirection]);
  useEffect(function () {
    return function () {
      return clearTimeout(timeIdRef.current);
    };
  }, []);
  return _jsxs("div", __assign({
    className: prefixCls + "-toggleable-toolbox",
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    ref: refCallback
  }, {
    children: [_jsx("div", __assign({
      className: classnames(prefixCls + "-toggleable-toolbox-sub-items-container", {
        'toggleable-toolbox-column-gap': layoutInColumn,
        'toggleable-toolbox-row-gap': !layoutInColumn
      }),
      style: __assign(__assign(__assign({}, initialStyle), subItemsVisible ? transitionStyle : {}), style)
    }, {
      children: subItems.map(function (item, index) {
        return /*#__PURE__*/_createElement(NormalToolbox, __assign({}, item, {
          id: id + "-" + index,
          key: item.id,
          getTooltipContainer: getTooltipContainer
        }));
      })
    }), void 0), _jsx(NormalToolbox, __assign({}, props, {
      onClick: onClick,
      style: normalToolboxStyle
    }), void 0)]
  }), void 0);
});
export default ToggleableToolbox;

