"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractSmartLinkIcon = exports.extractLinkIcon = exports.extractErrorIcon = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _constants = require("../../../constants");
var _extractIconRenderer = _interopRequireDefault(require("./extract-icon-renderer"));
var _extractJsonldDataIcon = _interopRequireDefault(require("./extract-jsonld-data-icon"));
var _extractProviderIcon = _interopRequireDefault(require("./extract-provider-icon"));
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var extractLinkIcon = exports.extractLinkIcon = function extractLinkIcon(response, renderers) {
  var data = response.data;
  var render = (0, _extractIconRenderer.default)(data, renderers);
  return _objectSpread(_objectSpread({}, (0, _extractJsonldDataIcon.default)(data)), {}, {
    render: render
  });
};
var extractErrorIcon = exports.extractErrorIcon = function extractErrorIcon(response, status) {
  // Try to get provider icon first.
  if (response) {
    var data = response.data;
    var _ref = (0, _extractProviderIcon.default)(data) || {},
      icon = _ref.icon,
      url = _ref.url;
    if (icon || url) {
      return {
        icon: icon,
        url: url
      };
    }
  }

  // Otherwise, use fallback icon for each status.
  switch (status) {
    case _constants.SmartLinkStatus.Forbidden:
    case _constants.SmartLinkStatus.Unauthorized:
      return {
        icon: _constants.IconType.Forbidden
      };
    case _constants.SmartLinkStatus.NotFound:
      return {
        icon: _constants.IconType.Error
      };
    case _constants.SmartLinkStatus.Errored:
    case _constants.SmartLinkStatus.Fallback:
    default:
      return {
        icon: _constants.IconType.Default
      };
  }
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
var extractSmartLinkIcon = exports.extractSmartLinkIcon = function extractSmartLinkIcon(response, renderers) {
  if (!response || !(response !== null && response !== void 0 && response.data)) {
    return undefined;
  }
  if ((0, _linkExtractors.isEntityPresent)(response)) {
    return (0, _linkExtractors.extractEntityIcon)(response);
  }
  return extractLinkIcon(response, renderers);
};