/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { deepQuerySelectorAll, getActiveElement, getShadowElement, removeElement, toggleClass } from '@tylertech/forge-core';
import { BACKDROP_CONSTANTS } from '../backdrop';
import { CHECKBOX_CONSTANTS } from '../checkbox/checkbox-constants';
import { BaseAdapter } from '../core/base/base-adapter';
import { ICON_BUTTON_CONSTANTS } from '../icon-button/icon-button-constants';
import { RADIO_CONSTANTS } from '../radio/radio-constants';
import { SWITCH_CONSTANTS } from '../switch/switch-constants';
import { DIALOG_CONSTANTS } from './dialog-constants';
/**
 * Provides facilities for interacting with the internal DOM of `DialogComponent`.
 */
export class DialogAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._backdropElement = getShadowElement(component, DIALOG_CONSTANTS.selectors.BACKDROP);
        this._containerElement = getShadowElement(component, DIALOG_CONSTANTS.selectors.CONTAINER);
        this._surfaceElement = getShadowElement(component, DIALOG_CONSTANTS.selectors.SURFACE);
    }
    initializeAccessibility() {
        if (!this._component.hasAttribute('role')) {
            this._component.setAttribute('role', 'dialog');
        }
        this._component.setAttribute('aria-modal', 'true');
    }
    setAnimating(animating) {
        if (animating) {
            this._containerElement.classList.add(DIALOG_CONSTANTS.classes.ANIMATING);
        }
        else {
            this._containerElement.classList.remove(DIALOG_CONSTANTS.classes.ANIMATING);
        }
    }
    setVisibility(visible) {
        if (visible) {
            this._containerElement.classList.add(DIALOG_CONSTANTS.classes.OPEN);
        }
        else {
            this._containerElement.classList.remove(DIALOG_CONSTANTS.classes.OPEN);
        }
    }
    attach() {
        document.body.appendChild(this._component);
    }
    detach() {
        if (this._activeElement) {
            this._activeElement = undefined;
        }
        removeElement(this._component);
    }
    registerTransitionEndHandler(handler) {
        this._surfaceElement.addEventListener('transitionend', handler);
    }
    deregisterTransitionEndHandler(handler) {
        this._surfaceElement.removeEventListener('transitionend', handler);
    }
    setDocumentListener(type, listener) {
        document.addEventListener(type, listener);
    }
    removeDocumentListener(type, listener) {
        document.removeEventListener(type, listener);
    }
    registerBackdropClickHandler(handler) {
        this._backdropElement.addEventListener(BACKDROP_CONSTANTS.events.BACKDROP_CLICK, handler);
    }
    deregisterBackdropClickHandler(handler) {
        this._backdropElement.removeEventListener(BACKDROP_CONSTANTS.events.BACKDROP_CLICK, handler);
    }
    getOpenDialogs(selector) {
        return document.querySelectorAll(selector);
    }
    setBodyAttribute(name, value) {
        document.body.setAttribute(name, value);
    }
    removeBodyAttribute(name) {
        document.body.removeAttribute(name);
    }
    trySetInitialFocus() {
        const elements = deepQuerySelectorAll(this._component, DIALOG_CONSTANTS.selectors.INITIAL_FOCUS);
        if (elements && elements.length) {
            const initialElement = elements[elements.length - 1];
            initialElement.focus();
        }
    }
    isScrollable() {
        const contentElement = this._component.querySelector(DIALOG_CONSTANTS.selectors.CONTENT);
        if (contentElement) {
            return contentElement.scrollHeight > contentElement.offsetHeight;
        }
        return false;
    }
    addRootClass(name) {
        this._component.classList.add(name);
    }
    setFullscreen(value) {
        if (value) {
            this._containerElement.classList.add(DIALOG_CONSTANTS.classes.FULLSCREEN);
        }
        else {
            this._containerElement.classList.remove(DIALOG_CONSTANTS.classes.FULLSCREEN);
        }
    }
    setMoveable(value) {
        toggleClass(this._containerElement, value, DIALOG_CONSTANTS.classes.MOVEABLE);
    }
    tryLayoutRippleChildren() {
        const selectors = [
            ICON_BUTTON_CONSTANTS.elementName,
            SWITCH_CONSTANTS.elementName,
            CHECKBOX_CONSTANTS.elementName,
            RADIO_CONSTANTS.elementName
        ];
        const rippleChildren = deepQuerySelectorAll(this._component, selectors.join(':focus-within,'));
        rippleChildren.filter(el => typeof el.layout === 'function').forEach(el => el.layout());
    }
    setMoveTarget(selector) {
        if (!selector) {
            return false;
        }
        this._moveTargetElement = this._component.querySelector(selector);
        return !!this._moveTargetElement;
    }
    setMoveTargetHandler(type, listener) {
        if (this._moveTargetElement) {
            this._moveTargetElement.addEventListener(type, listener);
        }
    }
    removeDragTargetHandler(type, listener) {
        if (this._moveTargetElement) {
            this._moveTargetElement.removeEventListener(type, listener);
        }
    }
    getSurfaceBounds() {
        return this._surfaceElement.getBoundingClientRect();
    }
    setSurfacePosition(x, y, positionType) {
        this._surfaceElement.style.position = positionType === 'absolute' ? positionType : 'relative';
        if (y != null) {
            this._surfaceElement.style.top = y;
        }
        else {
            this._surfaceElement.style.removeProperty('top');
        }
        if (x != null) {
            this._surfaceElement.style.left = x;
        }
        else {
            this._surfaceElement.style.removeProperty('left');
        }
    }
    captureActiveElement() {
        this._activeElement = getActiveElement(this._component.ownerDocument);
        if (this._activeElement) {
            this._activeElement.blur();
        }
    }
    tryRestoreActiveElement() {
        if (this._activeElement) {
            if (this._activeElement.isConnected) {
                this._activeElement.focus();
            }
            this._activeElement = undefined;
        }
    }
}
