/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { removeAllChildren, removeElement, matchesSelectors } from '@tylertech/forge-core';
import { BaseAdapter } from '../core/base/base-adapter';
import { attachTooltip } from './tooltip-utils';
/**
 * The DOM adapter for the tooltip component.
 */
export class TooltipAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._tooltipElement = null;
    }
    initializeTargetElement(selector) {
        this._targetElement = this._getTargetElement(selector);
    }
    initializeAccessibility(identifier) {
        if (this._targetElement && !this._targetElement.hasAttribute('aria-describedby')) {
            this._targetElement.setAttribute('aria-describedby', identifier);
        }
    }
    hasTargetElement() {
        return !!this._targetElement;
    }
    hasTooltipElement() {
        return !!this._tooltipElement;
    }
    isTargetElementConnected() {
        return !!this._targetElement && this._targetElement.isConnected;
    }
    destroy(identifier) {
        if (this._targetElement && this._targetElement.getAttribute('aria-describedby') === identifier) {
            this._targetElement.removeAttribute('aria-describedby');
        }
    }
    /**
     * Sets the text content of the host element to the provided text.
     * @param text The text content.
     */
    setTextContent(text) {
        removeAllChildren(this._component);
        if (text) {
            this._component.appendChild(document.createTextNode(text));
        }
    }
    /**
     * Adds an event listener to the target element.
     * @param targetElement The target element instance.
     * @param type The event type.
     * @param listener The event listener.
     */
    addTargetEventListener(type, listener) {
        if (this._targetElement) {
            this._targetElement.addEventListener(type, listener);
        }
    }
    /**
     * Removes an event listener from the target element.
     * @param type The event type.
     * @param listener The event listener.
     */
    removeTargetEventListener(type, listener) {
        if (this._targetElement) {
            this._targetElement.removeEventListener(type, listener);
        }
    }
    /**
     * Displays the tooltip around the target element based on the provided configuration.
     * @param position The position.
     * @param content The tooltip content.
     */
    showTooltip(position, content) {
        if (!this._targetElement) {
            return;
        }
        if (!content) {
            const child = this._getTooltipContent();
            content = child.cloneNode(true);
        }
        const isEmptyTextNode = content.nodeType === 3 && (!content.textContent || content.textContent.trim().length === 0);
        const isEmptyNode = !content || isEmptyTextNode;
        if (isEmptyNode) {
            return;
        }
        this._tooltipElement = attachTooltip(this._targetElement, position, content);
    }
    getInnerText() {
        return this._component.innerText;
    }
    /**
     * Removes the tooltip from the DOM.
     * @param tooltipElement The target element instance.
     */
    hideTooltip() {
        if (this._tooltipElement) {
            removeElement(this._tooltipElement);
            this._tooltipElement = null;
        }
    }
    getTooltipElement() {
        return this._tooltipElement;
    }
    /**
     * Gets the target element based on the provided CSS selector.
     * @param {string | undefined} selector The target element selector.
     */
    _getTargetElement(selector) {
        if (selector) {
            if (this._component.parentElement) {
                if (matchesSelectors(this._component.parentElement, selector)) {
                    return this._component.parentElement;
                }
                return this._component.parentElement.querySelector(selector);
            }
        }
        else {
            return (this._component.previousElementSibling || this._component.parentElement);
        }
        return null;
    }
    _getTooltipContent() {
        return this._component.firstElementChild || this._component.firstChild || document.createTextNode('');
    }
}
