var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { isArrayEqual } from '../../../../utils/array';
import changeManager from '../../../../helpers/change-manager/change-manager';
import { FORM_ELEMENT_VALUE_CHANGE } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
/** The `VegaInputRangeValueController` class in TypeScript manages the value and events of an input range component. */
export class VegaInputRangeValueController extends VegaSlimmer {
  /**
   * This function emits an event and notifies after a value change if the new value is different from the old value.
   *
   * @param {InputRangeValueType} newValue - The `newValue` parameter represents the new value of an input range element.
   * @param {InputRangeValueType} oldValue - The `oldValue` parameter represents the previous value of an input range element before it was changed.
   */
  emitEventAndNotifyAfterValueChange(newValue, oldValue) {
    if (!isArrayEqual(newValue, oldValue)) {
      this.changeEventEmitter.emit(newValue);
      changeManager.notify(FORM_ELEMENT_VALUE_CHANGE, { host: this.host, detail: newValue });
    }
  }
  /**
   * The function `updateValueByRangeIndex` updates the value of an array based on the specified index.
   *
   * @param {string} itemValue - The `itemValue` parameter is a string representing the value that will be updated in the range input.
   * @param {RangeInputIndexType} index - The `index` parameter in the `updateValueByRangeIndex` function is used to determine which element in the array `this.value` should be updated.
   */
  updateValueByRangeIndex(itemValue, index) {
    let newValue = this.value;
    if (index === 0) {
      newValue = [
        itemValue,
        Array.isArray(this.value) && !isNaN(Number(this.value[1])) ? this.value[1] : '',
      ];
    }
    else {
      newValue = [
        Array.isArray(this.value) && !isNaN(Number(this.value[0])) ? this.value[0] : '',
        itemValue,
      ];
    }
    if (!isArrayEqual(newValue, this.value)) {
      this.setValue(newValue);
    }
  }
  /**
   * The function `setValue` sets the value of an input range component in TypeScript.
   *
   * @param {InputRangeValueType} value - The `value` parameter in the `setValue` method is of type `InputRangeValueType`.
   */
  setValue(value) {
    if (this.valueGuard(value)) {
      this.value = value;
    }
  }
  /**
   * The function `valueGuard` checks if the input value is an array with exactly two elements, where each element is either a string or a number.
   *
   * @param {InputRangeValueType} value - The `value` parameter is expected to be an array with a length
   * of 2, where each element in the array is either a string or a number. The `valueGuard` function
   * returns `true` if these conditions are met, and `false` otherwise.
   * @returns {boolean} The `valueGuard` function is returning a boolean value. It checks if the `value` parameter
   * is an array with a length of 2, and if every item in the array is either a string or a number. If
   * these conditions are met, the function returns `true`, otherwise it returns `false`.
   */
  valueGuard(value) {
    return Array.isArray(value) && value.length === 2;
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaInputRangeValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputRangeValueController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaInputRangeValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentMethod('watchValue')
], VegaInputRangeValueController.prototype, "emitEventAndNotifyAfterValueChange", null);
