import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["context", "onAuthorize", "accessContext", "testId"];
import React, { useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import Button from '@atlaskit/button/new';
import { messages } from '../../../../messages';
import { toMessage } from '../../../../utils/intl-utils';
import UnresolvedView from '../unresolved-view';
import { ForbiddenSVG } from './forbidden-svg';
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 = _objectWithoutProperties(_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 = 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.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 = 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.createElement(Button, {
      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.createElement(FormattedMessage, _extends({}, toMessage(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.createElement(UnresolvedView, _extends({}, unresolvedViewProps, {
    icon: icon,
    image: image !== null && image !== void 0 ? image : /*#__PURE__*/React.createElement(ForbiddenSVG, null),
    testId: testId,
    text: text,
    title: /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, toMessage(messages.invalid_permissions, titleMessageKey), {
      values: values
    })),
    description: /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, toMessage(messages.invalid_permissions_description, descriptiveMessageKey), {
      values: values
    })),
    button: button
  }));
};
export default ForbiddenView;