"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractVoteCount = exports.extractViewCount = exports.extractUserAttributes = exports.extractTeamMemberCount = exports.extractTargetBranch = exports.extractSubscriberCount = exports.extractSubTasksProgress = exports.extractStoryPoints = exports.extractSourceBranch = exports.extractSmartLinkSentOn = exports.extractSmartLinkReactCount = exports.extractSmartLinkCommentCount = exports.extractSentOn = exports.extractReadTime = exports.extractReactCount = exports.extractProgrammingLanguage = exports.extractPersonAssignedToAsArray = exports.extractOwnedBy = exports.extractModifiedBy = exports.extractMetaTenantId = exports.extractMetaResourceType = exports.extractMetaObjectId = exports.extractLocation = exports.extractHostName = exports.extractDueOn = exports.extractCreatedBy = exports.extractCommentCount = exports.extractChecklistProgress = exports.extractAttachmentCount = exports.extractAssignedTo = exports.extractAppliedToComponentsCount = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _linkExtractors = require("@atlaskit/link-extractors");
var extractLinkName = function extractLinkName(link) {
  if (link && (0, _typeof2.default)(link) === 'object' && link['@type'] === 'Link') {
    return link.name;
  }
};
var extractValue = function extractValue(data, key) {
  return data === null || data === void 0 ? void 0 : data[key];
};
var extractCommentCount = exports.extractCommentCount = function extractCommentCount(data) {
  return extractValue(data, 'schema:commentCount');
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
var extractSmartLinkCommentCount = exports.extractSmartLinkCommentCount = function extractSmartLinkCommentCount(response) {
  if (!response || !response.data) {
    return undefined;
  }
  if ((0, _linkExtractors.isEntityPresent)(response)) {
    var entity = (0, _linkExtractors.extractEntity)(response);
    return entity && 'commentCount' in entity && typeof entity.commentCount === 'number' ? entity === null || entity === void 0 ? void 0 : entity.commentCount : undefined;
  }
  return (response === null || response === void 0 ? void 0 : response.data) && extractCommentCount(response === null || response === void 0 ? void 0 : response.data);
};
var extractAppliedToComponentsCount = exports.extractAppliedToComponentsCount = function extractAppliedToComponentsCount(data) {
  return extractValue(data, 'atlassian:appliedToComponentsCount');
};
var extractDueOn = exports.extractDueOn = function extractDueOn(data) {
  return extractValue(data, 'endTime');
};
var extractViewCount = exports.extractViewCount = function extractViewCount(data) {
  return extractValue(data, 'atlassian:viewCount');
};
var extractReactCount = exports.extractReactCount = function extractReactCount(data) {
  return extractValue(data, 'atlassian:reactCount');
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
var extractSmartLinkReactCount = exports.extractSmartLinkReactCount = function extractSmartLinkReactCount(response) {
  if (!response || !response.data) {
    return undefined;
  }
  if ((0, _linkExtractors.isEntityPresent)(response)) {
    var entity = (0, _linkExtractors.extractEntity)(response);
    var reactions = entity && 'reactions' in entity && Array.isArray(entity.reactions) ? entity.reactions : undefined;
    return reactions === null || reactions === void 0 ? void 0 : reactions.reduce(function (total, reaction) {
      return total + (reaction === null || reaction === void 0 ? void 0 : reaction.total);
    }, 0);
  }
  return (response === null || response === void 0 ? void 0 : response.data) && extractReactCount(response === null || response === void 0 ? void 0 : response.data);
};
var extractVoteCount = exports.extractVoteCount = function extractVoteCount(data) {
  return extractValue(data, 'atlassian:voteCount');
};
var extractOwnedBy = exports.extractOwnedBy = function extractOwnedBy(data) {
  var persons = (0, _linkExtractors.extractPersonOwnedBy)(data);
  if (persons && persons.length) {
    return persons[0].name;
  }
};
var extractAssignedTo = exports.extractAssignedTo = function extractAssignedTo(data) {
  var person = (0, _linkExtractors.extractPersonAssignedTo)(data);
  if (person) {
    return person.name;
  }
};
var extractPersonAssignedToAsArray = exports.extractPersonAssignedToAsArray = function extractPersonAssignedToAsArray(data) {
  var person = (0, _linkExtractors.extractPersonAssignedTo)(data);
  return person ? [person] : undefined;
};
var extractCreatedBy = exports.extractCreatedBy = function extractCreatedBy(data) {
  var persons = (0, _linkExtractors.extractPersonCreatedBy)(data);
  if (persons && persons.length) {
    return persons[0].name;
  }
};
var extractModifiedBy = exports.extractModifiedBy = function extractModifiedBy(data) {
  var person = (0, _linkExtractors.extractPersonUpdatedBy)(data);
  if (person) {
    return person.name;
  }
};
var extractProgrammingLanguage = exports.extractProgrammingLanguage = function extractProgrammingLanguage(data) {
  return extractValue(data, 'schema:programmingLanguage');
};
var extractSourceBranch = exports.extractSourceBranch = function extractSourceBranch(data) {
  return extractLinkName(data['atlassian:mergeSource']);
};
var extractSubscriberCount = exports.extractSubscriberCount = function extractSubscriberCount(data) {
  return extractValue(data, 'atlassian:subscriberCount');
};
var extractTeamMemberCount = exports.extractTeamMemberCount = function extractTeamMemberCount(data) {
  var val = data !== null && data !== void 0 && data.attributedTo ? Array.isArray(data === null || data === void 0 ? void 0 : data.attributedTo) ? data === null || data === void 0 ? void 0 : data.attributedTo.length : 0 : 0;
  return val;
};
var extractAttachmentCount = exports.extractAttachmentCount = function extractAttachmentCount(data) {
  return extractValue(data, 'atlassian:attachmentCount');
};
var extractTargetBranch = exports.extractTargetBranch = function extractTargetBranch(data) {
  return extractLinkName(data['atlassian:mergeDestination']);
};
var extractChecklistProgress = exports.extractChecklistProgress = function extractChecklistProgress(data) {
  var checkItemsObj = extractValue(data, 'atlassian:checkItems');
  return checkItemsObj ? "".concat(checkItemsObj.checkedItems, "/").concat(checkItemsObj.totalItems) : undefined;
};
var extractLocation = exports.extractLocation = function extractLocation(data) {
  var _ref = extractValue(data, 'location') || {},
    url = _ref.url,
    name = _ref.name;
  if (url && name && typeof url === 'string') {
    return {
      text: name,
      url: url
    };
  }
};
var extractSubTasksProgress = exports.extractSubTasksProgress = function extractSubTasksProgress(data) {
  var subTasksObject = extractValue(data, 'atlassian:subTasks');
  return subTasksObject && subTasksObject.totalCount ? "".concat(subTasksObject.resolvedCount, "/").concat(subTasksObject.totalCount) : undefined;
};
var extractUserAttributes = exports.extractUserAttributes = function extractUserAttributes(data) {
  return data === null || data === void 0 ? void 0 : data.userAttributes;
};
var extractReadTime = exports.extractReadTime = function extractReadTime(data) {
  return extractValue(data, 'atlassian:readTimeInMinutes');
};
var extractSentOn = exports.extractSentOn = function extractSentOn(data) {
  return extractValue(data, 'dateSent');
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
var extractSmartLinkSentOn = exports.extractSmartLinkSentOn = function extractSmartLinkSentOn(response) {
  if (!response || !response.data) {
    return undefined;
  }
  if ((0, _linkExtractors.isEntityPresent)(response)) {
    var _extractEntity;
    return (_extractEntity = (0, _linkExtractors.extractEntity)(response)) === null || _extractEntity === void 0 ? void 0 : _extractEntity.createdAt;
  }
  return (response === null || response === void 0 ? void 0 : response.data) && extractSentOn(response === null || response === void 0 ? void 0 : response.data);
};
var extractStoryPoints = exports.extractStoryPoints = function extractStoryPoints(data) {
  return extractValue(data, 'atlassian:storyPoints');
};
var extractMetaObjectId = exports.extractMetaObjectId = function extractMetaObjectId(meta) {
  return meta.objectId;
};
var extractMetaResourceType = exports.extractMetaResourceType = function extractMetaResourceType(meta) {
  return meta.resourceType;
};
var extractMetaTenantId = exports.extractMetaTenantId = function extractMetaTenantId(meta) {
  return meta.tenantId;
};
var extractHostName = exports.extractHostName = function extractHostName(response) {
  try {
    var url = (0, _linkExtractors.extractSmartLinkUrl)(response);
    var hostName = url ? new URL(url).hostname : undefined;
    return hostName;
  } catch (_unused) {
    return undefined;
  }
};