"use strict";

var __assign = void 0 && (void 0).__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);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});

var jsx_runtime_1 = require("react/jsx-runtime");

var react_1 = require("react");

var React = __importStar(require("react"));

var react_2 = require("react");

var index_1 = require("../index");

var normal_toolbox_1 = __importDefault(require("../normal-toolbox"));

var classnames_1 = __importDefault(require("classnames"));

var toggleableToolboxGap = 5;
var toolBoxWidth = 32;
var normalToolboxStyle = {
  position: 'absolute',
  left: 0,
  top: 0
};
var ToggleableToolbox = React.memo(function (props) {
  var id = props.id,
      onClick = props.onClick,
      subItems = props.subItems,
      _a = props.layoutDirection,
      layoutDirection = _a === void 0 ? index_1.ToggleableToolboxLayoutDirection.right : _a,
      style = props.style;
  var timeIdRef = react_2.useRef();
  var enterDelayTimeIdRef = react_2.useRef();

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

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

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

        case index_1.ToggleableToolboxLayoutDirection.right:
          return {};

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

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

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

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

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

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

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

      default:
        return {};
    }
  }, [initialStyle, layoutDirection]);
  react_2.useEffect(function () {
    return function () {
      return clearTimeout(timeIdRef.current);
    };
  }, []);
  return jsx_runtime_1.jsxs("div", __assign({
    className: index_1.prefixCls + "-toggleable-toolbox",
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    ref: refCallback
  }, {
    children: [jsx_runtime_1.jsx("div", __assign({
      className: classnames_1["default"](index_1.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 react_1.createElement(normal_toolbox_1["default"], __assign({}, item, {
          id: id + "-" + index,
          key: item.id
        }));
      })
    }), void 0), jsx_runtime_1.jsx(normal_toolbox_1["default"], __assign({}, props, {
      onClick: onClick,
      style: normalToolboxStyle
    }), void 0)]
  }), void 0);
});
exports["default"] = ToggleableToolbox;

