import { LisioParameterNames } from "@lisio/lisio-profils";
import CSSProperties from "../enums/lisio-css-properties";
import { kebabToCamel } from "../utils";
import Walker from "../walkers/lisio-walker";

/**
 * Abstract class representing an adapter.\
 * It aims to represent a functionality of Lisio.\
 * An adapter is basically a functionality of Lisio which will adapt the main page.\
 * To execute his responsability this class provides :
 *  * Methods to adapt the main page
 */
abstract class LisioAdapter<T extends string | boolean | number> {
  /**
   * Abstract method to adapt the main page
   * @param {Walker} walker - A walker to explore the DOM
   * @param {T} value - Value of the functionality
   * @param {LisioParameterNames | undefined} feature - feature that has to be applied by the adapter if not the default one
   * @param {boolean | number | string | undefined} defaultValue - default value of the input
   * @returns Returns nothing
   */
  public abstract adapt(walker: Walker, value: T, feature?: LisioParameterNames, defaultValue?: boolean | number | string): void;

  /**
   * Abstract method
   * @param {HTMLElement} element -
   * @param {T} value - Value of the functionality
   * @returns Returns nothing
   */
  protected abstract adaptFunction(element: HTMLElement, value: T): void;

  protected findOldSelector(elementToCheck: HTMLElement, style: string) {
    const reg = new RegExp(`lisio-${style}-\\w+`, "g");
    const regExecResults: RegExpExecArray | null = reg.exec(
      elementToCheck.className,
    );
    const oldSelector: string =
      regExecResults == undefined ? "" : regExecResults[0];
    return oldSelector;
  }

  /**
   * Abstract method
   * @param {HTMLElement} element -
   * @param {Map<CSSProperties, string>} properties -
   * @returns Returns nothing
   */
  protected saveInAttributes(
    element: HTMLElement,
    properties: Map<CSSProperties, string>,
  ): void {
    for (const [property, value] of properties.entries()) {
      element.dataset[kebabToCamel(`lisio-${property}`)] = value;
    }
  }
}

export default LisioAdapter;
