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 } from 'vega-slimmer/core';
import { ElementAppenderSlimmer } from '../../../../helpers/slimmers/element-appender/element-appender-slimmer';
import { ClickOutsideOnlyToDismissStrategy } from '../../../../helpers/slimmers/element-appender/trigger-strategies/click-outside-only-to-dismiss-strategy';
/** The `VegaTimePickerRangeDropdownController` class is responsible for managing the dropdown menu for selecting a range of time in the VegaTimePicker component. */
export class VegaTimePickerRangeDropdownController extends ElementAppenderSlimmer {
  /**
   * The constructor function sets up the configuration for a dropdown menu that is shown when a specific condition is met.
   *
   * @param {TimeRangeIndex} rangeIndex start or end
   */
  constructor(rangeIndex) {
    super(() => {
      const target = rangeIndex === 'start'
        ? this.rangeInputRenderer.getStartInputRef()
        : this.rangeInputRenderer.getEndInputRef();
      const appendableElement = rangeIndex === 'start'
        ? this.rangeDropdownRenderer.getStartDropdownRef()
        : this.rangeDropdownRenderer.getEndDropdownRef();
      return {
        target,
        appendableElement,
        positionRelativeTo: this.positionRelativeTo,
        translocation: { X: 0, Y: 8 },
        placement: ['bottom', 'top'],
        matchTargetWidth: true,
      };
    }, () => new ClickOutsideOnlyToDismissStrategy(), 'isRangeDropdownShow');
    this.isRangeDropdownShow = false;
    this.rangeDropdownType = 'start';
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-time-picker-range-input-focus}
     */
    /* istanbul ignore next */
    /**
     * The function shows a dropdown and scrolls to a selected element if a value is provided.
     *
     * @param {string} value - The `value` parameter is a string that represents the value to be scrolled to in the dropdown list.
     */
    this.showAndScrollPosition = (value) => {
      this.show();
      if (value) {
        const dropdownRef = this.rangeDropdownType === 'start'
          ? this.rangeDropdownRenderer.getStartDropdownRef()
          : this.rangeDropdownRenderer.getEndDropdownRef();
        const selectedItem = dropdownRef.shadowRoot.querySelector(`[aria-label="${value}"]`);
        if (selectedItem) {
          this.dropdownSelectedItemsController.scrollToSelectedElement(selectedItem);
        }
      }
    };
    this.rangeDropdownType = rangeIndex;
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-time-picker-range-disabled-change}
   */
  /* istanbul ignore next */
  /**
   * The function hides a dropdown when it is disabled.
   */
  hideDropdownWhenDisabled() {
    if (this.mode === 'range' && this.isRangeDropdownShow) {
      this.hide();
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTimePickerRangeDropdownController.prototype, "positionRelativeTo", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerRangeDropdownController.prototype, "mode", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerRangeDropdownController.prototype, "rangeDropdownRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerRangeDropdownController.prototype, "rangeInputRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerRangeDropdownController.prototype, "dropdownSelectedItemsController", void 0);
__decorate([
  MapToComponentMethod('watchDisabled')
], VegaTimePickerRangeDropdownController.prototype, "hideDropdownWhenDisabled", null);
