import Fuse from 'fuse.js';
import memoizeOne from 'memoize-one';
import { fg } from '@atlaskit/platform-feature-flags';
const processQuickInsertItems = (items, intl) => {
  return items.reduce((acc, item) => {
    if (typeof item === 'function' &&
    // we preserve handler items with disableMemo so that we
    // can process them in a later step outside of memoizations
    !item.disableMemo) {
      const quickInsertItems = item(intl);
      return acc.concat(quickInsertItems);
    }
    return acc.concat(item);
  }, []);
};
const memoizedProcessQuickInsertItems = memoizeOne(processQuickInsertItems);
export const memoProcessQuickInsertItems = (items, intl) => {
  const memoizedResults = memoizedProcessQuickInsertItems(items, intl);
  const hasDisabledMemos = items.some(item => typeof item === 'function' && item.disableMemo);
  if (!hasDisabledMemos) {
    return memoizedResults;
  }
  return memoizedResults.flatMap(item => typeof item === 'function' && item.disableMemo ? item(intl) : item);
};
const options = {
  threshold: 0.3,
  includeScore: true,
  keys: [{
    name: 'title',
    weight: 0.57
  }, {
    name: 'keywords',
    weight: 0.08
  }, {
    name: 'description',
    weight: 0.04
  }, {
    name: 'keyshortcut',
    weight: 0.01
  }]
};

/**
 * This function is used to find and sort QuickInsertItems based on a given query string.
 *
 * @export
 * @param {string} query - The query string to be used in the search.
 * @param {QuickInsertItem[]} items - An array of QuickInsertItems to be searched.
 * @returns {QuickInsertItem[]} - Returns a sorted array of QuickInsertItems based on the priority. If the query string is empty, it will return the array sorted by priority. If a query string is provided, it will return an array of QuickInsertItems that match the query string, sorted by relevance to the query.
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export function find(query, items, prioritySortingFn) {
  if (query === '') {
    // Copy and sort list by priority
    return items.slice(0).sort((a, b) => (a.priority || Number.POSITIVE_INFINITY) - (b.priority || Number.POSITIVE_INFINITY));
  }
  const fuseOptions = {
    ...options
  };
  if (prioritySortingFn) {
    const sortFn = prioritySortingFn(items);
    // prioritySortingFn will trigger the experiment exposure, but sortFn
    // will be undefined for the control group.
    if (sortFn) {
      fuseOptions.sortFn = sortFn;
    }
  }
  const fuse = new Fuse(items, fuseOptions);
  const results = fuse.search(query);
  if (fg('jim-lower-ranking-in-jira-macro-search')) {
    var _results$datasourceIn, _results$legacyIndex$;
    // searching for jira work items macro first
    const datasourceIndex = results.findIndex(r => {
      var _r$item$keywords;
      return r.item.id === 'datasource' && ((_r$item$keywords = r.item.keywords) === null || _r$item$keywords === void 0 ? void 0 : _r$item$keywords.includes('jira'));
    });

    //  then searching for the legacy jira macro
    const legacyIndex = results.findIndex(r => typeof r.item.key === 'string' && r.item.key.endsWith(':jira'));

    // the jira legcy macro is found before the jira work items macro then swap the two
    if (datasourceIndex > 0 && legacyIndex >= 0 && legacyIndex < datasourceIndex && Math.abs(((_results$datasourceIn = results[datasourceIndex].score) !== null && _results$datasourceIn !== void 0 ? _results$datasourceIn : 0) - ((_results$legacyIndex$ = results[legacyIndex].score) !== null && _results$legacyIndex$ !== void 0 ? _results$legacyIndex$ : 0)) < 0.2) {
      const [datasource] = results.splice(datasourceIndex, 1);
      results.splice(legacyIndex, 0, datasource);
    }
  }
  return results.map(result => result.item);
}