/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement, playKeyframeAnimation } from '@tylertech/forge-core';
import { BACKDROP_CONSTANTS } from '../backdrop';
import { setDefaultAria } from '../constants';
import { BaseAdapter } from '../core/base/base-adapter';
import { DialogComponent } from './dialog';
import { dialogStack, DIALOG_CONSTANTS, hideBackdrop, showBackdrop } from './dialog-constants';
export class DialogAdapter extends BaseAdapter {
    get moveHandleElement() {
        return this._moveHandleElement;
    }
    get surfaceElement() {
        return this._surfaceElement;
    }
    constructor(component) {
        super(component);
        this._dialogElement = getShadowElement(component, DIALOG_CONSTANTS.selectors.DIALOG);
        this._surfaceElement = getShadowElement(component, DIALOG_CONSTANTS.selectors.SURFACE);
        this._moveHandleElement = getShadowElement(component, DIALOG_CONSTANTS.selectors.MOVE_HANDLE);
        this._backdropElement = getShadowElement(component, BACKDROP_CONSTANTS.elementName);
        if (!this._backdropElement.shadowRoot) {
            window.customElements.upgrade(this._backdropElement);
        }
    }
    show() {
        /* c8 ignore next 3 */
        if (this._dialogElement.open) {
            return;
        }
        this._component[setDefaultAria]({
            role: this._component.type,
            ariaModal: this._component.mode === 'modal' || this._component.mode === 'inline-modal' ? 'true' : 'false'
        }, { setAttribute: true });
        // Show the dialog (and backdrop) based on modal vs non-modal
        const isModal = this._component.mode === 'modal' || this._component.mode === 'inline-modal';
        if (isModal) {
            if (this._component.animationType === 'none') {
                this._backdropElement.show();
            }
            else {
                this._backdropElement.fadeIn();
            }
        }
        if (this._component.mode === 'modal') {
            this._dialogElement.showModal();
        }
        else {
            this._dialogElement.show();
        }
        if (isModal) {
            this._hideBackdrops();
        }
        DialogComponent[dialogStack].add(this._component);
    }
    _hideBackdrops() {
        DialogComponent[dialogStack].forEach(dialog => dialog[hideBackdrop]());
    }
    _showBackdropMostRecent() {
        var _a;
        (_a = Array.from(DialogComponent[dialogStack]).filter(dialog => dialog.mode === 'modal' || dialog.mode === 'inline-modal').at(-1)) === null || _a === void 0 ? void 0 : _a[showBackdrop]();
    }
    async hide() {
        this._component[setDefaultAria]({
            role: null,
            ariaModal: null
        }, { setAttribute: true });
        const close = () => {
            this._surfaceElement.classList.remove(BACKDROP_CONSTANTS.classes.EXITING);
            this._dialogElement.close();
            DialogComponent[dialogStack].delete(this._component);
            this._showBackdropMostRecent();
        };
        if (this._component.animationType === 'none') {
            return Promise.resolve(close());
        }
        this._backdropElement.fadeOut();
        await playKeyframeAnimation(this._surfaceElement, BACKDROP_CONSTANTS.classes.EXITING);
        close();
    }
    addDialogFormSubmitListener(listener) {
        this._dialogElement.addEventListener('submit', listener);
    }
    removeDialogFormSubmitListener(listener) {
        this._dialogElement.removeEventListener('submit', listener);
    }
    addDialogCancelListener(listener) {
        this._dialogElement.addEventListener('cancel', listener);
    }
    removeDialogCancelListener(listener) {
        this._dialogElement.removeEventListener('cancel', listener);
    }
    addBackdropDismissListener(listener) {
        this._backdropElement.addEventListener('click', listener);
    }
    removeBackdropDismissListener(listener) {
        this._backdropElement.removeEventListener('click', listener);
    }
    tryAutofocus() {
        window.requestAnimationFrame(() => {
            window.requestAnimationFrame(() => {
                if (this._component.open && this._dialogElement.isConnected && !this._component.matches(':focus-within')) {
                    const autofocusElement = this._component.querySelector(DIALOG_CONSTANTS.selectors.AUTOFOCUS);
                    autofocusElement === null || autofocusElement === void 0 ? void 0 : autofocusElement.focus();
                }
            });
        });
    }
    tryLocateTriggerElement(id) {
        if (!id) {
            this.triggerElement = null;
            return;
        }
        const rootNode = this._component.getRootNode();
        this.triggerElement = rootNode.querySelector(`#${id}`);
    }
    addTriggerInteractionListener(listener) {
        var _a;
        (_a = this.triggerElement) === null || _a === void 0 ? void 0 : _a.addEventListener('click', listener);
    }
    removeTriggerInteractionListener(listener) {
        var _a;
        (_a = this.triggerElement) === null || _a === void 0 ? void 0 : _a.removeEventListener('click', listener);
    }
    hideBackdrop() {
        this._backdropElement.fadeOut();
    }
    showBackdrop() {
        this._backdropElement.fadeIn();
    }
    addSurfaceClass(className) {
        this._surfaceElement.classList.add(className);
    }
    removeSurfaceClass(className) {
        this._surfaceElement.classList.remove(className);
    }
}
