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

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

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

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