/* index.tsx generated by @compiled/babel-plugin v0.40.0 */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.InlineCardForbiddenView = void 0;
require("./index.compiled.css");
var _runtime = require("@compiled/react/runtime");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireDefault(require("react"));
var _reactIntl = require("react-intl");
var _lockLocked = _interopRequireDefault(require("@atlaskit/icon/core/lock-locked"));
var _compiled = require("@atlaskit/primitives/compiled");
var _messages = require("../../../messages");
var _HoverCard = require("../../HoverCard");
var _actionButton = require("../common/action-button");
var _inlineLozenge = _interopRequireDefault(require("../common/inline-lozenge"));
var _Frame = require("../Frame");
var _IconAndTitleLayout = require("../IconAndTitleLayout");
var styles = {
  actionButtonLozengeStyle: "_2rkolb4i _bfhksm61 _ca0qze3t _u5f3ze3t _n3tdze3t _19bvze3t _p12f1osq"
};
var fallbackForbiddenIcon = function fallbackForbiddenIcon() {
  return /*#__PURE__*/_react.default.createElement(_lockLocked.default, {
    label: "error",
    color: "var(--ds-icon-danger, #C9372C)",
    testId: "forbidden-view-fallback-icon"
  });
};
var InlineCardForbiddenView = exports.InlineCardForbiddenView = function InlineCardForbiddenView(_ref) {
  var url = _ref.url,
    icon = _ref.icon,
    onClick = _ref.onClick,
    isSelected = _ref.isSelected,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'inline-card-forbidden-view' : _ref$testId,
    truncateInline = _ref.truncateInline,
    requestAccessContext = _ref.requestAccessContext,
    onAuthorise = _ref.onAuthorise,
    context = _ref.context,
    showHoverPreview = _ref.showHoverPreview;
  var frameRef = _react.default.useRef(null);
  var _React$useState = _react.default.useState(!!(requestAccessContext !== null && requestAccessContext !== void 0 && requestAccessContext.callToActionMessageKey)),
    _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 1),
    hasRequestAccessContextMessage = _React$useState2[0];
  var handleRetry = _react.default.useCallback(function (event) {
    event.preventDefault();
    event.stopPropagation();
    if (onAuthorise) {
      onAuthorise();
    } else {
      var _requestAccessContext;
      requestAccessContext === null || requestAccessContext === void 0 || (_requestAccessContext = requestAccessContext.action) === null || _requestAccessContext === void 0 || _requestAccessContext.promise();
    }
  }, [onAuthorise, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.action]);
  var renderForbiddenAccessMessage = _react.default.useCallback(function () {
    if (requestAccessContext !== null && requestAccessContext !== void 0 && requestAccessContext.callToActionMessageKey) {
      var callToActionMessageKey = requestAccessContext.callToActionMessageKey;
      return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, _messages.messages[callToActionMessageKey], {
        values: {
          product: context
        }
      }));
    }
    return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.invalid_permissions, function (formattedMessage) {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, formattedMessage);
    });
  }, [context, requestAccessContext]);
  var renderActionButton = _react.default.useCallback(function () {
    var accessType = requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.accessType;
    if (hasRequestAccessContextMessage) {
      var isDisabled = accessType === 'PENDING_REQUEST_EXISTS';
      return /*#__PURE__*/_react.default.createElement(_actionButton.ActionButton, {
        isDisabled: isDisabled,
        onClick: handleRetry,
        testId: "button-connect-other-account"
      }, renderForbiddenAccessMessage());
    }
    if (onAuthorise) {
      return /*#__PURE__*/_react.default.createElement(_compiled.Pressable, {
        xcss: styles.actionButtonLozengeStyle,
        onClick: handleRetry,
        style: {
          font: "inherit"
        },
        testId: "button-connect-other-account"
      }, /*#__PURE__*/_react.default.createElement(_inlineLozenge.default, {
        appearance: "moved",
        isBold: true
      }, renderForbiddenAccessMessage()));
    }
    return null;
  }, [handleRetry, hasRequestAccessContextMessage, onAuthorise, renderForbiddenAccessMessage, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.accessType]);
  var content = /*#__PURE__*/_react.default.createElement(_Frame.Frame, {
    testId: testId,
    isSelected: isSelected,
    ref: frameRef,
    truncateInline: truncateInline
  }, /*#__PURE__*/_react.default.createElement(_IconAndTitleLayout.IconAndTitleLayout, {
    icon: icon ? icon : fallbackForbiddenIcon(),
    link: url,
    title: url,
    onClick: onClick,
    titleColor: "var(--ds-text-subtle, #505258)"
  }), renderActionButton());
  if (showHoverPreview) {
    return /*#__PURE__*/_react.default.createElement(_HoverCard.HoverCard, {
      url: url
    }, content);
  }
  return content;
};