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 input container has a fixed suffix that adapts based on the container size.
 * When the input container is large enough to fully display the input content, the suffix appears immediately after the input text.
 *    this is achieved by setting the input to not grow flexibly while allowing the suffix to grow.
 * If the container is too small to display all the input content, the suffix moves to the end of the container,
 *    with the input width controlled by the vega-input component.
 */
export class VegaInputCompactWidthController extends VegaSlimmer {
  /**
   * Component lifecycle - [componentDidLoad]
   */
  componentDidLoad() {
    this.setInputAutoWidth();
  }
  /**
   * While value changed, we should calculate input actual width
   */
  watchValue() {
    this.setInputAutoWidth();
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-input-suffix-text-change-e2e}
   */
  /* istanbul ignore next */
  /**
   *  While suffixText changed, we should calculate input actual width
   */
  watchSuffixText() {
    this.setInputAutoWidth();
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-input-compact-input-width-e2e}
   */
  /* istanbul ignore next */
  /**
   * Adjusts the width of the input based on its content and container size.
   *    while actual width of value larger than input width, we use native vega-input to controller the stretch
   *    otherwise, we should make the input container not flex grow,and the suffix flex grow.
   */
  setInputAutoWidth() {
    const vegaInputContainer = this.host.shadowRoot.querySelector('.vega-input-container');
    const inputBox = vegaInputContainer === null || vegaInputContainer === void 0 ? void 0 : vegaInputContainer.querySelector('.vega-input-input');
    const input = inputBox === null || inputBox === void 0 ? void 0 : inputBox.querySelector('input');
    if (!input)
      return;
    vegaInputContainer.classList.remove('auto-width');
    input.style.removeProperty('width');
    if (this.isCompactInputWidth && this.suffixText) {
      const actualWidth = this.getActualInputWidth();
      if (inputBox.getBoundingClientRect().width > actualWidth) {
        input.style.width = `${actualWidth}px`;
        vegaInputContainer.classList.add('auto-width');
      }
    }
  }
  /**
   * Calculates the actual width of the text content.
   *
   * @returns {number} - The actual width of the text content.
   */
  getActualInputWidth() {
    const container = this.getCalculateTextWidthContainer();
    container.style.display = 'inline-block';
    container.textContent = `${this.value || this.placeholder || ''}`;
    const actualWidth = container.getBoundingClientRect().width;
    container.style.display = 'none';
    return actualWidth;
  }
  /**
   * Creates and returns a hidden container element for calculating text width.
   *
   * @returns {HTMLElement} - calculating text width container.
   */
  getCalculateTextWidthContainer() {
    if (!this.calculateTextWidthContainer) {
      // eslint-disable-next-line no-restricted-globals
      const container = document.createElement('div');
      container.classList.add('v-font-field-value');
      container.style.visibility = 'hidden';
      container.style.display = 'none';
      this.host.shadowRoot.append(container);
      this.calculateTextWidthContainer = container;
    }
    return this.calculateTextWidthContainer;
  }
}
__decorate([
  MapToComponentField()
], VegaInputCompactWidthController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaInputCompactWidthController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputCompactWidthController.prototype, "placeholder", void 0);
__decorate([
  MapToComponentField()
], VegaInputCompactWidthController.prototype, "isCompactInputWidth", void 0);
__decorate([
  MapToComponentField()
], VegaInputCompactWidthController.prototype, "suffixText", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaInputCompactWidthController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('watchValue')
], VegaInputCompactWidthController.prototype, "watchValue", null);
__decorate([
  MapToComponentMethod('watchSuffixText')
], VegaInputCompactWidthController.prototype, "watchSuffixText", null);
