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';
/** The `VegaInputSelectItemController` class is a TypeScript React component that controls the state of selected item. */
export class VegaInputSelectItemController extends VegaSlimmer {
  /**
   * Set initial the value of selected item
   */
  componentWillLoad() {
    this.setSelectedItem();
  }
  /**
   * Reset the value of selected item based on select type
   */
  watchSelectType() {
    if (this.selectType === 'multiple') {
      this.selectedItem = [];
      this.vegaInputSelectValueController.setValue([]);
    }
    else {
      this.selectedItem = null;
      this.vegaInputSelectValueController.setValue('');
    }
  }
  /**
   * Synchronizes the controller's selectedItem with the current value and the source data.
   */
  setSelectedItem() {
    var _a;
    if (this.selectType === 'multiple') {
      const updatedItem = this.value.map((itemValue) => this.sourceController.getItem(itemValue));
      const originalItem = this.selectedItem
        ? this.selectedItem.filter((item) => this.value.includes(item === null || item === void 0 ? void 0 : item.id))
        : [];
      this.selectedItem = this.updateSelectedItemUnderMultiple(originalItem, updatedItem);
    }
    else {
      const updatedItem = this.sourceController.getItem(this.value);
      if (!this.value || this.value !== ((_a = this.selectedItem) === null || _a === void 0 ? void 0 : _a.id)) {
        this.selectedItem = null;
      }
      if (updatedItem) {
        this.selectedItem = updatedItem;
      }
    }
  }
  /**
   * Updates the selected items array under multiple selection mode.
   *
   * @param {SelectSourceItem[]} originalItem - the original selected items array.
   * @param {SelectSourceItem[]} updatedItem - the updated selected items array.
   * @returns {SelectSourceItem[]} The updated selected items array.
   */
  updateSelectedItemUnderMultiple(originalItem, updatedItem) {
    const map = new Map(originalItem.map((obj) => [obj.id, obj]));
    // Iterate through array2 and update the Map
    updatedItem.forEach((item) => {
      // If id exists, it replaces the old object; if not, it adds it
      map.set(item === null || item === void 0 ? void 0 : item.id, item);
    });
    // Convert the Map values back into a standard array
    return Array.from(map.values());
  }
}
__decorate([
  MapToComponentField()
], VegaInputSelectItemController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectItemController.prototype, "selectType", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputSelectItemController.prototype, "selectedItem", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectItemController.prototype, "sourceController", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectItemController.prototype, "vegaInputSelectValueController", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaInputSelectItemController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('watchSelectType')
], VegaInputSelectItemController.prototype, "watchSelectType", null);
