import _typeof from "@babel/runtime/helpers/typeof";
import { extractEntity, extractPersonAssignedTo, extractPersonCreatedBy, extractPersonOwnedBy, extractPersonUpdatedBy, extractSmartLinkUrl, isEntityPresent } from '@atlaskit/link-extractors';
var extractLinkName = function extractLinkName(link) {
  if (link && _typeof(link) === 'object' && link['@type'] === 'Link') {
    return link.name;
  }
};
var extractValue = function extractValue(data, key) {
  return data === null || data === void 0 ? void 0 : data[key];
};
export var extractCommentCount = function extractCommentCount(data) {
  return extractValue(data, 'schema:commentCount');
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
export var extractSmartLinkCommentCount = function extractSmartLinkCommentCount(response) {
  if (!response || !response.data) {
    return undefined;
  }
  if (isEntityPresent(response)) {
    var entity = 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);
};
export var extractAppliedToComponentsCount = function extractAppliedToComponentsCount(data) {
  return extractValue(data, 'atlassian:appliedToComponentsCount');
};
export var extractDueOn = function extractDueOn(data) {
  return extractValue(data, 'endTime');
};
export var extractViewCount = function extractViewCount(data) {
  return extractValue(data, 'atlassian:viewCount');
};
export var extractReactCount = function extractReactCount(data) {
  return extractValue(data, 'atlassian:reactCount');
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
export var extractSmartLinkReactCount = function extractSmartLinkReactCount(response) {
  if (!response || !response.data) {
    return undefined;
  }
  if (isEntityPresent(response)) {
    var entity = 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);
};
export var extractVoteCount = function extractVoteCount(data) {
  return extractValue(data, 'atlassian:voteCount');
};
export var extractOwnedBy = function extractOwnedBy(data) {
  var persons = extractPersonOwnedBy(data);
  if (persons && persons.length) {
    return persons[0].name;
  }
};
export var extractAssignedTo = function extractAssignedTo(data) {
  var person = extractPersonAssignedTo(data);
  if (person) {
    return person.name;
  }
};
export var extractPersonAssignedToAsArray = function extractPersonAssignedToAsArray(data) {
  var person = extractPersonAssignedTo(data);
  return person ? [person] : undefined;
};
export var extractCreatedBy = function extractCreatedBy(data) {
  var persons = extractPersonCreatedBy(data);
  if (persons && persons.length) {
    return persons[0].name;
  }
};
export var extractModifiedBy = function extractModifiedBy(data) {
  var person = extractPersonUpdatedBy(data);
  if (person) {
    return person.name;
  }
};
export var extractProgrammingLanguage = function extractProgrammingLanguage(data) {
  return extractValue(data, 'schema:programmingLanguage');
};
export var extractSourceBranch = function extractSourceBranch(data) {
  return extractLinkName(data['atlassian:mergeSource']);
};
export var extractSubscriberCount = function extractSubscriberCount(data) {
  return extractValue(data, 'atlassian:subscriberCount');
};
export var 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;
};
export var extractAttachmentCount = function extractAttachmentCount(data) {
  return extractValue(data, 'atlassian:attachmentCount');
};
export var extractTargetBranch = function extractTargetBranch(data) {
  return extractLinkName(data['atlassian:mergeDestination']);
};
export var extractChecklistProgress = function extractChecklistProgress(data) {
  var checkItemsObj = extractValue(data, 'atlassian:checkItems');
  return checkItemsObj ? "".concat(checkItemsObj.checkedItems, "/").concat(checkItemsObj.totalItems) : undefined;
};
export var 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
    };
  }
};
export var extractSubTasksProgress = function extractSubTasksProgress(data) {
  var subTasksObject = extractValue(data, 'atlassian:subTasks');
  return subTasksObject && subTasksObject.totalCount ? "".concat(subTasksObject.resolvedCount, "/").concat(subTasksObject.totalCount) : undefined;
};
export var extractUserAttributes = function extractUserAttributes(data) {
  return data === null || data === void 0 ? void 0 : data.userAttributes;
};
export var extractReadTime = function extractReadTime(data) {
  return extractValue(data, 'atlassian:readTimeInMinutes');
};
export var extractSentOn = function extractSentOn(data) {
  return extractValue(data, 'dateSent');
};

/**
 * Should be moved to link-extractors when jsonLd is deprecated
 */
export var extractSmartLinkSentOn = function extractSmartLinkSentOn(response) {
  if (!response || !response.data) {
    return undefined;
  }
  if (isEntityPresent(response)) {
    var _extractEntity;
    return (_extractEntity = 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);
};
export var extractStoryPoints = function extractStoryPoints(data) {
  return extractValue(data, 'atlassian:storyPoints');
};
export var extractMetaObjectId = function extractMetaObjectId(meta) {
  return meta.objectId;
};
export var extractMetaResourceType = function extractMetaResourceType(meta) {
  return meta.resourceType;
};
export var extractMetaTenantId = function extractMetaTenantId(meta) {
  return meta.tenantId;
};
export var extractHostName = function extractHostName(response) {
  try {
    var url = extractSmartLinkUrl(response);
    var hostName = url ? new URL(url).hostname : undefined;
    return hostName;
  } catch (_unused) {
    return undefined;
  }
};