/* UnauthorisedView.tsx generated by @compiled/babel-plugin v0.39.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 _reactIntl = require("react-intl");
var _reactMagneticDi = require("react-magnetic-di");
var _analyticsNext = require("@atlaskit/analytics-next");
var _linkExtractors = require("@atlaskit/link-extractors");
var _linkProvider = require("@atlaskit/link-provider");
var _platformFeatureFlagsReact = require("@atlaskit/platform-feature-flags-react");
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 _useSocialProofExperiment = _interopRequireWildcard(require("../../../state/hooks/use-social-proof-experiment"));
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 _general2x = _interopRequireDefault(require("./assets/general@2x.png"));
var _SocialProofMessage = _interopRequireDefault(require("./SocialProofMessage"));
var _utils2 = require("./utils");
var _withFlexibleUIBlockCardStyle = require("./utils/withFlexibleUIBlockCardStyle");
var _excluded = ["content", "providerName", "testId"],
  _excluded2 = ["testId"],
  _excluded3 = ["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 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);
    }
    if (search) {
      return search;
    }
    // Fall back to the full URL when there is no useful path/search to display,
    // so the title (and the underlying link) always has accessible text.
    return url;
  } catch (_unused) {
    return url;
  }
};
var UnauthorisedBlock = function UnauthorisedBlock(_ref) {
  var _useFlexibleCardConte, _data$preview$url, _data$preview;
  var actions = _ref.actions,
    children = _ref.children,
    url = _ref.url,
    CompetitorPrompt = _ref.CompetitorPrompt,
    testId = _ref.testId;
  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 overrideUrl = (_data$preview$url = data === null || data === void 0 || (_data$preview = data.preview) === null || _data$preview === void 0 ? void 0 : _data$preview.url) !== null && _data$preview$url !== void 0 ? _data$preview$url : _general2x.default;
  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", "_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 UnauthorisedViewFrame = function UnauthorisedViewFrame(_ref3) {
  var content = _ref3.content,
    providerName = _ref3.providerName,
    testId = _ref3.testId,
    props = (0, _objectWithoutProperties2.default)(_ref3, _excluded);
  var onAuthorize = props.onAuthorize;
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var handleAuthorize = (0, _react.useCallback)(function () {
    if (onAuthorize) {
      fireEvent('track.applicationAccount.authStarted', {});
      onAuthorize();
    }
  }, [onAuthorize, fireEvent]);
  var actions = (0, _react.useMemo)(function () {
    return onAuthorize ? [(0, _AuthorizeAction.AuthorizeAction)(handleAuthorize, providerName)] : [];
  }, [handleAuthorize, onAuthorize, providerName]);
  return /*#__PURE__*/React.createElement(_FlexibleCard.default, (0, _extends2.default)({
    appearance: "block",
    onAuthorize: onAuthorize,
    origin: "smartLinkCard",
    testId: testId,
    ui: _utils2.FlexibleCardUiOptions
  }, props), /*#__PURE__*/React.createElement(UnauthorisedBlock, {
    actions: actions,
    cardState: props.cardState,
    CompetitorPrompt: props.CompetitorPrompt,
    testId: testId,
    url: props.url
  }, /*#__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)));
};
var UnauthorisedViewBase = function UnauthorisedViewBase(_ref4) {
  var _extractSmartLinkProv;
  var _ref4$testId = _ref4.testId,
    testId = _ref4$testId === void 0 ? 'smart-block-unauthorized-view' : _ref4$testId,
    props = (0, _objectWithoutProperties2.default)(_ref4, _excluded2);
  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 content = (0, _react.useMemo)(function () {
    return onAuthorize ? /*#__PURE__*/React.createElement(_UnauthorisedViewContent.default, {
      providerName: providerName,
      isProductIntegrationSupported: isProductIntegrationSupported,
      testId: testId
    }) : /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, (0, _extends2.default)({}, _messages.messages[providerName ? 'unauthorised_account_description' : 'unauthorised_account_description_no_provider'], {
      values: {
        context: providerName
      }
    }));
  }, [isProductIntegrationSupported, onAuthorize, providerName, testId]);
  return /*#__PURE__*/React.createElement(UnauthorisedViewFrame, (0, _extends2.default)({}, props, {
    content: content,
    providerName: providerName,
    testId: testId
  }));
};

/**
 * Experiment wrapper: fires social proof exposure and renders social proof UI when FG is on.
 * TODO: remove when social-proof-3p-unauth-block-fg is cleaned up
 */
var UnauthorisedViewWithExperiment = function UnauthorisedViewWithExperiment(_ref5) {
  var _props$cardState, _extractSmartLinkProv2;
  var _ref5$testId = _ref5.testId,
    testId = _ref5$testId === void 0 ? 'smart-block-unauthorized-view' : _ref5$testId,
    props = (0, _objectWithoutProperties2.default)(_ref5, _excluded3);
  var extensionKey = (_props$cardState = props.cardState) === null || _props$cardState === void 0 || (_props$cardState = _props$cardState.details) === null || _props$cardState === void 0 || (_props$cardState = _props$cardState.meta) === null || _props$cardState === void 0 ? void 0 : _props$cardState.key;
  var providerName = (_extractSmartLinkProv2 = (0, _linkExtractors.extractSmartLinkProvider)(props.cardState.details)) === null || _extractSmartLinkProv2 === void 0 ? void 0 : _extractSmartLinkProv2.text;
  var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
    connections = _useSmartLinkContext.connections;
  var _useSocialProofExperi = (0, _useSocialProofExperiment.default)(providerName ? extensionKey : undefined, connections.client.baseUrlOverride),
    isTreatment = _useSocialProofExperi.isTreatment,
    tier = _useSocialProofExperi.tier,
    connectedPct = _useSocialProofExperi.connectedPct;
  var socialProofExperimentMeta = (0, _useSocialProofExperiment.getSocialProofExperimentMeta)({
    extensionKey: extensionKey,
    baseUriWithNoTrailingSlash: connections.client.baseUrlOverride
  });
  var analyticsContextData = {
    attributes: {
      experiment: 'social_proof_3p_unauth_block_exp',
      cohort: isTreatment ? 'treatment' : 'control',
      tier: tier,
      experimentMeta: socialProofExperimentMeta
    }
  };
  if (!isTreatment || !providerName) {
    var fallbackView = /*#__PURE__*/React.createElement(UnauthorisedViewBase, (0, _extends2.default)({}, props, {
      testId: testId
    }));
    return connectedPct !== undefined ? /*#__PURE__*/React.createElement(_analyticsNext.AnalyticsContext, {
      data: analyticsContextData
    }, fallbackView) : fallbackView;
  }
  return /*#__PURE__*/React.createElement(_analyticsNext.AnalyticsContext, {
    data: analyticsContextData
  }, /*#__PURE__*/React.createElement(UnauthorisedViewFrame, (0, _extends2.default)({}, props, {
    content: /*#__PURE__*/React.createElement(_SocialProofMessage.default, {
      tier: tier,
      connectedPct: connectedPct,
      providerName: providerName
    }),
    providerName: providerName,
    testId: testId
  })));
};
var UnauthorisedViewWithoutExperiment = function UnauthorisedViewWithoutExperiment(props) {
  return /*#__PURE__*/React.createElement(UnauthorisedViewBase, props);
};
var UnauthorisedView = (0, _platformFeatureFlagsReact.componentWithFG)('social-proof-3p-unauth-block-fg', UnauthorisedViewWithExperiment, UnauthorisedViewWithoutExperiment);
var _default_1 = (0, _withFlexibleUIBlockCardStyle.withFlexibleUIBlockCardStyle)(UnauthorisedView);
var _default = exports.default = _default_1;