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 { isArrayOfType, isSelectSourceItemType } from '../../../../types/type-guard';
/**
 * The `VegaInputSelectSourceController` class manages the source data for
 * a dropdown input component in TypeScript.
 */
export class VegaInputSelectSourceController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * convert the dropdown source when the component watchSource call
     */
    this.handleSourceChange = () => {
      this.host.dataset.source = JSON.stringify(this.source);
      this.source = isArrayOfType(this.source, isSelectSourceItemType) ? this.source : [];
      this.vegaInputSelectItemController.setSelectedItem();
    };
  }
  /**
   * Append items to source
   *
   * @param {SelectSourceItem[]} items - Appendable items
   */
  appendSourceItems(items) {
    this.source = [...this.source, ...items];
  }
  /**
   * This TypeScript function retrieves an item from a source array based on its ID.
   *
   * @param {SelectValueType<SelectType>} id - The `id` parameter in the `getItem` function is of type `SelectValueType<SelectType>`,
   * which means it can accept a value that corresponds to the `SelectType` defined in the function.
   * @returns { Nullable<SelectSourceItem>} The `getItem` function is returning a `SelectSourceItem` object that matches the provided
   * `id`, or `null` if no matching item is found in the `source` array.
   */
  getItem(id) {
    return this.source.find((item) => item.id === id);
  }
}
__decorate([
  MapToComponentField()
], VegaInputSelectSourceController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputSelectSourceController.prototype, "source", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectSourceController.prototype, "vegaInputSelectItemController", void 0);
__decorate([
  MapToComponentMethod('watchSource')
], VegaInputSelectSourceController.prototype, "handleSourceChange", void 0);
