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 { LogUtility } from '../../../../utils/log';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import { FORM_ELEMENT_VALUE_CHANGE } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
/** The `VegaInputSelectValueController` class is a TypeScript React component that handles the value and change events for an input select component. */
export class VegaInputSelectValueController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * handle the value change when the component watchValue call
     *
     * @param {SelectValueType<SelectType>} newValue the new value
     * @param {SelectValueType<SelectType>} oldValue the old value
     */
    this.handleValueChange = (newValue, oldValue) => {
      if (this.selectType === 'multiple') {
        newValue = newValue;
        oldValue = oldValue;
        let validValue;
        if (newValue) {
          validValue = !this.vegaDropdownProps.preserveValueIfNotInSource
            ? newValue.filter((value) => this.sourceController.getItem(value))
            : newValue;
        }
        else {
          validValue = [];
        }
        if (!isArrayEqual(validValue, oldValue, true)) {
          this.setValue(validValue);
          this.changeEventEmitter.emit(validValue);
          ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
            host: this.host,
            detail: validValue,
          });
        }
        else if (!isArrayEqual(validValue, newValue, true)) {
          this.setValue(validValue);
        }
      }
      else {
        if (newValue !== oldValue) {
          const sourceItem = this.sourceController.getItem(newValue);
          if (!this.vegaDropdownProps.preserveValueIfNotInSource) {
            this.setValue(sourceItem ? sourceItem.id : '');
          }
          else {
            this.setValue(newValue);
          }
          this.changeEventEmitter.emit(this.value);
          ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
            host: this.host,
            detail: this.value,
          });
        }
      }
    };
  }
  /**
   * The function sets the value of a select input if it passes a value guard check.
   *
   * @param {unknown} value - The `value` parameter is the value that you want to set for the `value` property. It
   * can be of any type that extends the `SelectType` type.
   */
  setValue(value) {
    if (this.valueGuard(value)) {
      this.value = value;
      this.vegaInputSelectItemController.setSelectedItem();
    }
  }
  /**
   * The function `valueGuard` checks if the input value is valid based on the select type.
   *
   * @param {SelectValueType<SelectType>} value - The `value` parameter is of type `SelectValueType<SelectType>`.
   * @returns {boolean} a boolean value.
   */
  valueGuard(value) {
    switch (this.selectType) {
      case 'single':
        return value === null || typeof value === 'string';
      case 'multiple':
        return Array.isArray(value) && value.every((item) => typeof item === 'string');
      default:
        LogUtility.warn(`Invalid input select value ${value} for ${this.selectType} mode`);
        return false;
    }
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaInputSelectValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectValueController.prototype, "selectType", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectValueController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectValueController.prototype, "sourceController", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectValueController.prototype, "vegaInputSelectItemController", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectValueController.prototype, "vegaDropdownProps", void 0);
__decorate([
  MapToComponentMethod('watchValue')
], VegaInputSelectValueController.prototype, "handleValueChange", void 0);
