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 { MapToComponentMethod } from 'vega-slimmer/core';
import { MutationObserverSlimmer } from './mutation-observer-slimmer';
/**
 * Maps host data-tabindex to inner native input tabindex.
 */
export class DataTabIndexValueMapper extends MutationObserverSlimmer {
  /**
   * Constructor for DataTabIndexValueMapper
   *
   * @param {() => HTMLElement} targetGetter - Function to get the target HTMLElement
   */
  constructor(targetGetter) {
    super({
      options: { attributes: true, attributeFilter: [DataTabIndexValueMapper.OBSERVED_ATTRIBUTE] },
      // eslint-disable-next-line jsdoc/require-jsdoc
      callback: () => {
        this.performMapping();
      },
    });
    this.targetGetter = targetGetter;
  }
  /**
   * Component lifecycle - [componentDidLoad]
   */
  componentDidLoad() {
    this.performMapping();
    super.componentDidLoad && super.componentDidLoad();
  }
  /**
   * Component lifecycle - [componentDidRender]
   */
  componentDidRender() {
    this.performMapping();
  }
  /**
   * Performs the mapping of host data-tabindex to target element's tabindex.
   * If the host attribute value is null, undefined, or empty string, it removes the tabindex attribute from the target.
   * Otherwise, it sets the target's tabindex to the host attribute value.
   */
  performMapping() {
    const target = this.targetGetter ? this.targetGetter() : null;
    if (!target)
      return;
    const hostValue = this.host.getAttribute(DataTabIndexValueMapper.OBSERVED_ATTRIBUTE);
    if (this.shouldRemoveTabIndex(hostValue)) {
      target.removeAttribute('tabindex');
      return;
    }
    const tabIndexValue = String(hostValue);
    if (target.getAttribute('tabindex') !== tabIndexValue) {
      target.setAttribute('tabindex', tabIndexValue);
    }
  }
  /**
   * Determines whether the tabindex attribute should be removed from the target element.
   *
   * @param {string | null} hostValue - The value of the host element's data-tabindex attribute
   * @returns {boolean} True if the tabindex should be removed, false otherwise
   */
  shouldRemoveTabIndex(hostValue) {
    if (hostValue === null || hostValue === undefined) {
      return true;
    }
    const normalizedValue = String(hostValue).trim().toLowerCase();
    return normalizedValue === '' || normalizedValue === 'null' || normalizedValue === 'undefined';
  }
}
DataTabIndexValueMapper.OBSERVED_ATTRIBUTE = 'data-tabindex';
__decorate([
  MapToComponentMethod('componentDidLoad')
], DataTabIndexValueMapper.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('componentDidRender')
], DataTabIndexValueMapper.prototype, "componentDidRender", null);
