import { extractEntityIcon, extractEntityProvider, extractProvider, extractSmartLinkTitle, extractSmartLinkUrl, extractType, isEntityPresent } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
import { getEmptyJsonLd } from '../../utils/jsonld';
import { extractIcon } from '../common/icon';
import { extractLozenge } from '../common/lozenge';
import { extractTitleTextColor } from '../common/primitives';
import { extractTitlePrefix } from '../common/title-prefix';
import { CONFLUENCE_GENERATOR_ID, JIRA_GENERATOR_ID } from '../constants';
const extractInlineIcon = (jsonLd, showIconLabel = true) => {
  const provider = extractProvider(jsonLd);
  if (provider && provider.id) {
    if (provider.id === CONFLUENCE_GENERATOR_ID || provider.id === JIRA_GENERATOR_ID) {
      return extractIcon(jsonLd, 'type', showIconLabel);
    }
  }
  return extractIcon(jsonLd, 'provider', showIconLabel);
};

/**
 * Should be moved to the smart link-extractor when jsonld is deprecated
 */
const extractSmartLinkInlineIcon = (response, showLabel = true) => {
  if (isEntityPresent(response)) {
    if (fg('platform_lp_use_entity_icon_url_for_icon')) {
      const entityIcon = extractEntityIcon(response);
      if (entityIcon) {
        return [entityIcon.url, entityIcon.label];
      }
    }
    const provider = extractEntityProvider(response);
    if (provider) {
      return provider.icon;
    }
  }
  return extractInlineIcon((response === null || response === void 0 ? void 0 : response.data) || getEmptyJsonLd(), showLabel);
};
export const extractInlineProps = (response, renderers, removeTextHighlightingFromTitle, showLabel = true) => {
  const jsonLd = (response === null || response === void 0 ? void 0 : response.data) || getEmptyJsonLd();
  return {
    link: extractSmartLinkUrl(response),
    title: extractSmartLinkTitle(response, removeTextHighlightingFromTitle),
    icon: extractSmartLinkInlineIcon(response, showLabel),
    // As we migrate to support more entities we can incorporate these fields
    lozenge: extractLozenge(jsonLd),
    titleTextColor: extractTitleTextColor(jsonLd),
    titlePrefix: extractTitlePrefix(jsonLd, renderers, 'inline'),
    type: extractType(jsonLd)
  };
};