import { extractEntity, extractEntityProvider, extractTitle, isEntityPresent } from '@atlaskit/link-extractors';
import { IconType } from '../../../constants';
import { CONFLUENCE_GENERATOR_ID, JIRA_GENERATOR_ID } from '../../constants';
import extractUrlIcon from './extract-url-icon';
/**
 * Extracts the provider icon and label from the given JSON-LD data.
 *
 * If the data is from Confluence, the icon will be the Confluence icon and the label will be 'Confluence'.
 * If the data is from Jira, the icon will be the Jira icon and the label will be 'Jira'.
 * If the data is from another provider, the icon and label will be extracted from the generator object.
 *
 * @param data - The JSON-LD data to extract the provider icon and label from.
 * @returns IconDescriptor with optional icon string name, url and label, or `undefined` if the data is not provided.
 */
const extractProviderIcon = data => {
  if (!data) {
    return undefined;
  }
  const generator = data.generator;
  const provider = generator === null || generator === void 0 ? void 0 : generator['@id'];
  const icon = generator === null || generator === void 0 ? void 0 : generator.icon;
  const label = (generator === null || generator === void 0 ? void 0 : generator.name) || extractTitle(data);
  if (provider === CONFLUENCE_GENERATOR_ID) {
    return {
      icon: IconType.Confluence,
      label: label || 'Confluence'
    };
  }
  if (provider === JIRA_GENERATOR_ID) {
    return {
      icon: IconType.Jira,
      label: label || 'Jira'
    };
  }
  return extractUrlIcon(icon, label);
};
export default extractProviderIcon;

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
export const extractSmartLinkProviderIcon = response => {
  var _extractEntity, _generator$icon;
  if (!response || !(response !== null && response !== void 0 && response.data)) {
    return undefined;
  }
  if (isEntityPresent(response)) {
    const provider = extractEntityProvider(response);
    if (!provider) {
      return undefined;
    }
    switch (provider.id) {
      case CONFLUENCE_GENERATOR_ID:
        return {
          icon: IconType.Confluence,
          label: provider.text || 'Confluence'
        };
      case JIRA_GENERATOR_ID:
        return {
          icon: IconType.Jira,
          label: provider.text || 'Jira'
        };
      default:
        const {
          generator
        } = response.meta;
        if (!generator) {
          return undefined;
        }
        return {
          label: generator.name || ((_extractEntity = extractEntity(response)) === null || _extractEntity === void 0 ? void 0 : _extractEntity.displayName),
          url: (_generator$icon = generator.icon) === null || _generator$icon === void 0 ? void 0 : _generator$icon.url
        };
    }
  }
  return extractProviderIcon(response.data);
};