"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractSmartLinkProviderIcon = exports.default = void 0;
var _linkExtractors = require("@atlaskit/link-extractors");
var _constants = require("../../../constants");
var _constants2 = require("../../constants");
var _extractUrlIcon = _interopRequireDefault(require("./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.
 */
var extractProviderIcon = function extractProviderIcon(data) {
  if (!data) {
    return undefined;
  }
  var generator = data.generator;
  var provider = generator === null || generator === void 0 ? void 0 : generator['@id'];
  var icon = generator === null || generator === void 0 ? void 0 : generator.icon;
  var label = (generator === null || generator === void 0 ? void 0 : generator.name) || (0, _linkExtractors.extractTitle)(data);
  if (provider === _constants2.CONFLUENCE_GENERATOR_ID) {
    return {
      icon: _constants.IconType.Confluence,
      label: label || 'Confluence'
    };
  }
  if (provider === _constants2.JIRA_GENERATOR_ID) {
    return {
      icon: _constants.IconType.Jira,
      label: label || 'Jira'
    };
  }
  return (0, _extractUrlIcon.default)(icon, label);
};
var _default = exports.default = extractProviderIcon;
/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
var extractSmartLinkProviderIcon = exports.extractSmartLinkProviderIcon = function extractSmartLinkProviderIcon(response) {
  var _extractEntity, _generator$icon;
  if (!response || !(response !== null && response !== void 0 && response.data)) {
    return undefined;
  }
  if ((0, _linkExtractors.isEntityPresent)(response)) {
    var provider = (0, _linkExtractors.extractEntityProvider)(response);
    if (!provider) {
      return undefined;
    }
    switch (provider.id) {
      case _constants2.CONFLUENCE_GENERATOR_ID:
        return {
          icon: _constants.IconType.Confluence,
          label: provider.text || 'Confluence'
        };
      case _constants2.JIRA_GENERATOR_ID:
        return {
          icon: _constants.IconType.Jira,
          label: provider.text || 'Jira'
        };
      default:
        var _ref = response.meta,
          generator = _ref.generator;
        if (!generator) {
          return undefined;
        }
        return {
          label: generator.name || ((_extractEntity = (0, _linkExtractors.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);
};