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';
const 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'
});
const NO_EXTERNAL_BADGE_HOSTS = ['atlassian.com', 'loom.com', 'dam-cdn.atl.orangelogic.com'];
const NO_EXTERNAL_BADGE_HOSTS_NEW = ['atlassian.com', 'loom.com', 'dam-cdn.atl.orangelogic.com', 'bitbucket.org'];
export const isUnbadgedUrl = url => {
  if (!url) {
    return false;
  }
  // Check if URL is valid
  try {
    new URL(url);
  } catch {
    return false;
  }
  const parsedUrl = new URL(url || '');
  const {
    hostname,
    pathname,
    protocol
  } = parsedUrl;
  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(host => hostname === host || hostname.endsWith(`.${host}`)));
  }
  return Boolean(hostname && NO_EXTERNAL_BADGE_HOSTS.some(host => hostname === host || hostname.endsWith(`.${host}`)));
};
export const ExternalImageBadge = ({
  type,
  url
}) => {
  const intl = useIntl();
  const 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
  })));
};