"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isUnbadgedUrl = exports.ExternalImageBadge = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactIntl = require("react-intl");
var _statusInformation = _interopRequireDefault(require("@atlaskit/icon/core/status-information"));
var _primitives = require("@atlaskit/primitives");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
var _media = require("../media");
// eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- to be migrated to @atlaskit/primitives/compiled – go/akcss

var baseStyles = (0, _primitives.xcss)({
  borderRadius: 'radius.small',
  backgroundColor: 'elevation.surface',
  // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
  lineHeight: "var(--ds-space-200, 16px)",
  cursor: 'pointer'
});
var NO_EXTERNAL_BADGE_HOSTS = ['atlassian.com', 'loom.com', 'dam-cdn.atl.orangelogic.com'];
var NO_EXTERNAL_BADGE_HOSTS_NEW = ['atlassian.com', 'loom.com', 'dam-cdn.atl.orangelogic.com', 'bitbucket.org'];
var isUnbadgedUrl = exports.isUnbadgedUrl = function isUnbadgedUrl(url) {
  if (!url) {
    return false;
  }
  // Check if URL is valid
  try {
    new URL(url);
  } catch (_unused) {
    return false;
  }
  var parsedUrl = new URL(url || '');
  var hostname = parsedUrl.hostname,
    pathname = parsedUrl.pathname,
    protocol = parsedUrl.protocol;
  if (protocol === 'data:') {
    return pathname === null || pathname === void 0 ? void 0 : pathname.startsWith('image/');
  }
  if ((0, _expValEquals.expValEquals)('platform_editor_media_external_badge_bbc_fix', 'isEnabled', true)) {
    return Boolean(hostname && NO_EXTERNAL_BADGE_HOSTS_NEW.some(function (host) {
      return hostname === host || hostname.endsWith(".".concat(host));
    }));
  }
  return Boolean(hostname && NO_EXTERNAL_BADGE_HOSTS.some(function (host) {
    return hostname === host || hostname.endsWith(".".concat(host));
  }));
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var ExternalImageBadge = exports.ExternalImageBadge = function ExternalImageBadge(_ref) {
  var type = _ref.type,
    url = _ref.url;
  var intl = (0, _reactIntl.useIntl)();
  var message = intl.formatMessage(_media.externalMediaMessages.externalMediaFile);
  if (type !== 'external' || isUnbadgedUrl(url)) {
    return null;
  }
  return /*#__PURE__*/_react.default.createElement(_primitives.Box, {
    padding: "space.050",
    xcss: baseStyles,
    tabIndex: 0
  }, /*#__PURE__*/_react.default.createElement("div", {
    "data-testid": "external-image-badge"
  }), /*#__PURE__*/_react.default.createElement(_tooltip.default, {
    content: message,
    position: "top"
  }, /*#__PURE__*/_react.default.createElement(_statusInformation.default, {
    label: message
  })));
};