<commit_before>import _ from 'lodash';

import DebouncedCall from './DebouncedCall';


export default class Field {
  constructor(form, fieldName) {
    // TODO: protected props rename to __prop
    this.$form = form;
    this.$pathToField = fieldName;
    this.$onChangeCallback = null;
    this.__storage = this.$form.$storage;
    this.__onSaveCallback = null;
    this.__debouncedCall = new DebouncedCall(this.$form.$config.debounceTime);

    this._debouncedCb = undefined;
    this._validateCb = undefined;

    // init state
    // TODO: !!!! this.$pathToField и fieldName = одно и то же
    this.__storage.initFieldState(this.$pathToField, fieldName);
  }

  get form() {
    return this.$form;
  }
  // TODO: ?? может тоже переименовать в inputValue
  get userInput() {
    return this.__storage.getUserInput(this.$pathToField);
  }
  // TODO: переименовать в fixedValue / savedValue / prevStateValue
  get savedValue() {
    return this.__storage.getSavedValue(this.$pathToField);
  }
  // combined value
  get value() {
    return this.__storage.getValue(this.$pathToField);
  }
  get name() {
    return this.__storage.getFieldState(this.$pathToField, 'name');
  }
  get dirty() {
    return this.__storage.getFieldState(this.$pathToField, 'dirty');
  }
  get touched() {
    return this.__storage.getFieldState(this.$pathToField, 'touched');
  }
  get valid() {
    return this.__storage.getFieldState(this.$pathToField, 'valid');
  }
  get invalidMsg() {
    return this.__storage.getFieldState(this.$pathToField, 'invalidMsg');
  }
  get saving() {
    return this.__storage.getFieldState(this.$pathToField, 'saving');
  }
  get focused() {
    return this.__storage.getFieldState(this.$pathToField, 'focused');
  }
  get disabled() {
    return this.__storage.getFieldState(this.$pathToField, 'disabled');
  }
  get validateCb() {
    return this._validateCb;
  }
  get debounceTime() {
    return this.__debouncedCall.delay;
  }

  // set outer value with clearing user input
  setValue(newSavedValue) {
    // TODO: может переименовать в setFixedValue?
    // TODO: !!!! WTF??!!
    this._hardlySetSavedValue(newSavedValue);
  }
  setDisabled(value) {
    this.__storage.setFieldState(this.$pathToField, { disabled: value });
  }
  setValidateCb(value) {
    this._validateCb = value;
  }
  setDebounceTime(delay) {
    this.__debouncedCall.delay = delay;
  }


  // TODO: WTF??? наверное это setFixedValueSilent ???
  $setSavedValue(newValue) {
    this.__storage.setSavedValue(this.$pathToField, newValue);
  }

  /**
   * Recalculate dirty state.
   */
  $recalcDirty() {
    let newDirtyValue;

    if (this.userInput === '' && (this.savedValue === '' || _.isNil(this.savedValue))) {
      // 0 compares as common value.
      newDirtyValue = false;
    }
    else {
      // just compare initial value and value
      newDirtyValue = this.userInput !== this.savedValue;
    }

    this.$form.$handlers.handleFieldDirtyChange(this.$pathToField, newDirtyValue);
  }

  /**
   * Start saving field and form in they have a save handlers.
   * It will reset saving in progress before start saving.
   * @param {boolean} force
   *   * if true it will save immediately.
   *   * if false it will save with dobounce delay
   * @private
   */
  __startSave(force) {<commit_msg>refactor<commit_after>import _ from 'lodash';

import DebouncedCall from './DebouncedCall';


export default class Field {
  constructor(form, fieldName) {
    // TODO: protected props rename to _prop
    this.$form = form;
    this.$pathToField = fieldName;
    this.$onChangeCallback = null;
    this.__storage = this.$form.$storage;
    this.__onSaveCallback = null;
    this.__debouncedCall = new DebouncedCall(this.$form.$config.debounceTime);

    this._debouncedCb = undefined;
    this._validateCb = undefined;

    // init state
    // TODO: !!!! this.$pathToField и fieldName = одно и то же
    this.__storage.initFieldState(this.$pathToField, fieldName);
  }

  get form() {
    return this.$form;
  }
  // TODO: ?? remove
  get userInput() {
    return this.__storage.getUserInput(this.$pathToField);
  }
  get savedValue() {
    return this.__storage.getSavedValue(this.$pathToField);
  }
  // combined value
  get value() {
    return this.__storage.getValue(this.$pathToField);
  }
  get name() {
    return this.__storage.getFieldState(this.$pathToField, 'name');
  }
  get dirty() {
    return this.__storage.getFieldState(this.$pathToField, 'dirty');
  }
  get touched() {
    return this.__storage.getFieldState(this.$pathToField, 'touched');
  }
  get valid() {
    return this.__storage.getFieldState(this.$pathToField, 'valid');
  }
  get invalidMsg() {
    return this.__storage.getFieldState(this.$pathToField, 'invalidMsg');
  }
  get saving() {
    return this.__storage.getFieldState(this.$pathToField, 'saving');
  }
  get focused() {
    return this.__storage.getFieldState(this.$pathToField, 'focused');
  }
  get disabled() {
    return this.__storage.getFieldState(this.$pathToField, 'disabled');
  }
  get validateCb() {
    return this._validateCb;
  }
  get debounceTime() {
    return this.__debouncedCall.delay;
  }

  // set outer value with clearing user input
  setValue(newSavedValue) {
    // TODO: для программной установки (silent)
    // TODO: !!!! WTF??!!
    this._hardlySetSavedValue(newSavedValue);
  }
  setSavedValue() {
    // TODO: !!!
  }

  $setDefaultValue() {
    // TODO: !!!
  }

  setDisabled(value) {
    this.__storage.setFieldState(this.$pathToField, { disabled: value });
  }
  setValidateCb(value) {
    this._validateCb = value;
  }
  setDebounceTime(delay) {
    this.__debouncedCall.delay = delay;
  }


  // TODO: WTF??? наверное это setFixedValueSilent ???
  $setSavedValue(newValue) {
    this.__storage.setSavedValue(this.$pathToField, newValue);
  }

  /**
   * Recalculate dirty state.
   */
  $recalcDirty() {
    let newDirtyValue;

    if (this.value === '' && (this.savedValue === '' || _.isNil(this.savedValue))) {
      // 0 compares as common value.
      newDirtyValue = false;
    }
    else {
      // just compare initial value and value
      newDirtyValue = this.value !== this.savedValue;
    }

    this.$form.$handlers.handleFieldDirtyChange(this.$pathToField, newDirtyValue);
  }

  /**
   * Start saving field and form in they have a save handlers.
   * It will reset saving in progress before start saving.
   * @param {boolean} force
   *   * if true it will save immediately.
   *   * if false it will save with dobounce delay
   * @private
   */
  __startSave(force) {
{'fraction_non_alphanumeric': 0.13102501525320318, 'fraction_numerical': 0.0003050640634533252, 'mean_word_length': 3.246761658031088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2167389', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2101, 'n_words': 450}