/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { BaseSelectAdapter } from '../core/base-select-adapter';
export class SelectDropdownAdapter extends BaseSelectAdapter {
    constructor(component) {
        super(component);
    }
    initializeAccessibility() {
        this._targetElement.setAttribute('role', 'listbox');
        this._targetElement.setAttribute('aria-live', 'polite');
        this._targetElement.setAttribute('aria-haspopup', 'true');
        this._targetElement.setAttribute('aria-expanded', 'false');
    }
    addClickListener(listener) {
        this._targetElement.addEventListener('click', listener);
    }
    removeClickListener(listener) {
        if (this._targetElement) {
            this._targetElement.removeEventListener('click', listener);
        }
    }
    addTargetListener(type, listener) {
        let passive;
        let capture;
        if (type === 'keydown') {
            // We don't use a passive keydown listener because we are preventing default in this event and Angular doesn't like that
            // We need to use capturing to ensure that we get to this event before zone.js does
            passive = false;
            capture = true;
        }
        this._targetElement.addEventListener(type, listener, { passive, capture });
    }
    removeTargetListener(type, listener) {
        if (this._targetElement) {
            this._targetElement.removeEventListener(type, listener);
        }
    }
    updateActiveDescendant(id) {
        if (id) {
            this._targetElement.setAttribute('aria-activedescendant', id);
        }
        else {
            this._targetElement.removeAttribute('aria-activedescendant');
        }
    }
    open(config) {
        super.open(config);
        this._targetElement.setAttribute('aria-controls', `list-dropdown-popup-${config.id}`);
        this._targetElement.setAttribute('aria-expanded', 'true');
    }
    close() {
        this._targetElement.setAttribute('aria-expanded', 'false');
        this._targetElement.removeAttribute('aria-activedescendant');
        this._targetElement.removeAttribute('aria-controls');
        return super.close();
    }
    attach(selector) {
        const rootNode = this._component.getRootNode() || HTMLDocument;
        const doc = rootNode || this._component.ownerDocument || document;
        const element = doc.querySelector(selector);
        if (element) {
            this._targetElement = element;
        }
    }
    detach() {
        this._targetElement = undefined;
    }
    setTargetDisconnectedListener(cb) {
        if (!this._targetElement || !this._targetElement.parentElement) {
            return () => { };
        }
        const observer = new MutationObserver(mutations => {
            const isTargetRemoved = mutations.some(mutation => {
                return Array.from(mutation.removedNodes).some(node => node === this._targetElement);
            });
            if (isTargetRemoved) {
                observer.disconnect();
                cb();
            }
        });
        observer.observe(this._targetElement.parentElement, { childList: true });
        return () => observer.disconnect();
    }
    isAttached() {
        return !!this._targetElement;
    }
    setTargetText(text, selector) {
        let target = this._targetElement;
        if (selector) {
            const element = this._getElementBySelector(selector);
            if (element) {
                target = element;
            }
        }
        if (target) {
            target.textContent = text;
        }
    }
    getTargetText(selector) {
        const element = selector ? this._getElementBySelector(selector) : this._targetElement;
        return element ? element.innerText : '';
    }
    _getElementBySelector(selector) {
        return this._targetElement.querySelector(selector) || this._getRootNode().querySelector(selector);
    }
    _getRootNode() {
        return this._component.getRootNode() || this._component.ownerDocument || document;
    }
}
