"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _download = _interopRequireDefault(require("@atlaskit/icon/core/download"));
var _constants = require("../../../../../constants");
var _messages = require("../../../../../messages");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var _useInvokeClientAction = _interopRequireDefault(require("../../../../../state/hooks/use-invoke-client-action"));
var _action = _interopRequireDefault(require("../action"));
var _excluded = ["onClick"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var DownloadAction = function DownloadAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var invoke = (0, _useInvokeClientAction.default)({});
  var data = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[_constants.ActionName.DownloadAction];
  var onClick = (0, _react.useCallback)(function () {
    if (data !== null && data !== void 0 && data.invokeAction) {
      invoke(data.invokeAction);
      onClickCallback === null || onClickCallback === void 0 || onClickCallback();
    }
  }, [data, invoke, onClickCallback]);
  var isStackItem = props.as === 'stack-item';
  var label = isStackItem ? _messages.messages.download_file : _messages.messages.download;
  var tooltipMessage = isStackItem ? _messages.messages.download_description : _messages.messages.download;
  return data ? /*#__PURE__*/_react.default.createElement(_action.default, (0, _extends2.default)({
    content: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, label),
    icon: /*#__PURE__*/_react.default.createElement(_download.default, {
      spacing: "spacious",
      color: "currentColor",
      label: "Download"
    }),
    onClick: onClick,
    testId: "smart-action-download-action",
    tooltipMessage: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, tooltipMessage)
  }, data, props)) : null;
};
var _default = exports.default = DownloadAction;