import React from 'react';
import { useIntl } from 'react-intl-next';
import InfoIcon from '@atlaskit/icon/core/status-information';
// eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- to be migrated to @atlaskit/primitives/compiled – go/akcss
import { Box, xcss } from '@atlaskit/primitives';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import Tooltip from '@atlaskit/tooltip';
import { externalMediaMessages } from '../media';
var baseStyles = 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'];
export var 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 (expValEquals('platform_editor_media_external_badge_bbc_fix', 'isEnable', 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));
  }));
};
export var ExternalImageBadge = function ExternalImageBadge(_ref) {
  var type = _ref.type,
    url = _ref.url;
  var intl = useIntl();
  var message = intl.formatMessage(externalMediaMessages.externalMediaFile);
  if (type !== 'external' || isUnbadgedUrl(url)) {
    return null;
  }
  return /*#__PURE__*/React.createElement(Box, {
    padding: "space.050",
    xcss: baseStyles,
    tabIndex: 0
  }, /*#__PURE__*/React.createElement("div", {
    "data-testid": "external-image-badge"
  }), /*#__PURE__*/React.createElement(Tooltip, {
    content: message,
    position: "top"
  }, /*#__PURE__*/React.createElement(InfoIcon, {
    label: message
  })));
};