function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import cx from 'classnames';
import { EditorState } from 'prosemirror-state';
import { Transform } from 'prosemirror-transform';
import { EditorView } from 'prosemirror-view';
import * as React from 'react';
import CommandButton from './CommandButton.js';
import CommandMenuButton from './CommandMenuButton.js';
import { CustomButton } from '@modusoperandi/licit-ui-commands';
import { COMMAND_GROUPS, parseLabel } from './EditorToolbarConfig.js';
import Icon from './Icon.js';
import ResizeObserver from './ResizeObserver.js';
import { UICommand } from '@modusoperandi/licit-doc-attrs-step';
import isReactClass from './isReactClass.js';
var EditorToolbar = /*#__PURE__*/function (_React$PureComponent) {
  function EditorToolbar() {
    var _this;
    _classCallCheck(this, EditorToolbar);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, EditorToolbar, [].concat(args));
    _defineProperty(_this, "_body", null);
    _defineProperty(_this, "props", void 0);
    _defineProperty(_this, "state", {
      expanded: false,
      wrapped: null
    });
    _defineProperty(_this, "_renderButtonsGroup", function (group, index) {
      var buttons = Object.keys(group).map(function (label) {
        var obj = group[label];
        if (isReactClass(obj)) {
          // JSX requies the component to be named with upper camel case.
          var ThatComponent = obj;
          var _this$props = _this.props,
            editorState = _this$props.editorState,
            editorView = _this$props.editorView,
            dispatchTransaction = _this$props.dispatchTransaction;
          return /*#__PURE__*/React.createElement(ThatComponent, {
            dispatch: dispatchTransaction,
            editorState: editorState,
            editorView: editorView,
            key: label
          });
        } else if (obj instanceof UICommand) {
          return _this._renderButton(label, obj);
        } else if (Array.isArray(obj)) {
          return _this._renderMenuButton(label, obj);
        } else {
          return null;
        }
      }).filter(Boolean);
      return /*#__PURE__*/React.createElement("div", {
        className: "czi-custom-buttons",
        key: 'g' + String(index)
      }, buttons);
    });
    _defineProperty(_this, "_renderMenuButton", function (label, commandGroups) {
      var _this$props2 = _this.props,
        editorState = _this$props2.editorState,
        editorView = _this$props2.editorView,
        disabled = _this$props2.disabled,
        dispatchTransaction = _this$props2.dispatchTransaction;
      var _parseLabel = parseLabel(label),
        icon = _parseLabel.icon,
        title = _parseLabel.title;
      return /*#__PURE__*/React.createElement(CommandMenuButton, {
        commandGroups: commandGroups,
        disabled: disabled,
        dispatch: dispatchTransaction,
        editorState: editorState,
        editorView: editorView,
        icon: icon,
        key: label,
        label: icon ? null : title,
        title: title
      });
    });
    _defineProperty(_this, "_renderButton", function (label, command) {
      var _this$props3 = _this.props,
        disabled = _this$props3.disabled,
        editorState = _this$props3.editorState,
        editorView = _this$props3.editorView,
        dispatchTransaction = _this$props3.dispatchTransaction;
      var _parseLabel2 = parseLabel(label),
        icon = _parseLabel2.icon,
        title = _parseLabel2.title;
      return /*#__PURE__*/React.createElement(CommandButton, {
        command: command,
        disabled: disabled,
        dispatch: dispatchTransaction,
        editorState: editorState,
        editorView: editorView,
        icon: icon,
        key: label,
        label: icon ? null : title,
        title: title
      });
    });
    _defineProperty(_this, "_onBodyRef", function (ref) {
      if (ref) {
        _this._body = ref;
        // Mounting
        ResizeObserver.observe(ref, _this._checkIfContentIsWrapped);
      } else if (_this._body) {
        // Unmounting
        ResizeObserver.unobserve(_this._body);
        _this._body = null;
      }
    });
    _defineProperty(_this, "_checkIfContentIsWrapped", function () {
      var ref = _this._body;
      if (ref) {
        var startAnchor = ref.firstChild;
        var endAnchor = ref.lastChild;
        if (startAnchor && endAnchor) {
          var wrapped = startAnchor.offsetTop < endAnchor.offsetTop;
          _this.setState({
            wrapped: wrapped
          });
        }
      }
    });
    _defineProperty(_this, "_toggleExpansion", function (expanded) {
      _this.setState({
        expanded: !expanded
      });
    });
    return _this;
  }
  _inherits(EditorToolbar, _React$PureComponent);
  return _createClass(EditorToolbar, [{
    key: "render",
    value: function render() {
      var _this$state = this.state,
        wrapped = _this$state.wrapped,
        expanded = _this$state.expanded;
      var className = cx('czi-editor-toolbar', {
        expanded: expanded,
        wrapped: wrapped
      });
      var wrappedButton = wrapped ? /*#__PURE__*/React.createElement(CustomButton, {
        active: expanded,
        className: "czi-editor-toolbar-expand-button",
        icon: Icon.get('more_horiz'),
        key: "expand",
        onClick: this._toggleExpansion,
        title: "More",
        value: expanded
      }) : null;

      // Start with static button controls and append any button groups
      // supplied by plugins
      var commandGroups = COMMAND_GROUPS.concat((this.props.editorState.plugins || []
      // This should have been a simple property instead of a function:
      //  p => p.buttonGroup
      // but changing it now would mean finding every plugin that was
      // implemented this way.
      ).map(function (p) {
        var _p$initButtonCommands;
        return (_p$initButtonCommands = p.initButtonCommands) === null || _p$initButtonCommands === void 0 ? void 0 : _p$initButtonCommands.call(p);
      }).filter(Boolean)).map(this._renderButtonsGroup).filter(Boolean);
      return /*#__PURE__*/React.createElement("div", {
        className: className
      }, /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-flex"
      }, /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-body"
      }, /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-body-content",
        ref: this._onBodyRef
      }, /*#__PURE__*/React.createElement("i", {
        className: "czi-editor-toolbar-wrapped-anchor"
      }), commandGroups, /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-background"
      }, /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-background-line"
      }), /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-background-line"
      }), /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-background-line"
      }), /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-background-line"
      }), /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-background-line"
      })), /*#__PURE__*/React.createElement("i", {
        className: "czi-editor-toolbar-wrapped-anchor"
      })), wrappedButton), /*#__PURE__*/React.createElement("div", {
        className: "czi-editor-toolbar-footer"
      })));
    }
  }]);
}(React.PureComponent);
export default EditorToolbar;