"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 _new = _interopRequireDefault(require("@atlaskit/button/new"));
var _messages = require("../../../../messages");
var _intlUtils = require("../../../../utils/intl-utils");
var _unresolvedView = _interopRequireDefault(require("../unresolved-view"));
var _forbiddenSvg = require("./forbidden-svg");
var _excluded = ["context", "onAuthorize", "accessContext", "testId"];
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 ForbiddenView = function ForbiddenView(_ref) {
  var context = _ref.context,
    onAuthorize = _ref.onAuthorize,
    accessContext = _ref.accessContext,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'embed-card-forbidden-view' : _ref$testId,
    unresolvedViewProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _ref2 = context !== null && context !== void 0 ? context : {},
    icon = _ref2.icon,
    image = _ref2.image,
    _ref2$text = _ref2.text,
    text = _ref2$text === void 0 ? '' : _ref2$text;
  var _ref3 = accessContext !== null && accessContext !== void 0 ? accessContext : {},
    accessType = _ref3.accessType,
    hostname = _ref3.hostname,
    titleMessageKey = _ref3.titleMessageKey,
    descriptiveMessageKey = _ref3.descriptiveMessageKey,
    callToActionMessageKey = _ref3.callToActionMessageKey,
    action = _ref3.action;
  var values = (0, _react.useMemo)(function () {
    var _context$text;
    var product = (_context$text = context === null || context === void 0 ? void 0 : context.text) !== null && _context$text !== void 0 ? _context$text : '';
    return {
      context: product,
      product: product,
      hostname: /*#__PURE__*/_react.default.createElement("b", null, hostname)
    };
  }, [hostname, context === null || context === void 0 ? void 0 : context.text]);

  /**
   * if there is a request access context, but no action to perform, do not show any button.
   * By default, a "Try another account" button shows, but with request access context, we don't
   * want to encourage users to try another account, if their request is already pending, etc.
   */
  var button = (0, _react.useMemo)(function () {
    var _action$promise;
    var onEmbedCardClick = (_action$promise = action === null || action === void 0 ? void 0 : action.promise) !== null && _action$promise !== void 0 ? _action$promise : onAuthorize;
    if (!onEmbedCardClick) {
      return null;
    }
    return /*#__PURE__*/_react.default.createElement(_new.default, {
      testId: "button-".concat((action === null || action === void 0 ? void 0 : action.id) || 'connect-other-account'),
      appearance: "primary",
      onClick: onEmbedCardClick,
      isDisabled: accessType === 'PENDING_REQUEST_EXISTS'
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, (0, _intlUtils.toMessage)(_messages.messages.try_another_account, callToActionMessageKey), {
      values: values
    })));
  }, [accessType, action === null || action === void 0 ? void 0 : action.id, action === null || action === void 0 ? void 0 : action.promise, callToActionMessageKey, onAuthorize, values]);
  return /*#__PURE__*/_react.default.createElement(_unresolvedView.default, (0, _extends2.default)({}, unresolvedViewProps, {
    icon: icon,
    image: image !== null && image !== void 0 ? image : /*#__PURE__*/_react.default.createElement(_forbiddenSvg.ForbiddenSVG, null),
    testId: testId,
    text: text,
    title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, (0, _intlUtils.toMessage)(_messages.messages.invalid_permissions, titleMessageKey), {
      values: values
    })),
    description: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, (0, _intlUtils.toMessage)(_messages.messages.invalid_permissions_description, descriptiveMessageKey), {
      values: values
    })),
    button: button
  }));
};
var _default = exports.default = ForbiddenView;