"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.boostNativeResultsAboveSkills = boostNativeResultsAboveSkills;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
// Items with priority >= this threshold are treated as extension-provided
// items (e.g. Rovo skills) and demoted below native editor elements.
// Coupled with priority: 9000 set in
// platform/packages/ai-mate/rovo-skills/src/services/insert-menu-provider/extension-manifest.tsx
var SKILL_PRIORITY_THRESHOLD = 1000;
var SCORE_PROXIMITY_THRESHOLD = 0.2;

/**
 * Promotes native editor elements above extension-provided items (e.g. Rovo
 * skills) in search results when their Fuse scores are close.
 *
 * Skills set a high priority value (e.g. 9000) while native items have no
 * priority (undefined / 0). 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.
 */
function boostNativeResultsAboveSkills(results) {
  var isSkill = function isSkill(r) {
    var _r$item$priority;
    return ((_r$item$priority = r.item.priority) !== null && _r$item$priority !== void 0 ? _r$item$priority : 0) >= SKILL_PRIORITY_THRESHOLD;
  };
  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 = (0, _slicedToArray2.default)(_reordered$splice, 1),
        native = _reordered$splice2[0];
      reordered.splice(insertAt, 0, native);
    }
  }
  return reordered;
}