<commit_before>/* @flow */

import update from 'immutability-helper';
import findIndex from 'lodash/findIndex';
import findLastIndex from 'lodash/findLastIndex';
import invertBy from 'lodash/invertBy';
import sortBy from 'lodash/sortBy';

export type Item<T> = {
  groupId: string;
  id: string;
  orderHint: number;
  value: T;
};

type PersistedData = {
  order: Array<{
    groupId: string;
    id: string;
    orderHint: number;
    lastUse: number;
  }>;
};

function itemToCombinedId(x: Item<any>) {
  return `${JSON.stringify(x.groupId)}:${JSON.stringify(x.id)}`;
}

export default class OrderManager<T> {
  _items: Array<Item<T>> = [];
  _storage: Storage;
  _needsSort = false;
  _get: () => PersistedData;
  _set: (x: PersistedData) => void;
  _maxLimit: number;
  constructor(options: {get(): PersistedData, set(x: PersistedData): void, maxLimit?: number}) {
    this._get = options.get;
    this._set = options.set;
    this._maxLimit = options.maxLimit || 200;
  }
  _read(): PersistedData {
    const pdata = this._get();
    return pdata == null ? {order: []} : pdata;
  }
  _save(pdata: PersistedData) {
    if (pdata.order.length > this._maxLimit) {
      const itemsAndIndexes = pdata.order.map((item,index) => ({item, index}));
      const indexesToRemove = sortBy(itemsAndIndexes, x => x.item.lastUse)
        .slice(0, pdata.order.length - this._maxLimit)
        .map(x => x.index);
      const sortedIndexesToRemove = sortBy(indexesToRemove, x => -x);
      pdata = update(pdata, {order: {$splice: sortedIndexesToRemove.map(i => [i, 1])}});
    }
    this._set(pdata);
  }
  _updatePersistedDataWithItem(item: Item<T>): PersistedData {
    let pdata = this._read();

    let ixOfId = findIndex(
      pdata.order,<commit_msg>Fix type.<commit_after>/* @flow */

import update from 'immutability-helper';
import findIndex from 'lodash/findIndex';
import findLastIndex from 'lodash/findLastIndex';
import invertBy from 'lodash/invertBy';
import sortBy from 'lodash/sortBy';

export type Item<T> = {
  groupId: string;
  id: string;
  orderHint: number;
  value: T;
};

type PersistedData = {
  order: Array<{
    groupId: string;
    id: string;
    orderHint: number;
    lastUse: number;
  }>;
};

function itemToCombinedId(x: Item<any>) {
  return `${JSON.stringify(x.groupId)}:${JSON.stringify(x.id)}`;
}

export default class OrderManager<T> {
  _items: Array<Item<T>> = [];
  _storage: Storage;
  _needsSort = false;
  _get: () => ?PersistedData;
  _set: (x: PersistedData) => void;
  _maxLimit: number;
  constructor(options: {get(): PersistedData, set(x: PersistedData): void, maxLimit?: number}) {
    this._get = options.get;
    this._set = options.set;
    this._maxLimit = options.maxLimit || 200;
  }
  _read(): PersistedData {
    const pdata = this._get();
    return pdata == null ? {order: []} : pdata;
  }
  _save(pdata: PersistedData) {
    if (pdata.order.length > this._maxLimit) {
      const itemsAndIndexes = pdata.order.map((item,index) => ({item, index}));
      const indexesToRemove = sortBy(itemsAndIndexes, x => x.item.lastUse)
        .slice(0, pdata.order.length - this._maxLimit)
        .map(x => x.index);
      const sortedIndexesToRemove = sortBy(indexesToRemove, x => -x);
      pdata = update(pdata, {order: {$splice: sortedIndexesToRemove.map(i => [i, 1])}});
    }
    this._set(pdata);
  }
  _updatePersistedDataWithItem(item: Item<T>): PersistedData {
    let pdata = this._read();

    let ixOfId = findIndex(
      pdata.order,
{'fraction_non_alphanumeric': 0.1540223303750358, 'fraction_numerical': 0.0028628685943315204, 'mean_word_length': 3.7280108254397835, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7167645', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1220, 'n_words': 272}