/**
 * @mergeTarget
 * @module Src/Components/AuthInput
 */

import { LisioComponent } from "@lisio/lisio-engine";

class AuthInput extends LisioComponent {
  /**
   * Private attribute to store input name
   * @source
   */
  private _name: string;

  /**
   * Private attribute to store input type
   * @source
   */
  private _type: string;

  /**
   * Private attribute to store input placeholder
   * @source
   */
  private _placeholder: string;

  /**
   * Private attribute to store input autocomplete value
   * @source
   */
  private _autocomplete: string;

  /**
   * Private attribute to store input value before render
   * @source
   */
  private _value: string = "";

  /**
   * Getter for attribute {@link _value | value}
   * @returns Returns current input value
   * @source
   */
  public get value(): string {
    if (this._htmlElement != undefined) {
      return (this._htmlElement as HTMLInputElement).value;
    }
    return this._value;
  }

  /**
   * Setter for attribute {@link _value | value}
   * @param {string} value - New input value
   * @source
   */
  public set value(value: string) {
    this._value = value;
    if (this._htmlElement != undefined) {
      (this._htmlElement as HTMLInputElement).value = value;
    }
  }

  /**
   * Public method to change input type
   * @param {string} type - New input type
   * @returns Returns nothing
   * @source
   */
  public changeType(type: string): void {
    this._type = type;
    this._htmlElement?.setAttribute("type", type);
  }

  /**
   * Public method to focus input
   * @returns Returns nothing
   * @source
   */
  public focus(): void {
    (this._htmlElement as HTMLInputElement | undefined)?.focus();
  }

  /**
   * @async
   * Public method implementing abstract method render of LisioComponent
   * @returns Returns a promise containing the representation of an auth input in HTML string
   * @source
   */
  public async render(): Promise<string> {
    return `
      <input
        id="${this._id}"
        name="${this._name}"
        type="${this._type}"
        class="lisio-custom-input ${this.renderClasses()}"
        value="${this._value}"
        placeholder="${this._placeholder}"
        autocomplete="${this._autocomplete}"
        ${this.renderAttributes()}
      />
    `;
  }

  /**
   * Constructor of class {@link AuthInput | AuthInput}
   * @param {string} id - Id of input
   * @param {string} name - Name of input
   * @param {string} type - Type of input
   * @param {string} placeholder - Placeholder text
   * @param {string} autocomplete - Autocomplete mode
   * @param {string[] | undefined} cssClasses - CSS classes of component
   * @param {Map<string, string> | undefined} attributes - Attributes of component
   * @param {object[] | undefined} otherEventHandlers - Additional event handlers
   * @source
   */
  constructor(
    id: string,
    name: string,
    type: string,
    placeholder: string,
    autocomplete: string,
    cssClasses?: string[],
    attributes?: Map<string, string>,
    otherEventHandlers?: {
      triggerer: string;
      handler: (...args: any) => void;
    }[],
  ) {
    super(AuthInput.name, id, cssClasses, attributes);
    this._name = name;
    this._type = type;
    this._placeholder = placeholder;
    this._autocomplete = autocomplete;
    this._eventListeners.push({
      type: "input",
      handler: (event: unknown) => {
        if (event instanceof Event) {
          this._value = (event.target as HTMLInputElement).value;
        }
      },
    });
    if (otherEventHandlers) {
      otherEventHandlers.forEach((otherEvent) => {
        this._eventListeners.push({
          type: otherEvent.triggerer,
          handler: otherEvent.handler,
        });
      });
    }
  }
}

export { AuthInput };
