"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.InlineCard = void 0;
Object.defineProperty(exports, "InlineCardErroredView", {
  enumerable: true,
  get: function get() {
    return _ErroredView.InlineCardErroredView;
  }
});
Object.defineProperty(exports, "InlineCardForbiddenView", {
  enumerable: true,
  get: function get() {
    return _ForbiddenView.InlineCardForbiddenView;
  }
});
Object.defineProperty(exports, "InlineCardResolvedView", {
  enumerable: true,
  get: function get() {
    return _ResolvedView.InlineCardResolvedView;
  }
});
Object.defineProperty(exports, "InlineCardResolvingView", {
  enumerable: true,
  get: function get() {
    return _ResolvingView.InlineCardResolvingView;
  }
});
Object.defineProperty(exports, "InlineCardUnauthorizedView", {
  enumerable: true,
  get: function get() {
    return _UnauthorisedView.InlineCardUnauthorizedView;
  }
});
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _analyticsNext = require("@atlaskit/analytics-next");
var _linkExtractors = require("@atlaskit/link-extractors");
var _loadHold = _interopRequireDefault(require("@atlaskit/react-ufo/load-hold"));
var _constants = require("../../constants");
var _extractAccessContext = require("../../extractors/common/context/extractAccessContext");
var _inline = require("../../extractors/inline");
var _helpers = require("../../state/helpers");
var _jsonld = require("../../utils/jsonld");
var _LinkView = require("../LinkView");
var _ErroredView = require("./ErroredView");
var _ForbiddenView = require("./ForbiddenView");
var _ResolvedView = require("./ResolvedView");
var _ResolvingView = require("./ResolvingView");
var _UnauthorisedView = require("./UnauthorisedView");
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 UFOLoadHoldWrapper = function UFOLoadHoldWrapper(_ref) {
  var children = _ref.children;
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_loadHold.default, {
    name: "smart-card-inline-card"
  }), children);
};
var InlineCard = exports.InlineCard = function InlineCard(_ref2) {
  var _details$meta2, _details$meta3, _providerForbidden$te;
  var id = _ref2.id,
    url = _ref2.url,
    cardState = _ref2.cardState,
    handleAuthorize = _ref2.handleAuthorize,
    handleFrameClick = _ref2.handleFrameClick,
    handleFrameAuxClick = _ref2.handleFrameAuxClick,
    handleFrameContextMenu = _ref2.handleFrameContextMenu,
    isSelected = _ref2.isSelected,
    isHovered = _ref2.isHovered,
    renderers = _ref2.renderers,
    onResolve = _ref2.onResolve,
    onError = _ref2.onError,
    testId = _ref2.testId,
    inlinePreloaderStyle = _ref2.inlinePreloaderStyle,
    showHoverPreview = _ref2.showHoverPreview,
    hoverPreviewOptions = _ref2.hoverPreviewOptions,
    actionOptions = _ref2.actionOptions,
    removeTextHighlightingFromTitle = _ref2.removeTextHighlightingFromTitle,
    resolvingPlaceholder = _ref2.resolvingPlaceholder,
    truncateInline = _ref2.truncateInline,
    hideIconLoadingSkeleton = _ref2.hideIconLoadingSkeleton;
  var _useAnalyticsEvents = (0, _analyticsNext.useAnalyticsEvents)(),
    createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent;
  var status = cardState.status,
    details = cardState.details;
  var extensionKey = (0, _helpers.getExtensionKey)(details);
  var testIdWithStatus = testId ? "".concat(testId, "-").concat(status, "-view") : undefined;
  var resolvedProps = status === _constants.SmartLinkStatus.Resolved ? (0, _inline.extractInlineProps)(details, renderers, removeTextHighlightingFromTitle, false) : {};
  (0, _react.useEffect)(function () {
    var _details$meta;
    switch (status) {
      case _constants.SmartLinkStatus.Resolved:
        onResolve === null || onResolve === void 0 || onResolve({
          url: url,
          title: resolvedProps.title,
          extensionKey: details === null || details === void 0 || (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.key
        });
        break;
      case _constants.SmartLinkStatus.Errored:
      case _constants.SmartLinkStatus.Fallback:
      case _constants.SmartLinkStatus.Forbidden:
      case _constants.SmartLinkStatus.NotFound:
      case _constants.SmartLinkStatus.Unauthorized:
        if (onError) {
          onError({
            status: status,
            url: url
          });
        }
        break;
    }
  }, [onError, onResolve, status, url, resolvedProps.title, details === null || details === void 0 || (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.key]);
  switch (status) {
    case 'pending':
    case 'resolving':
      return /*#__PURE__*/_react.default.createElement(UFOLoadHoldWrapper, null, /*#__PURE__*/_react.default.createElement(_ResolvingView.InlineCardResolvingView, {
        url: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        testId: testIdWithStatus,
        inlinePreloaderStyle: inlinePreloaderStyle,
        resolvingPlaceholder: resolvingPlaceholder,
        truncateInline: truncateInline
      }));
    case 'resolved':
      return /*#__PURE__*/_react.default.createElement(_ResolvedView.InlineCardResolvedView, (0, _extends2.default)({}, resolvedProps, {
        id: id,
        showHoverPreview: showHoverPreview,
        hoverPreviewOptions: hoverPreviewOptions,
        actionOptions: actionOptions,
        link: url,
        isSelected: isSelected,
        isHovered: isHovered,
        onClick: handleFrameClick
        // Support middle-/right-clicks for "resolved" only — the vast majority of clicks happen here.
        // Other states (e.g. "resolving") could be added later for completeness.
        ,
        onAuxClick: handleFrameAuxClick,
        onContextMenu: handleFrameContextMenu,
        testId: testIdWithStatus,
        truncateInline: truncateInline,
        hideIconLoadingSkeleton: hideIconLoadingSkeleton
      }));
    case 'unauthorized':
      var provider = (0, _linkExtractors.extractSmartLinkProvider)(details);
      return /*#__PURE__*/_react.default.createElement(_UnauthorisedView.InlineCardUnauthorizedView, {
        icon: provider && provider.icon,
        context: provider && provider.text,
        url: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        onAuthorise: handleAuthorize,
        testId: testIdWithStatus,
        showHoverPreview: showHoverPreview,
        id: id,
        extensionKey: extensionKey,
        truncateInline: truncateInline
      });
    case 'forbidden':
      var providerForbidden = (0, _linkExtractors.extractSmartLinkProvider)(details);
      var cardMetadata = (_details$meta3 = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta3 !== void 0 ? _details$meta3 : (0, _jsonld.getForbiddenJsonLd)().meta;
      var requestAccessContext = (0, _extractAccessContext.extractRequestAccessContextImproved)({
        jsonLd: cardMetadata,
        url: url,
        product: (_providerForbidden$te = providerForbidden === null || providerForbidden === void 0 ? void 0 : providerForbidden.text) !== null && _providerForbidden$te !== void 0 ? _providerForbidden$te : '',
        createAnalyticsEvent: createAnalyticsEvent
      });
      return /*#__PURE__*/_react.default.createElement(_ForbiddenView.InlineCardForbiddenView, {
        url: url,
        icon: providerForbidden && providerForbidden.icon,
        context: providerForbidden === null || providerForbidden === void 0 ? void 0 : providerForbidden.text,
        isSelected: isSelected,
        onClick: handleFrameClick,
        onAuthorise: handleAuthorize,
        testId: testIdWithStatus,
        requestAccessContext: requestAccessContext,
        showHoverPreview: showHoverPreview,
        truncateInline: truncateInline
      });
    case 'not_found':
      var providerNotFound = (0, _linkExtractors.extractSmartLinkProvider)(details);
      return /*#__PURE__*/_react.default.createElement(_ErroredView.InlineCardErroredView, {
        url: url,
        icon: providerNotFound && providerNotFound.icon,
        isSelected: isSelected,
        message: "Can't find link",
        onClick: handleFrameClick,
        testId: testIdWithStatus || 'inline-card-not-found-view',
        showHoverPreview: showHoverPreview,
        truncateInline: truncateInline
      });
    case 'fallback':
    case 'errored':
    default:
      return /*#__PURE__*/_react.default.createElement(_LinkView.CardLinkView, {
        link: url,
        isSelected: isSelected,
        onClick: handleFrameClick,
        testId: testIdWithStatus || 'inline-card-errored-view',
        truncateInline: truncateInline,
        viewType: "errored"
      });
  }
};