'use strict';

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _inherits = require('@babel/runtime/helpers/inherits');
var _createSuper = require('@babel/runtime/helpers/createSuper');
var Button = require('@uidu/button');
var theme = require('@uidu/theme');
var React = require('react');
var styled = require('styled-components');
var main = require('./main-811eea00.cjs.prod.js');
var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var ExpandIcon = require('@atlaskit/icon/glyph/chevron-down');
var EditorDoneIcon = require('@atlaskit/icon/glyph/editor/done');
var menu = require('@uidu/menu');
var jsxRuntime = require('react/jsx-runtime');
require('@atlaskit/icon/glyph/editor/align-center');
require('@atlaskit/icon/glyph/editor/align-left');
require('@atlaskit/icon/glyph/editor/align-right');
require('@babel/runtime/helpers/objectSpread2');
require('prosemirror-state');
require('@uidu/adf-schema');
require('@babel/runtime/helpers/toConsumableArray');
require('./withAnalytics-cd63af43.cjs.prod.js');
require('./index-be5cf307.cjs.prod.js');
require('@uidu/editor-common');
require('@uidu/editor-json-transformer');
require('prosemirror-commands');
require('prosemirror-model');
require('prosemirror-transform');
require('prosemirror-utils');
require('prosemirror-view');
require('@uidu/editor-common/selection');
require('@uidu/adf-utils');
require('@uidu/mentions');
require('@uidu/editor-common/utils');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/regenerator');
require('@babel/runtime/helpers/typeof');
require('prosemirror-keymap');
require('prosemirror-history');
require('@uidu/editor-common/safe-plugin');
require('./index-2f033c32.cjs.prod.js');
require('prosemirror-tables');
require('@babel/runtime/helpers/createForOfIteratorHelper');
require('@uidu/analytics-listeners');
require('prosemirror-inputrules');
require('./ReactNodeView-0997bd10.cjs.prod.js');
require('@babel/runtime/helpers/get');
require('@babel/runtime/helpers/getPrototypeOf');
require('w3c-keyname');
require('@babel/runtime/helpers/slicedToArray');
require('@atlaskit/icon/glyph/editor/text-style');
require('react-intl');
require('@loadable/component');
require('prop-types');
require('@uidu/date');
require('@uidu/emoji');
require('@uidu/emoji/element');
require('@atlaskit/icon/glyph/editor/edit');
require('@atlaskit/icon/glyph/editor/media-center');
require('@atlaskit/icon/glyph/editor/media-full-width');
require('@atlaskit/icon/glyph/editor/media-wide');
require('@atlaskit/icon/glyph/editor/remove');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@atlaskit/icon/glyph/editor/file');
require('raf-schd');
require('@atlaskit/icon/glyph/cross-circle');
require('@atlaskit/icon/glyph/link');
require('@uidu/tooltip');
require('@atlaskit/icon/glyph/editor/layout-three-equal');
require('@atlaskit/icon/glyph/editor/layout-three-with-sidebars');
require('@atlaskit/icon/glyph/editor/layout-two-equal');
require('@atlaskit/icon/glyph/editor/layout-two-left-sidebar');
require('@atlaskit/icon/glyph/editor/layout-two-right-sidebar');
require('uuid');
require('@uidu/analytics-gas-types');
require('@atlaskit/icon/glyph/editor/mention');
require('@atlaskit/icon/glyph/chevron-right-circle');
require('@atlaskit/icon/glyph/editor/add');
require('@atlaskit/icon/glyph/editor/code');
require('@atlaskit/icon/glyph/editor/date');
require('@atlaskit/icon/glyph/editor/decision');
require('@atlaskit/icon/glyph/editor/emoji');
require('@atlaskit/icon/glyph/editor/horizontal-rule');
require('@atlaskit/icon/glyph/editor/info');
require('@atlaskit/icon/glyph/editor/more');
require('@atlaskit/icon/glyph/editor/task');
require('@atlaskit/icon/glyph/media-services/text');
require('@atlaskit/icon/glyph/quote');
require('@atlaskit/icon/glyph/status');
require('@fortawesome/free-solid-svg-icons');
require('@fortawesome/react-fontawesome');
require('@uidu/emoji/picker');
require('react-dom');
require('@atlaskit/icon/glyph/editor/bullet-list');
require('@atlaskit/icon/glyph/editor/number-list');
require('@atlaskit/icon/glyph/editor/close');
require('@uidu/media-filmstrip');
require('classnames');
require('@uidu/media-picker');
require('assert');
require('react-dom/client');
require('@atlaskit/icon/glyph/document-filled');
require('@uidu/media-card');
require('re-resizable');
require('@uidu/media-core');
require('@atlaskit/icon/glyph/chevron-left-large');
require('@uidu/analytics');
require('@atlaskit/icon/glyph/media-services/annotate');
require('@atlaskit/icon/glyph/editor/align-image-center');
require('@atlaskit/icon/glyph/editor/align-image-left');
require('@atlaskit/icon/glyph/editor/align-image-right');
require('@atlaskit/icon/glyph/editor/media-wrap-left');
require('@atlaskit/icon/glyph/editor/media-wrap-right');
require('@atlaskit/icon/glyph/editor/link');
require('@atlaskit/icon/glyph/shortcut');
require('@atlaskit/icon/glyph/editor/unlink');
require('@uidu/spinner');
require('@atlaskit/icon/glyph/editor/attachment');
require('@uidu/status/element');
require('@uidu/status/picker');
require('@uidu/editor-common/ui-color');
require('@uidu/droplist');
require('@atlaskit/icon/glyph/editor/collapse');
require('@atlaskit/icon/glyph/editor/expand');
require('prosemirror-schema-list');
require('@uidu/editor-markdown-transformer');
require('linkify-it');
require('markdown-it');
require('@atlaskit/icon/glyph/editor/background-color');
require('@babel/runtime/helpers/toArray');
require('@atlaskit/icon/base');
require('get-video-id');

function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

var styled__default = /*#__PURE__*/_interopDefault(styled);
var ExpandIcon__default = /*#__PURE__*/_interopDefault(ExpandIcon);
var EditorDoneIcon__default = /*#__PURE__*/_interopDefault(EditorDoneIcon);

var shallowEqual = function shallowEqual(objA, objB) {
  if (objA === objB) {
    return true;
  }
  if (objA == null || objB == null) {
    return false;
  }
  var keysA = Object.keys(objA);
  var keysB = Object.keys(objB);
  if (keysA.length !== keysB.length) {
    return false;
  }
  var bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
  for (var idx = 0; idx < keysA.length; idx++) {
    var key = keysA[idx];
    if (!bHasOwnProperty(key)) {
      return false;
    }
    if (objA[key] !== objB[key]) {
      return false;
    }
  }
  return true;
};
var compareArrays = function compareArrays(left, right) {
  var compareFn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : shallowEqual;
  if (left.length !== right.length) {
    return false;
  }
  for (var idx = 0; idx < left.length; idx++) {
    if (!compareFn(left[idx], right[idx])) {
      return false;
    }
  }
  return true;
};

var menuItemDimensions = {
  width: 175,
  height: 32
};
var Spacer = styled__default["default"].span.withConfig({
  displayName: "DropdownMenu__Spacer",
  componentId: "sc-zndc0s-0"
})(["display:flex;flex:1;padding:8px;"]);
var MenuContainer = styled__default["default"].div.withConfig({
  displayName: "DropdownMenu__MenuContainer",
  componentId: "sc-zndc0s-1"
})(["min-width:", "px;"], menuItemDimensions.width);
var itemSpacing = theme.gridSize() / 2;
var Dropdown$1 = /*#__PURE__*/function (_Component) {
  _inherits(Dropdown, _Component);
  var _super = _createSuper(Dropdown);
  function Dropdown() {
    _classCallCheck(this, Dropdown);
    return _super.apply(this, arguments);
  }
  _createClass(Dropdown, [{
    key: "render",
    value: function render() {
      var _this = this;
      var _this$props = this.props,
        hide = _this$props.hide,
        dispatchCommand = _this$props.dispatchCommand,
        items = _this$props.items;
      return /*#__PURE__*/jsxRuntime.jsx(MenuContainer, {
        children: items.filter(function (item) {
          return !item.hidden;
        }).map(function (item, idx) {
          return /*#__PURE__*/jsxRuntime.jsx(menu.ButtonItem, {
            isCompact: true,
            iconBefore: _this.renderSelected(item),
            onClick: function onClick() {
              hide();
              dispatchCommand(item.onClick);
            },
            isDisabled: item.disabled,
            children: item.title
          }, idx);
        })
      });
    }
  }, {
    key: "renderSelected",
    value: function renderSelected(item) {
      var selected = item.selected;
      if (selected !== undefined) {
        return selected ? /*#__PURE__*/jsxRuntime.jsx(EditorDoneIcon__default["default"], {
          primaryColor: theme.colors.B400,
          size: "small",
          label: "test question"
        }) : /*#__PURE__*/jsxRuntime.jsx(Spacer, {});
      }
      return undefined;
    }
  }]);
  return Dropdown;
}(React.Component);

var DropdownExpandContainer = styled__default["default"].span.withConfig({
  displayName: "Dropdown__DropdownExpandContainer",
  componentId: "sc-3zkct4-0"
})(["margin-left:-8px;"]);
var IconGroup = styled__default["default"].div.withConfig({
  displayName: "Dropdown__IconGroup",
  componentId: "sc-3zkct4-1"
})(["display:flex;"]);
var CompositeIcon = function CompositeIcon(_ref) {
  var icon = _ref.icon;
  return /*#__PURE__*/jsxRuntime.jsxs(IconGroup, {
    children: [icon, /*#__PURE__*/jsxRuntime.jsx(DropdownExpandContainer, {
      children: /*#__PURE__*/jsxRuntime.jsx(ExpandIcon__default["default"], {
        label: "Expand dropdown menu"
      })
    })]
  });
};
var Dropdown = /*#__PURE__*/function (_Component) {
  _inherits(Dropdown, _Component);
  var _super = _createSuper(Dropdown);
  function Dropdown() {
    var _this;
    _classCallCheck(this, Dropdown);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this), "state", {
      isOpen: false
    });
    _defineProperty(_assertThisInitialized(_this), "renderArrayOptions", function (options) {
      return /*#__PURE__*/jsxRuntime.jsx(Dropdown$1, {
        hide: _this.hide,
        dispatchCommand: _this.props.dispatchCommand,
        items: options
      });
    });
    _defineProperty(_assertThisInitialized(_this), "toggleOpen", function () {
      _this.setState({
        isOpen: !_this.state.isOpen
      });
    });
    _defineProperty(_assertThisInitialized(_this), "hide", function () {
      _this.setState({
        isOpen: false
      });
    });
    return _this;
  }
  _createClass(Dropdown, [{
    key: "render",
    value: function render() {
      var isOpen = this.state.isOpen;
      var _this$props = this.props,
        title = _this$props.title,
        icon = _this$props.icon,
        options = _this$props.options,
        dispatchCommand = _this$props.dispatchCommand,
        mountPoint = _this$props.mountPoint,
        boundariesElement = _this$props.boundariesElement,
        scrollableElement = _this$props.scrollableElement,
        hideExpandIcon = _this$props.hideExpandIcon;
      var trigger;
      if (icon) {
        var TriggerIcon = hideExpandIcon ? icon : /*#__PURE__*/jsxRuntime.jsx(CompositeIcon, {
          icon: icon
        });
        trigger = /*#__PURE__*/jsxRuntime.jsx(main.Button, {
          title: title,
          icon: TriggerIcon,
          onClick: this.toggleOpen,
          selected: isOpen
        });
      } else {
        trigger = /*#__PURE__*/jsxRuntime.jsx(main.Button, {
          iconAfter: /*#__PURE__*/jsxRuntime.jsx(DropdownExpandContainer, {
            children: /*#__PURE__*/jsxRuntime.jsx(ExpandIcon__default["default"], {
              label: "Expand dropdown menu"
            })
          }),
          onClick: this.toggleOpen,
          selected: isOpen,
          children: title
        });
      }

      /**
       * We want to change direction of our dropdowns a bit early,
       * not exactly when it hits the boundary.
       */
      var fitTolerance = 10;
      var fitWidth = Array.isArray(options) ? menuItemDimensions.width : options.width;
      var fitHeight = Array.isArray(options) ? options.length * menuItemDimensions.height + itemSpacing * 2 : options.height;
      return /*#__PURE__*/jsxRuntime.jsx(main.DropdownWithOuterListeners, {
        mountTo: mountPoint,
        boundariesElement: boundariesElement,
        scrollableElement: scrollableElement,
        isOpen: isOpen,
        handleClickOutside: this.hide,
        handleEscapeKeydown: this.hide,
        fitWidth: fitWidth + fitTolerance,
        fitHeight: fitHeight + fitTolerance,
        trigger: trigger,
        children: Array.isArray(options) ? this.renderArrayOptions(options) : options.render({
          hide: this.hide,
          dispatchCommand: dispatchCommand
        })
      });
    }
  }]);
  return Dropdown;
}(React.Component);

var TextField = /*#__PURE__*/function (_Component) {
  _inherits(TextField, _Component);
  var _super = _createSuper(TextField);
  function TextField(props) {
    var _this;
    _classCallCheck(this, TextField);
    _this = _super.call(this, props);
    _defineProperty(_assertThisInitialized(_this), "handleChange", function (e) {
      _this.setState({
        text: e.target.value
      });
    });
    _defineProperty(_assertThisInitialized(_this), "handleSubmit", function (e) {
      e.preventDefault();
      if (_this.props.onSubmit) {
        _this.props.onSubmit(_this.state.text);
      }
    });
    _defineProperty(_assertThisInitialized(_this), "handleBlur", function (e) {
      e.preventDefault();
      if (_this.props.onBlur) {
        _this.props.onBlur(_this.state.text);
      }
    });
    _this.state = {
      text: props.defaultValue || ''
    };
    return _this;
  }
  _createClass(TextField, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (this.state.text !== nextProps.defaultValue) {
        this.setState({
          text: nextProps.defaultValue || ''
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var placeholder = this.props.placeholder;
      return /*#__PURE__*/jsxRuntime.jsx("form", {
        onSubmit: this.handleSubmit,
        children: /*#__PURE__*/jsxRuntime.jsx(main.Input, {
          value: this.state.text,
          onChange: this.handleChange,
          placeholder: placeholder,
          onBlur: this.handleBlur
        })
      });
    }
  }]);
  return TextField;
}(React.Component);

var SelectWrapper = styled__default["default"].div.withConfig({
  displayName: "Select__SelectWrapper",
  componentId: "sc-1g866sn-0"
})(["width:", "px;"], function (props) {
  return props.width;
});
var Search = /*#__PURE__*/function (_Component) {
  _inherits(Search, _Component);
  var _super = _createSuper(Search);
  function Search() {
    var _this;
    _classCallCheck(this, Search);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this), "state", {
      isOpen: false
    });
    return _this;
  }
  _createClass(Search, [{
    key: "render",
    value: function render() {
      var _this$props = this.props;
        _this$props.options;
        _this$props.onChange;
        _this$props.defaultValue;
        _this$props.placeholder;
        var _this$props$width = _this$props.width,
        width = _this$props$width === void 0 ? 200 : _this$props$width;
      return /*#__PURE__*/jsxRuntime.jsx(SelectWrapper, {
        width: width
      });
    }
  }]);
  return Search;
}(React.Component);

var akGridSize = theme.gridSize();
var ToolbarContainer = styled__default["default"].div.withConfig({
  displayName: "Toolbar__ToolbarContainer",
  componentId: "sc-i2m8ex-0"
})(["background-color:", ";border-radius:", "px;box-shadow:0 0 1px rgba(9,30,66,0.31),0 4px 8px -2px rgba(9,30,66,0.25);padding:", "px ", "px;display:flex;line-height:1;box-sizing:border-box;", ";& > div{align-items:center;}"], theme.themed({
  light: 'white',
  dark: theme.colors.DN70
}), theme.borderRadius(), akGridSize / 2, akGridSize, function (props) {
  return props.hasCompactLeftPadding ? "padding-left: ".concat(akGridSize / 2, "px") : '';
});
var compareItemWithKeys = function compareItemWithKeys(leftItem, rightItem) {
  var excludedKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  return Object.keys(leftItem).filter(function (key) {
    return excludedKeys.indexOf(key) === -1;
  }).every(function (key) {
    return leftItem[key] instanceof Object ? shallowEqual(leftItem[key], rightItem[key]) : leftItem[key] === rightItem[key];
  });
};
var isSameItem = function isSameItem(leftItem, rightItem) {
  if (leftItem.type !== rightItem.type) {
    return false;
  }
  switch (leftItem.type) {
    case 'button':
      // Need to typecast `rightItem as typeof leftItem` otherwise we will
      // have to put the `type !==` inside each case.
      return compareItemWithKeys(leftItem, rightItem, ['type', 'onClick', 'onMouseEnter', 'onMouseLeave']);
    case 'input':
      return compareItemWithKeys(leftItem, rightItem, ['type', 'onSubmit', 'onBlur']);
    case 'select':
      if (Array.isArray(leftItem.options) && Array.isArray(rightItem.options) && !compareArrays(leftItem.options, rightItem.options, function (left, right) {
        return compareItemWithKeys(left, right);
      })) {
        return false;
      }
      return compareItemWithKeys(leftItem, rightItem, ['type', 'onChange', 'options']);
    case 'dropdown':
      if (Array.isArray(leftItem.options) && Array.isArray(rightItem.options) && !compareArrays(leftItem.options, rightItem.options, function (left, right) {
        return compareItemWithKeys(left, right, ['onClick']);
      })) {
        return false;
      }
      return compareItemWithKeys(leftItem, rightItem, ['type', 'options']);
    case 'custom':
      return false;
    case 'separator':
      return compareItemWithKeys(leftItem, rightItem);
  }
  return true;
};
var areSameItems = function areSameItems(leftArr, rightArr) {
  if (leftArr === undefined && rightArr === undefined) {
    return true;
  }
  if (leftArr === undefined || rightArr === undefined) {
    return false;
  }
  if (leftArr.length !== rightArr.length) {
    return false;
  }
  return leftArr.every(function (item, index) {
    return isSameItem(rightArr[index], item);
  });
};
var Toolbar = /*#__PURE__*/function (_Component) {
  _inherits(Toolbar, _Component);
  var _super = _createSuper(Toolbar);
  function Toolbar() {
    _classCallCheck(this, Toolbar);
    return _super.apply(this, arguments);
  }
  _createClass(Toolbar, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
        items = _this$props.items,
        dispatchAnalyticsEvent = _this$props.dispatchAnalyticsEvent,
        dispatchCommand = _this$props.dispatchCommand,
        popupsMountPoint = _this$props.popupsMountPoint,
        popupsBoundariesElement = _this$props.popupsBoundariesElement,
        popupsScrollableElement = _this$props.popupsScrollableElement,
        className = _this$props.className,
        editorView = _this$props.editorView;
      if (!items || !items.length) {
        return null;
      }

      // Select has left padding of 4px to the border, everything else 8px
      var firstElementIsSelect = items[0].type === 'select';
      return /*#__PURE__*/jsxRuntime.jsx(ToolbarContainer, {
        "aria-label": "Floating Toolbar",
        hasCompactLeftPadding: firstElementIsSelect,
        className: className,
        children: /*#__PURE__*/jsxRuntime.jsx(Button.ButtonGroup, {
          children: items.filter(function (item) {
            return !item.hidden;
          }).map(function (item, idx) {
            switch (item.type) {
              case 'button':
                var ButtonIcon = item.icon;
                return /*#__PURE__*/jsxRuntime.jsx(main.Button, {
                  className: item.className,
                  title: item.title,
                  href: item.href,
                  icon: item.icon ? /*#__PURE__*/jsxRuntime.jsx(ButtonIcon, {
                    label: item.title
                  }) : undefined,
                  appearance: item.appearance,
                  target: item.target,
                  onClick: function onClick() {
                    return dispatchCommand(item.onClick);
                  },
                  onMouseEnter: function onMouseEnter() {
                    return dispatchCommand(item.onMouseEnter);
                  },
                  onMouseLeave: function onMouseLeave() {
                    return dispatchCommand(item.onMouseLeave);
                  },
                  selected: item.selected,
                  disabled: item.disabled,
                  tooltipContent: item.tooltipContent,
                  testId: item.testId,
                  children: item.showTitle && item.title
                }, idx);
              case 'input':
                return /*#__PURE__*/jsxRuntime.jsx(TextField, {
                  mountPoint: popupsMountPoint,
                  boundariesElement: popupsBoundariesElement,
                  defaultValue: item.defaultValue,
                  placeholder: item.placeholder,
                  onSubmit: function onSubmit(value) {
                    return dispatchCommand(item.onSubmit(value));
                  },
                  onBlur: function onBlur(value) {
                    return dispatchCommand(item.onBlur(value));
                  }
                }, idx);
              case 'custom':
                {
                  return item.render(editorView, idx, dispatchAnalyticsEvent);
                }
              case 'dropdown':
                var DropdownIcon = item.icon;
                return /*#__PURE__*/jsxRuntime.jsx(Dropdown, {
                  title: item.title,
                  icon: DropdownIcon && /*#__PURE__*/jsxRuntime.jsx(DropdownIcon, {
                    label: item.title
                  }),
                  dispatchCommand: dispatchCommand,
                  options: item.options,
                  hideExpandIcon: item.hideExpandIcon,
                  mountPoint: popupsMountPoint,
                  boundariesElement: popupsBoundariesElement,
                  scrollableElement: popupsScrollableElement
                }, idx);
              case 'select':
                return /*#__PURE__*/jsxRuntime.jsx(Search, {
                  dispatchCommand: dispatchCommand,
                  options: item.options,
                  hideExpandIcon: item.hideExpandIcon,
                  mountPoint: popupsMountPoint,
                  boundariesElement: popupsBoundariesElement,
                  scrollableElement: popupsScrollableElement,
                  defaultValue: item.defaultValue,
                  placeholder: item.placeholder,
                  onChange: function onChange(selected) {
                    return dispatchCommand(item.onChange(selected));
                  }
                }, idx);
              case 'separator':
                return /*#__PURE__*/jsxRuntime.jsx(main.Separator, {}, idx);
            }
          })
        })
      });
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      return this.props.node.type !== nextProps.node.type || !areSameItems(this.props.items, nextProps.items);
    }
  }]);
  return Toolbar;
}(React.Component);

exports.areSameItems = areSameItems;
exports["default"] = Toolbar;
exports.isSameItem = isSameItem;
