/* UnauthorisedView.tsx generated by @compiled/babel-plugin v0.38.1 */
"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;
require("./UnauthorisedView.compiled.css");
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _runtime = require("@compiled/react/runtime");
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _reactIntlNext = require("react-intl-next");
var _linkExtractors = require("@atlaskit/link-extractors");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _compiled = require("@atlaskit/primitives/compiled");
var _useAnalyticsEvents2 = require("../../../common/analytics/generated/use-analytics-events");
var _constants = require("../../../constants");
var _messages = require("../../../messages");
var _flexibleUiContext = require("../../../state/flexible-ui-context");
var _helpers = require("../../../state/helpers");
var _experiments = require("../../../utils/experiments");
var _UnauthorisedViewContent = _interopRequireDefault(require("../../common/UnauthorisedViewContent"));
var _FlexibleCard = _interopRequireDefault(require("../../FlexibleCard"));
var _actionGroup = _interopRequireDefault(require("../../FlexibleCard/components/blocks/action-group"));
var _block = _interopRequireDefault(require("../../FlexibleCard/components/blocks/block"));
var _elementGroup = _interopRequireDefault(require("../../FlexibleCard/components/blocks/element-group"));
var _utils = require("../../FlexibleCard/components/blocks/utils");
var _elements = require("../../FlexibleCard/components/elements");
var _AuthorizeAction = require("../actions/AuthorizeAction");
var _figma2x = _interopRequireDefault(require("./assets/figma@2x.png"));
var _general2x = _interopRequireDefault(require("./assets/general@2x.png"));
var _googleDrive2x = _interopRequireDefault(require("./assets/google-drive@2x.png"));
var _onedrive2x = _interopRequireDefault(require("./assets/onedrive@2x.png"));
var _slack2x = _interopRequireDefault(require("./assets/slack@2x.png"));
var _unresolvedView = _interopRequireDefault(require("./unresolved-view"));
var _utils2 = require("./utils");
var _withFlexibleUIBlockCardStyle = require("./utils/withFlexibleUIBlockCardStyle");
var _excluded = ["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 contentStyles = null;
var newContentStyles = null;
var contentAdditionalStyles = null;
var newTitleBlockStyles = null;
var intermediumGapStyles = null;
var titleBlockStyles = null;
var previewBlockStyle = null;
var previewBlockStyleWithOverlap = null;
var linkIconStyles = null;
var containerStyles = null;
var previewBlockImageStyle = null;
var styles = {
  titleBoxCard: "_1e0c1txw _2lx2vrvc _4cvr1h6o _1bsb1osq"
};
var getBetterTitle = function getBetterTitle(url) {
  // List of regexps to extract human-readable titles from URLs
  // Each regex should have a capture group for the title at index 1
  var titleExtractors = [
  // Dropbox: https://www.dropbox.com/scl/fi/scebraopvx4bni0sechq9/Atlas23_D1_A_0014.jpg?rlkey=... -> Atlas23_D1_A_0014.jpg
  /^https:\/\/www\.dropbox\.com\/scl\/fi\/[^\/]+\/([^\/\?]+)/,
  // Dropbox: https://www.dropbox.com/home/Teamwork%20Ecosystem?preview=xsociety00.dat -> Teamwork Ecosystem
  /^https:\/\/www\.dropbox\.com\/home\/([^\/\?]+)/,
  // Dropbox: https://www.dropbox.com/home?preview=Insomnia-All_2019-03-14.json -> Insomnia-All_2019-03-14.json
  /^https:\/\/www\.dropbox\.com\/home\?(?:.*?&)?preview=([^&#]+)/,
  // Dropbox: https://www.dropbox.com/preview/AI%20Contest -> AI Contest
  /^https:\/\/www\.dropbox\.com\/preview\/([^&?]+)/,
  // Dropbox: https://www.dropbox.com/s/votiehk00g4opif/xsociety00.dat?dl=0 -> xsociety00.dat
  /^https:\/\/www\.dropbox\.com\/(?:s|sh|sc|t)\/[^\/]+\/([^\/\?]+)/,
  // Figma: https://www.figma.com/slides/vW47To0dYPnT7jlrJdUd7h/Universal-Create-in-TwC?node-id=... -> Universal-Create-in-TwC
  // Also supports: /file/, /design/, /board/, /proto/
  /^https:\/\/(?:[\w.-]+\.)?figma\.com\/(?:design|board|file|proto|slides)\/[0-9a-zA-Z]{22,128}\/([^\/\?]+)/,
  // OneDrive: https://1drv.ms/w/s!ABC123DEF456/Project_Plan.docx?e=abcdEF -> Project_Plan.docx
  /^https:\/\/1drv\.ms\/[a-z]+\/s![^\/]+\/([^\/\?]+)/,
  // Miro: https://miro.com/app/board/uXjVOPnT123=/my-board-name/ -> my-board-name
  /^https:\/\/miro\.com\/app\/board\/[^\/]+\/([^\/\?]+)\/?/];

  // Try each extractor pattern
  for (var _i = 0, _titleExtractors = titleExtractors; _i < _titleExtractors.length; _i++) {
    var pattern = _titleExtractors[_i];
    var match = url.match(pattern);
    if (match) {
      var extracted = decodeURIComponent(match[1]);
      if (extracted) {
        return extracted;
      }
    }
  }
  try {
    var _URL = new URL(url),
      pathname = _URL.pathname,
      search = _URL.search;
    if (pathname.length > 1) {
      return pathname.substring(1);
    } else {
      return search;
    }
  } catch (_unused) {
    return url;
  }
};
var NewUnauthorisedBlock = function NewUnauthorisedBlock(_ref) {
  var _useFlexibleCardConte, _getExtensionKey, _data$preview;
  var actions = _ref.actions,
    children = _ref.children,
    url = _ref.url,
    CompetitorPrompt = _ref.CompetitorPrompt,
    testId = _ref.testId,
    cardState = _ref.cardState;
  var _ref2 = (_useFlexibleCardConte = (0, _flexibleUiContext.useFlexibleCardContext)()) !== null && _useFlexibleCardConte !== void 0 ? _useFlexibleCardConte : {
      ui: undefined,
      data: undefined
    },
    ui = _ref2.ui,
    data = _ref2.data;
  var hasActions = actions && actions.length > 0;
  var actionGroup = hasActions ? /*#__PURE__*/React.createElement(_actionGroup.default, {
    size: _constants.SmartLinkSize.Large,
    items: actions
  }) : null;
  var competitorPrompt = CompetitorPrompt && data !== null && data !== void 0 && data.url ? /*#__PURE__*/React.createElement(CompetitorPrompt, {
    sourceUrl: data === null || data === void 0 ? void 0 : data.url,
    linkType: 'card'
  }) : null;
  var title = /*#__PURE__*/React.createElement(_elements.Title, {
    hideTooltip: false,
    maxLines: 1,
    theme: ui === null || ui === void 0 ? void 0 : ui.theme,
    text: getBetterTitle(url),
    className: (0, _runtime.ax)(["_k48p1pd9 _syaz13af"])
  });
  var position = _utils2.titleBlockOptions.position;
  var extensionKey = (_getExtensionKey = (0, _helpers.getExtensionKey)(cardState === null || cardState === void 0 ? void 0 : cardState.details)) !== null && _getExtensionKey !== void 0 ? _getExtensionKey : '';
  var overrideUrl = data === null || data === void 0 || (_data$preview = data.preview) === null || _data$preview === void 0 ? void 0 : _data$preview.url;
  var isHardcodedImage = !overrideUrl;
  if (isHardcodedImage) {
    if ((0, _platformFeatureFlags.fg)('navx-3264-refactoring-unauth-provider-images-fe')) {
      overrideUrl = _general2x.default;
    } else {
      switch (extensionKey) {
        case 'figma-object-provider':
          overrideUrl = _figma2x.default;
          break;
        case 'google-object-provider':
          overrideUrl = _googleDrive2x.default;
          break;
        case 'onedrive-object-provider':
          overrideUrl = _onedrive2x.default;
          break;
        case 'slack-object-provider':
          overrideUrl = _slack2x.default;
          break;
        default:
          overrideUrl = _general2x.default;
          break;
      }
    }
  }
  return /*#__PURE__*/React.createElement(_block.default, (0, _extends2.default)({}, _utils2.titleBlockOptions, {
    testId: "".concat(testId, "-errored-view"),
    className: (0, _runtime.ax)(["_7cca15wb _1f3816vg"])
  }), /*#__PURE__*/React.createElement(_elementGroup.default, {
    direction: _constants.SmartLinkDirection.Vertical,
    width: _constants.SmartLinkWidth.Flexible,
    size: _constants.SmartLinkSize.Medium,
    className: (0, _runtime.ax)(["_zulputpp"])
  }, /*#__PURE__*/React.createElement(_block.default, null, /*#__PURE__*/React.createElement(_elements.LinkIcon, {
    position: position,
    size: _constants.SmartLinkSize.Medium,
    isTiledIcon: true,
    className: (0, _runtime.ax)(["_vsqgglyw"])
  }), /*#__PURE__*/React.createElement(_elementGroup.default, {
    direction: _constants.SmartLinkDirection.Vertical,
    width: _constants.SmartLinkWidth.Flexible,
    size: _constants.SmartLinkSize.Large,
    className: (0, _runtime.ax)(["_zulpidpf"])
  }, competitorPrompt ? /*#__PURE__*/React.createElement(_compiled.Box, {
    xcss: styles.titleBoxCard
  }, title, competitorPrompt) : title, /*#__PURE__*/React.createElement(_elementGroup.default, {
    size: _constants.SmartLinkSize.Medium,
    direction: _constants.SmartLinkDirection.Horizontal
  }, (0, _utils.renderElementItems)([{
    name: _constants.ElementName.HostName
  }])))), children, actionGroup), hasActions ? /*#__PURE__*/React.createElement("div", {
    className: (0, _runtime.ax)(["_1reo15vq _18m915vq _1bsb1o87 _1wpz1fhb _1dptglyw", (isHardcodedImage || (0, _platformFeatureFlags.fg)('navx-3264-refactoring-unauth-provider-images-fe')) && "_1bsbo60d"])
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      backgroundImage: "url(".concat(overrideUrl, ")")
    },
    className: (0, _runtime.ax)(["_kqswstnw _154iidpf _1xi2idpf _94n5idpf _4t3i1osq _1bsb1o87 _1lrw1f51 _ouxl1ssb _12vemgnk"])
  })) : null);
};

/**
 * This view represents a Block card that has an 'Unauthorized' status .
 * It should have a "Connect" button that will allow a user to connect their account and view the block card.
 *
 * @see SmartLinkStatus
 * @see FlexibleCardProps
 */
var UnauthorisedView = function UnauthorisedView(_ref3) {
  var _extractSmartLinkProv;
  var _ref3$testId = _ref3.testId,
    testId = _ref3$testId === void 0 ? 'smart-block-unauthorized-view' : _ref3$testId,
    props = (0, _objectWithoutProperties2.default)(_ref3, _excluded);
  var cardState = props.cardState,
    onAuthorize = props.onAuthorize;
  var providerName = (_extractSmartLinkProv = (0, _linkExtractors.extractSmartLinkProvider)(cardState.details)) === null || _extractSmartLinkProv === void 0 ? void 0 : _extractSmartLinkProv.text;
  var isProductIntegrationSupported = (0, _helpers.hasAuthScopeOverrides)(cardState.details);
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var handleAuthorize = (0, _react.useCallback)(function () {
    if (onAuthorize) {
      fireEvent('track.applicationAccount.authStarted', {});
      onAuthorize();
    }
  }, [onAuthorize, fireEvent]);
  var content = (0, _react.useMemo)(function () {
    return onAuthorize ? /*#__PURE__*/React.createElement(_UnauthorisedViewContent.default, {
      providerName: providerName,
      isProductIntegrationSupported: isProductIntegrationSupported,
      testId: testId
    }) : /*#__PURE__*/React.createElement(_reactIntlNext.FormattedMessage, (0, _extends2.default)({}, _messages.messages[providerName ? 'unauthorised_account_description' : 'unauthorised_account_description_no_provider'], {
      values: {
        context: providerName
      }
    }));
  }, [isProductIntegrationSupported, onAuthorize, providerName, testId]);
  var actions = (0, _react.useMemo)(function () {
    return onAuthorize ? [(0, _AuthorizeAction.AuthorizeAction)(handleAuthorize, providerName)] : [];
  }, [handleAuthorize, onAuthorize, providerName]);
  if ((0, _experiments.isNewBlockcardUnauthorizedRefreshExperimentEnabled)(true)) {
    return /*#__PURE__*/React.createElement(_FlexibleCard.default, (0, _extends2.default)({
      appearance: "block",
      onAuthorize: onAuthorize,
      origin: "smartLinkCard",
      testId: testId,
      ui: _utils2.FlexibleCardUiOptions
    }, props), /*#__PURE__*/React.createElement(NewUnauthorisedBlock, (0, _extends2.default)({}, props, {
      actions: actions,
      testId: testId
    }), /*#__PURE__*/React.createElement("div", {
      "data-testid": "".concat(testId, "-content"),
      className: (0, _runtime.ax)(["_11c8fhey _syazi7uo _19pku2gc", actions.length > 0 && "_1reo15vq _18m915vq _otyrpxbi _p12f1osq _o5721q9c _1bto1l2s"])
    }, content)));
  } else {
    return /*#__PURE__*/React.createElement(_unresolvedView.default, (0, _extends2.default)({}, props, {
      actions: actions,
      testId: testId
    }), /*#__PURE__*/React.createElement("div", {
      "data-testid": "".concat(testId, "-content"),
      className: (0, _runtime.ax)(["_11c8wadc _syazi7uo _19pku2gc"])
    }, content));
  }
};
var _default = exports.default = (0, _withFlexibleUIBlockCardStyle.withFlexibleUIBlockCardStyle)(UnauthorisedView);