"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.TOOLBAR_BUTTON = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@emotion/react");
var _analyticsListeners = require("@atlaskit/analytics-listeners");
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
var _analytics = require("../../analytics");
var _styles = _interopRequireDefault(require("./styles"));
var _templateObject; // This file is copied to `packages/editor/editor-plugin-ai/src/ui/components/AtlassianIntelligenceToolbarButton/ToolbarButton/index.tsx`
// If you make any change here, copy it to above file as well
// and notify about the change in #team-fc-editor-ai-dev channel.
/** @jsx jsx */
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var TOOLBAR_BUTTON = exports.TOOLBAR_BUTTON = _analytics.TOOLBAR_ACTION_SUBJECT_ID;
var buttonWrapper = (0, _react2.css)(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["\n  display: flex;\n  height: 100%;\n"])));
var ToolbarButton = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var buttonId = props.buttonId,
    testId = props.testId,
    _props$className = props.className,
    className = _props$className === void 0 ? '' : _props$className,
    href = props.href,
    iconAfter = props.iconAfter,
    iconBefore = props.iconBefore,
    disabled = props.disabled,
    selected = props.selected,
    spacing = props.spacing,
    target = props.target,
    children = props.children,
    hideTooltip = props.hideTooltip,
    title = props.title,
    _props$titlePosition = props.titlePosition,
    titlePosition = _props$titlePosition === void 0 ? 'top' : _props$titlePosition,
    item = props.item,
    ariaLabel = props['aria-label'],
    ariaHasPopup = props['aria-haspopup'],
    ariaExpanded = props['aria-expanded'],
    ariaPressed = props['aria-pressed'],
    ariaKeyShortcuts = props['aria-keyshortcuts'],
    onClick = props.onClick,
    onKeyDown = props.onKeyDown,
    onItemClick = props.onItemClick;
  var handleClick = (0, _react.useCallback)(function (event, analyticsEvent) {
    if (disabled) {
      return;
    }
    if (buttonId) {
      analyticsEvent.update(function (payload) {
        return _objectSpread(_objectSpread({}, payload), {}, {
          action: _analytics.ACTION.CLICKED,
          actionSubject: _analytics.ACTION_SUBJECT.TOOLBAR_BUTTON,
          actionSubjectId: buttonId,
          eventType: _analytics.EVENT_TYPE.UI
        });
      }).fire(_analyticsListeners.FabricChannel.editor);
    }
    if (onClick) {
      onClick(event);
    }
    if (item && onItemClick) {
      onItemClick(item);
    }
  }, [disabled, onClick, onItemClick, item, buttonId]);
  var id = buttonId ? "editor-toolbar__".concat(buttonId) : undefined;
  var button = (0, _react2.jsx)(_styles.default, {
    id: id,
    ref: ref,
    appearance: "subtle",
    testId: testId,
    className: className,
    href: href,
    iconAfter: iconAfter,
    iconBefore: iconBefore,
    isDisabled: disabled,
    isSelected: selected,
    onClick: handleClick,
    spacing: spacing || 'default',
    target: target,
    shouldFitContainer: true,
    "aria-expanded": ariaExpanded,
    "aria-haspopup": ariaHasPopup,
    "aria-label": ariaLabel,
    "aria-pressed": ariaPressed,
    "aria-keyshortcuts": ariaKeyShortcuts,
    onKeyDown: onKeyDown
  }, children);
  if (!title) {
    return button;
  }
  var tooltipContent = !hideTooltip ? title : null;
  return (0, _react2.jsx)(_tooltip.default, {
    content: tooltipContent,
    hideTooltipOnClick: true,
    position: titlePosition
  }, (0, _react2.jsx)("div", {
    css: buttonWrapper
  }, button));
});
var _default = exports.default = ToolbarButton;