import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var SCORE_PROXIMITY_THRESHOLD = 0.2;

/**
 * The extension type for Rovo skills, used to identify skill items in the
 * quick insert menu. Matches the value defined in
 * @atlassian/conversation-assistant-content-transformer/utils/skills.
 *
 * Intentionally duplicated here to avoid adding a runtime dependency from
 * editor-common to the conversation-assistant packages.
 */
var ROVO_SKILL_EXTENSION_TYPE = 'com.atlassian.rovo.skill';

/**
 * Promotes native editor elements above Rovo skill items in search results
 * when their Fuse scores are close.
 *
 * An item is classified as a Rovo skill when its `extensionType` property
 * (set by `buildMenuItem` for all extension manifest items) matches the
 * Rovo skill extension type. Native editor items (Link, Code snippet, Table,
 * etc.) and other non-Rovo extensions are unaffected, regardless of their
 * priority value.
 *
 * When a skill and a native item have similar scores (within the proximity
 * threshold), the native item is promoted above the skill. But when a skill
 * is a significantly better match (e.g. searching "research" and the skill is
 * "Research Insights" while the native item is "Live search"), the skill keeps
 * its higher rank.
 */
export function boostNativeResultsAboveSkills(results) {
  var isSkill = function isSkill(r) {
    return r.item.extensionType === ROVO_SKILL_EXTENSION_TYPE;
  };
  var reordered = results.slice(0);
  for (var i = 1; i < reordered.length; i++) {
    if (isSkill(reordered[i])) {
      continue;
    }
    // reordered[i] is a native item — check if there are skills
    // ranked above it that have a similar score.
    var insertAt = i;
    while (insertAt > 0 && isSkill(reordered[insertAt - 1]) && Math.abs(((_reordered$score = reordered[insertAt - 1].score) !== null && _reordered$score !== void 0 ? _reordered$score : 0) - ((_reordered$i$score = reordered[i].score) !== null && _reordered$i$score !== void 0 ? _reordered$i$score : 0)) < SCORE_PROXIMITY_THRESHOLD) {
      var _reordered$score, _reordered$i$score;
      insertAt--;
    }
    if (insertAt < i) {
      var _reordered$splice = reordered.splice(i, 1),
        _reordered$splice2 = _slicedToArray(_reordered$splice, 1),
        native = _reordered$splice2[0];
      reordered.splice(insertAt, 0, native);
    }
  }
  return reordered;
}