import React, { useCallback } from 'react';
import { ToolbarUIProvider } from '@atlaskit/editor-toolbar';
export var EditorToolbarUIProvider = function EditorToolbarUIProvider(_ref) {
  var children = _ref.children,
    api = _ref.api,
    isDisabled = _ref.isDisabled,
    popupsMountPoint = _ref.popupsMountPoint,
    popupsBoundariesElement = _ref.popupsBoundariesElement,
    popupsScrollableElement = _ref.popupsScrollableElement,
    fireAnalyticsEvent = _ref.fireAnalyticsEvent,
    keyboardNavigation = _ref.keyboardNavigation;
  var onDropdownOpenChanged = useCallback(function (_ref2) {
    var isOpen = _ref2.isOpen,
      event = _ref2.event;
    if (!isOpen) {
      // Only refocus the editor when the dropdown closes via mouse or programmatic close.
      // When closed via keyboard Escape, keep focus on the trigger for better keyboard UX.
      var isKeyboardEscape = event instanceof KeyboardEvent && event.key === 'Escape';
      var shouldFocusEditor = !isKeyboardEscape;
      if (shouldFocusEditor) {
        // On Dropdown closed, focus is returned to trigger button by default in requestAnimationFrame
        // Hence, `.focus()` should also be called in requestAnimationFrame
        setTimeout(function () {
          return requestAnimationFrame(function () {
            api === null || api === void 0 || api.core.actions.focus({
              scrollIntoView: false
            });
          });
        }, 1);
      }
    }
  }, [api]);
  return /*#__PURE__*/React.createElement(ToolbarUIProvider, {
    onDropdownOpenChanged: onDropdownOpenChanged,
    preventDefaultOnMouseDown: true,
    isDisabled: isDisabled,
    popupsMountPoint: popupsMountPoint,
    popupsBoundariesElement: popupsBoundariesElement,
    popupsScrollableElement: popupsScrollableElement,
    fireAnalyticsEvent: fireAnalyticsEvent,
    keyboardNavigation: keyboardNavigation
  }, children);
};