<commit_before>import Ember from 'ember';
import Token from './token';
import layout from './template';
import { setCursor, KEYS } from './util';

const { set, on, run, getProperties, computed, get } = Ember;

function sanitizeTokens(tokens) {
  return tokens.reduce(function(sum, token) {
    let t = sanitizeToken(token);
    if (t) {
      if (sum[t.modifier]) {
        sum[t.modifier].push(t);
      } else {
        sum[t.modifier] = [t];
      }
    }
    return sum;
  }, Object.create(null));
}

function sanitizeToken(token) {
  let type = get(token, 'type');
  if (type !== 'space' && type !== 'default') {
    return getProperties(token, 'model', 'fullText', 'modifier', 'value');
  }
  return null;
}

function getDefaultContent(modifierConfig, modifiersList) {
  let key, val, list;
  let allList = [];

  for (key in modifierConfig) {
    val = modifierConfig[key];
    if (val.type === 'list' && val.content) {
      list = val.content.map(function(item) {
        return {
          label: item.label,
          value: key + item.value,
          fullText: true,
          section: val.sectionTitle
        };
      });
      allList = allList.concat(list);
    }
  }
  var modifiers = modifiersList.map(function(item) {
    item.section = 'modifiers';
    return item;
  });
  return allList.concat(modifiers);
}

function getAllModifiers(modifierConfig) {
  let modifiers = [];
  for (let key in modifierConfig) {
    let config = modifierConfig[key];
    let section = config.type === 'date' ? 'time' : 'others';
    modifiers.push({
      value: key,
      label: config.defaultHint,
      modifier: true,
      section
    });
  }
  return modifiers;
}

function tokenize(text, modifierConfig) {
  let tokens = [];
  let mem = '';
  let i = 0;

  for (i = 0; i <= text.length; i++) {
    var character = text[i];
    if ((character === ' ' || !character)) {
      if (mem) {
        tokens.push(Token.create({ modifierConfig, fullText: mem }));
      }
      if (character) {
        tokens.push(Token.create({ modifierConfig, fullText: ' ' }));
      }
      mem = '';
    } else if (character !== ' ') {
      mem += character;
    }
  }
  return tokens;
}

function prepareConig(modifierConfig) {
  let modifiers = getAllModifiers(modifierConfig);
  modifierConfig['+'] = { type: 'modifier-list', content: modifiers };
  modifierConfig['_default'] = {
    type: 'default', content: getDefaultContent(modifierConfig, modifiers)
  };
  return modifierConfig;
}

export function deserializeQueryString(string, modifierConfig) {
  return sanitizeTokens(tokenize(string, modifierConfig));
}

export default Ember.Component.extend({
  layout: layout,
  classNames: ['tag-search-input'],
  cursorLocation: -1,
  // used to pass event to child components
  enterClicked: false,
  upClicked: false,
  downClicked: false,
  isPopupFocused: false, // when popup focused not triggering enter event when enter clicked

  modifierConfig: {},
  firstTimeFocus: true,
  showHelp: computed('inputValue', 'cursorLocation', 'firstTimeFocus', function() {
    return !get(this, 'inputValue') &&
      get(this, 'cursorLocation') !== -1 &&
      get(this, 'firstTimeFocus');
  }),

  tokenConfig: computed('modifierConfig', function() {
    return prepareConig(get(this, 'modifierConfig'));
  }),

  allTags: computed('modifierConfig', function() {
    return Object.keys(get(this, 'modifierConfig')).filter((value) => value !== '_default');
  }),

  firstThreeTags: computed('allTags.[]', function() {
    return get(this, 'allTags').slice(0,3);
  }),

  tagOne: computed('allTags.[]', function() { return get(this, 'allTags').objectAt(0); }),
  tagTwo: computed('allTags.[]', function() { return get(this, 'allTags').objectAt(1); }),
  tagThree: computed('allTags.[]', function() { return get(this, 'allTags').objectAt(2); }),

  hasOnlyOneTag: computed.equal('allTags.length', 1),
  hasOnlyTwoTags: computed.equal('allTags.length', 2),
  hasMoreThanThreeTags: computed.gt('allTags.length', 3),

  hasMoreThanThreeTags: computed('allTags.[]', function() {
    return get(this, 'allTags.length') > 3;
  }),

  tokenTypes: ['default', 'modifier-list', 'space'],
  proxyValue: '',
  inputValue: computed({
    set(key, val) {
      set(this, 'proxyValue', val);
      return val;
    },
    get() {
      return '';
    }
  }),

  popupComponent: computed('currentToken', 'currentToken.type', function() {
    let type = get(this, 'currentToken.type');
    set(this, 'isPopupFocused', false);
    if (type && (type !== 'space')) {
      return 'tag-search-input.hint-popup.' + (type === 'modifier-list' ? 'list' : type);
    }
  }),

  onInitComponent: on('didInsertElement', function() {
    run.schedule('afterRender', this, function() {
      this._mainInput = this.$('.main-input');
      this._background = this.$('.background-container');
      this._mouseWheelListener = run.bind(this, 'onMouseMove');
      this._mainInput.on("mousewheel DOMMouseScroll", this._mouseWheelListener); // maybe to do with custom events ?
    });
  }),

  teadDown: on('willRemoveElement', function() {<commit_msg>removed duplicate definition<commit_after>import Ember from 'ember';
import Token from './token';
import layout from './template';
import { setCursor, KEYS } from './util';

const { set, on, run, getProperties, computed, get } = Ember;

function sanitizeTokens(tokens) {
  return tokens.reduce(function(sum, token) {
    let t = sanitizeToken(token);
    if (t) {
      if (sum[t.modifier]) {
        sum[t.modifier].push(t);
      } else {
        sum[t.modifier] = [t];
      }
    }
    return sum;
  }, Object.create(null));
}

function sanitizeToken(token) {
  let type = get(token, 'type');
  if (type !== 'space' && type !== 'default') {
    return getProperties(token, 'model', 'fullText', 'modifier', 'value');
  }
  return null;
}

function getDefaultContent(modifierConfig, modifiersList) {
  let key, val, list;
  let allList = [];

  for (key in modifierConfig) {
    val = modifierConfig[key];
    if (val.type === 'list' && val.content) {
      list = val.content.map(function(item) {
        return {
          label: item.label,
          value: key + item.value,
          fullText: true,
          section: val.sectionTitle
        };
      });
      allList = allList.concat(list);
    }
  }
  var modifiers = modifiersList.map(function(item) {
    item.section = 'modifiers';
    return item;
  });
  return allList.concat(modifiers);
}

function getAllModifiers(modifierConfig) {
  let modifiers = [];
  for (let key in modifierConfig) {
    let config = modifierConfig[key];
    let section = config.type === 'date' ? 'time' : 'others';
    modifiers.push({
      value: key,
      label: config.defaultHint,
      modifier: true,
      section
    });
  }
  return modifiers;
}

function tokenize(text, modifierConfig) {
  let tokens = [];
  let mem = '';
  let i = 0;

  for (i = 0; i <= text.length; i++) {
    var character = text[i];
    if ((character === ' ' || !character)) {
      if (mem) {
        tokens.push(Token.create({ modifierConfig, fullText: mem }));
      }
      if (character) {
        tokens.push(Token.create({ modifierConfig, fullText: ' ' }));
      }
      mem = '';
    } else if (character !== ' ') {
      mem += character;
    }
  }
  return tokens;
}

function prepareConig(modifierConfig) {
  let modifiers = getAllModifiers(modifierConfig);
  modifierConfig['+'] = { type: 'modifier-list', content: modifiers };
  modifierConfig['_default'] = {
    type: 'default', content: getDefaultContent(modifierConfig, modifiers)
  };
  return modifierConfig;
}

export function deserializeQueryString(string, modifierConfig) {
  return sanitizeTokens(tokenize(string, modifierConfig));
}

export default Ember.Component.extend({
  layout: layout,
  classNames: ['tag-search-input'],
  cursorLocation: -1,
  // used to pass event to child components
  enterClicked: false,
  upClicked: false,
  downClicked: false,
  isPopupFocused: false, // when popup focused not triggering enter event when enter clicked

  modifierConfig: {},
  firstTimeFocus: true,
  showHelp: computed('inputValue', 'cursorLocation', 'firstTimeFocus', function() {
    return !get(this, 'inputValue') &&
      get(this, 'cursorLocation') !== -1 &&
      get(this, 'firstTimeFocus');
  }),

  tokenConfig: computed('modifierConfig', function() {
    return prepareConig(get(this, 'modifierConfig'));
  }),

  allTags: computed('modifierConfig', function() {
    return Object.keys(get(this, 'modifierConfig')).filter((value) => value !== '_default');
  }),

  firstThreeTags: computed('allTags.[]', function() {
    return get(this, 'allTags').slice(0,3);
  }),

  tagOne: computed('allTags.[]', function() { return get(this, 'allTags').objectAt(0); }),
  tagTwo: computed('allTags.[]', function() { return get(this, 'allTags').objectAt(1); }),
  tagThree: computed('allTags.[]', function() { return get(this, 'allTags').objectAt(2); }),

  hasOnlyOneTag: computed.equal('allTags.length', 1),
  hasOnlyTwoTags: computed.equal('allTags.length', 2),
  hasMoreThanThreeTags: computed.gt('allTags.length', 3),

  tokenTypes: ['default', 'modifier-list', 'space'],
  proxyValue: '',
  inputValue: computed({
    set(key, val) {
      set(this, 'proxyValue', val);
      return val;
    },
    get() {
      return '';
    }
  }),

  popupComponent: computed('currentToken', 'currentToken.type', function() {
    let type = get(this, 'currentToken.type');
    set(this, 'isPopupFocused', false);
    if (type && (type !== 'space')) {
      return 'tag-search-input.hint-popup.' + (type === 'modifier-list' ? 'list' : type);
    }
  }),

  onInitComponent: on('didInsertElement', function() {
    run.schedule('afterRender', this, function() {
      this._mainInput = this.$('.main-input');
      this._background = this.$('.background-container');
      this._mouseWheelListener = run.bind(this, 'onMouseMove');
      this._mainInput.on("mousewheel DOMMouseScroll", this._mouseWheelListener); // maybe to do with custom events ?
    });
  }),

  teadDown: on('willRemoveElement', function() {
{'fraction_non_alphanumeric': 0.16534810126582278, 'fraction_numerical': 0.0024723101265822786, 'mean_word_length': 3.4866903283052353, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '4255273', 'n_tokens_mistral': 3202, 'n_tokens_neox': 3160, 'n_words': 725}