import { defautValueOfNumericParameters, LisioNumericParameterNames } from "@lisio/lisio-profils";
import LisioCSSProperties from "../../enums/lisio-css-properties";
import LisioTextTreeWalker from "../../walkers/lisio-text-tree-walker";
import LisioFontStyleAdapter from "./lisio-font-style-adapter";

/**
 * Class representing a font-size adapter extending an adapter.\
 * It aims to represents the font style functionality of Lisio.\
 * A font-size adapter is basically a functionality of Lisio which will change the font-size of texts in the main page.\
 */
class LisioWordSpacingAdapter extends LisioFontStyleAdapter {
  constructor() {
    super(LisioCSSProperties.WORD_SPACING);
  }

  /**
   * Public method implementing abstract method adapt of Adapter
   * @param {LisioTextTreeWalker} walker - A walker to explore the DOM
   * @param {number} value - Value of the functionality
   * @returns Returns nothing
   * @source
   */
  public adapt(walker: LisioTextTreeWalker, value: number): void {
    for (const tag of walker.tags.values()) {
      if (tag.localName != "option") {
        this.adaptFunction(tag, value);
      }
    }

    // const formTags = document.body.querySelectorAll('input, textarea, i'); // on récupère tout ce qui ne compte pas comme des noeuds textes mais qui peuvent contenir du texte
    const formTags: NodeListOf<HTMLElement> =
      document.body.querySelectorAll("input, textarea, i");
    for (const formTag of formTags) {
      this.adaptFunction(formTag, value);
    }
  }

  protected adaptFunction(element: HTMLElement, value: number): void {
    if(value === defautValueOfNumericParameters.get(LisioNumericParameterNames.WORD_SPACING)){
      if(element.dataset.lisioWordSpacing == undefined){
        return
      }
    }
    const adaptedValue: number = this.fontStyleCalculator(LisioNumericParameterNames.WORD_SPACING, element, value);
    this.fontStyleCalculatorApply(element, adaptedValue);
  }
}

export default LisioWordSpacingAdapter;
