"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _withEditorProps = require("../withEditorProps");
var _core = require("@blueprintjs/core");
var _react = _interopRequireDefault(require("react"));
require("./style.css");
var _excluded = ["isOpen", "index", "Icon", "onIconClick", "tooltip", "tooltipToggled", "dropdowntooltip", "Dropdown", "disabled", "isHidden", "renderIconAbove", "noDropdownIcon", "IconWrapper", "editorName", "popoverDisabled", "IconWrapperProps", "toolName", "dropdownicon", "tooltipDisabled", "toggled"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var ToolbarItem = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(ToolbarItem, _React$Component);
  function ToolbarItem() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _defineProperty(_assertThisInitialized(_this), "toggleDropdown", function (_temp) {
      var _ref = _temp === void 0 ? {} : _temp,
        forceClose = _ref.forceClose;
      var _this$props = _this.props,
        toolName = _this$props.toolName,
        isOpen = _this$props.isOpen;
      _this.props.openToolbarItemUpdate(isOpen || forceClose ? "" : toolName);
    });
    return _this;
  }
  var _proto = ToolbarItem.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var _this$props$overrides = this.props.overrides,
      overrides = _this$props$overrides === void 0 ? {} : _this$props$overrides;
    var _this$props$overrides2 = _extends({}, this.props, overrides),
      isOpen = _this$props$overrides2.isOpen,
      index = _this$props$overrides2.index,
      Icon = _this$props$overrides2.Icon,
      _this$props$overrides3 = _this$props$overrides2.onIconClick,
      onIconClick = _this$props$overrides3 === void 0 ? noop : _this$props$overrides3,
      _this$props$overrides4 = _this$props$overrides2.tooltip,
      tooltip = _this$props$overrides4 === void 0 ? "" : _this$props$overrides4,
      tooltipToggled = _this$props$overrides2.tooltipToggled,
      _this$props$overrides5 = _this$props$overrides2.dropdowntooltip,
      dropdowntooltip = _this$props$overrides5 === void 0 ? "" : _this$props$overrides5,
      Dropdown = _this$props$overrides2.Dropdown,
      disabled = _this$props$overrides2.disabled,
      isHidden = _this$props$overrides2.isHidden,
      renderIconAbove = _this$props$overrides2.renderIconAbove,
      noDropdownIcon = _this$props$overrides2.noDropdownIcon,
      IconWrapper = _this$props$overrides2.IconWrapper,
      editorName = _this$props$overrides2.editorName,
      popoverDisabled = _this$props$overrides2.popoverDisabled,
      IconWrapperProps = _this$props$overrides2.IconWrapperProps,
      toolName = _this$props$overrides2.toolName,
      dropdownicon = _this$props$overrides2.dropdownicon,
      tooltipDisabled = _this$props$overrides2.tooltipDisabled,
      _this$props$overrides6 = _this$props$overrides2.toggled,
      toggled = _this$props$overrides6 === void 0 ? false : _this$props$overrides6,
      rest = _objectWithoutPropertiesLoose(_this$props$overrides2, _excluded);
    if (!toolName) console.warn("toolName is required!");
    if (isHidden) return null;
    var tooltipToDisplay = tooltip;
    if (toggled && tooltipToggled) {
      tooltipToDisplay = tooltipToggled;
    }
    // const Dropdown = _DropDown && withEditorProps && withEditorProps(_DropDown);

    var buttonTarget = /*#__PURE__*/_react["default"].createElement("div", {
      className: "veToolbarItemOuter ve-tool-container-" + toolName + (disabled ? " disabled " : "")
    }, renderIconAbove && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
      className: "veToolbarItem"
    }, Icon)), Icon && !renderIconAbove && /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
      disabled: tooltipDisabled,
      portalClassName: "ve-toolbar-item-popover",
      content: tooltipToDisplay
    }, /*#__PURE__*/_react["default"].createElement(_core.AnchorButton, {
      intent: _core.Intent.PRIMARY,
      onClick: onIconClick === "toggleDropdown" ? this.toggleDropdown : onIconClick,
      active: toggled,
      disabled: disabled,
      minimal: true,
      icon: _react["default"].isValidElement(Icon) ? Icon : /*#__PURE__*/_react["default"].createElement(Icon, {
        toggleDropdown: this.toggleDropdown
      })
    })), Dropdown && !noDropdownIcon ? /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
      disabled: tooltipDisabled,
      content: dropdowntooltip
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: (isOpen ? " isOpen " : "") + (dropdownicon ? "" : " veToolbarDropdown"),
      onClick: this.toggleDropdown
    }, dropdownicon ? /*#__PURE__*/_react["default"].createElement("div", {
      className: "veToolbarIcon"
    }, /*#__PURE__*/_react["default"].createElement("div", null, dropdownicon)) : isOpen ? /*#__PURE__*/_react["default"].createElement(_core.Icon, {
      "data-test": toolName + "Dropdown",
      iconSize: 13,
      icon: "caret-up"
    }) : /*#__PURE__*/_react["default"].createElement(_core.Icon, {
      "data-test": toolName + "Dropdown",
      iconSize: 13,
      icon: "caret-down"
    }))) : null);
    var content = /*#__PURE__*/_react["default"].createElement("div", {
      ref: function ref(n) {
        if (n) _this2.dropdownNode = n;
      },
      style: {
        padding: 10,
        minWidth: 250,
        maxWidth: 350
      },
      className: "ve-toolbar-dropdown content"
    }, Dropdown && /*#__PURE__*/_react["default"].createElement(Dropdown, _extends({}, rest, {
      editorName: editorName,
      toggleDropdown: this.toggleDropdown
    })));
    var target = IconWrapper ? /*#__PURE__*/_react["default"].createElement(IconWrapper, IconWrapperProps, function (_ref2) {
      var getRootProps = _ref2.getRootProps,
        getInputProps = _ref2.getInputProps;
      return /*#__PURE__*/_react["default"].createElement("div", getRootProps(), /*#__PURE__*/_react["default"].createElement("input", getInputProps()), buttonTarget);
    }) : buttonTarget;
    return /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: "flex",
        alignItems: "center"
      }
    }, index !== 0 && /*#__PURE__*/_react["default"].createElement("div", {
      className: "veToolbarSpacer"
    }), /*#__PURE__*/_react["default"].createElement(_core.Popover, {
      disabled: popoverDisabled,
      isOpen: !!Dropdown && isOpen,
      onClose: function onClose(e) {
        var srcElement;
        if (e) {
          srcElement = e.srcElement || e.target;
        }
        if (e && srcElement && _this2.dropdownNode && (_this2.dropdownNode.contains(srcElement) || !document.body.contains(srcElement))) {
          return;
        }
        _this2.toggleDropdown({
          forceClose: true
        });
      },
      canEscapeKeyClose: true,
      minimal: true,
      position: _core.Position.BOTTOM,
      target: target,
      content: content
    }));
  };
  return ToolbarItem;
}(_react["default"].Component);
function noop() {}
var _default = (0, _withEditorProps.connectToEditor)(function (_ref3, _ref4) {
  var _ref3$toolBar = _ref3.toolBar,
    toolBar = _ref3$toolBar === void 0 ? {} : _ref3$toolBar;
  var toolName = _ref4.toolName;
  return {
    isOpen: toolBar.openItem === toolName
  };
})(ToolbarItem);
exports["default"] = _default;
module.exports = exports.default;