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 { FORM_ELEMENT_VALUE_CHANGE } from '../../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import ChangeManager from '../../../../../helpers/change-manager/change-manager';
/**
 * The `VegaSelectionTileGroupValueController` class in TypeScript manages the value selection for
 * a group of checkboxes or radio buttons.
 */
export class VegaSelectionTileGroupValueController extends VegaSlimmer {
  /**
   * The `watchValue` function emits a change event with the current value and notifies the
   * `ChangeManager` about a form element value change.
   */
  watchValue() {
    this.changeEventEmitter.emit(this.value);
    ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
      host: this.host,
      detail: this.value,
    });
  }
  /**
   * The function `onSelectionTilesChanged` determines whether to set the value from checkbox items or
   * radio items based on the selected type.
   */
  onSelectionTilesChanged() {
    this.selectType === 'checkbox' ? this.setValueFromCheckboxItems() : this.setValueFromRadioItems();
  }
  /**
   * The function `onSelectionTileChecked` checks the type of input and calls the appropriate function to set the value
   * based on the type (checkbox or radio).
   *
   * @param {VegaSelectionTileCheckedPayload} payload - The `value` parameter in the `onSelectionTileChecked` function is a string that represents
   * the value of the checkbox or radio button that was checked.
   */
  onSelectionTileChecked(payload) {
    this.selectType === 'checkbox' ? this.setValueFromCheckbox(payload) : this.setValueFromRadio(payload);
  }
  /**
   * The function `setValueFromCheckbox` updates the value array by adding or removing a specified value.
   *
   * @param {VegaSelectionTileCheckedPayload} payload - The `value` parameter in the `setValueFromCheckbox` function is a string
   * representing the value of a checkbox.
   */
  setValueFromCheckbox(payload) {
    if (payload.checked && !this.value.includes(payload.value)) {
      this.value = [...this.value, payload.value];
    }
    else if (!payload.checked && this.value.includes(payload.value)) {
      this.value = [...this.value.filter((value) => value !== payload.value)];
    }
  }
  /**
   * The function `setValueFromRadio` sets the value of a radio button in TypeScript.
   *
   * @param {VegaSelectionTileCheckedPayload} payload - The `value` parameter is a string that represents the value to be set for
   * the radio button.
   */
  setValueFromRadio(payload) {
    if (payload.checked) {
      this.value = payload.value;
    }
  }
  /**
   * The function `setValueFromCheckboxItems` sets the value of an element based on the checked items in
   * a selection tile array.
   */
  setValueFromCheckboxItems() {
    if (this.value.length === 0) {
      this.value = this.itemsController
        .getSelectionTileArray()
        .filter((tile) => tile.checked)
        .map((tile) => tile.value);
    }
  }
  /**
   * The function `setValueFromRadioItems` sets the value of a radio item based on the checked status of
   * the items.
   */
  setValueFromRadioItems() {
    if (!this.value) {
      const checkedItem = this.itemsController
        .getSelectionTileArray()
        .find((tile) => tile.checked);
      if (checkedItem) {
        this.value = checkedItem.value;
      }
    }
  }
}
__decorate([
  MapToComponentField()
], VegaSelectionTileGroupValueController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaSelectionTileGroupValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileGroupValueController.prototype, "selectType", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileGroupValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileGroupValueController.prototype, "itemsController", void 0);
__decorate([
  MapToComponentMethod('watchValue')
], VegaSelectionTileGroupValueController.prototype, "watchValue", null);
