/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement, deepQuerySelectorAll } from '@tylertech/forge-core';
import { setDefaultAria } from '../constants';
import { BaseAdapter } from '../core/base/base-adapter';
import { BOTTOM_SHEET_CONSTANTS } from './bottom-sheet-constants';
export class BottomSheetAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._dialogElement = getShadowElement(component, BOTTOM_SHEET_CONSTANTS.selectors.DIALOG);
        this._surfaceElement = getShadowElement(component, BOTTOM_SHEET_CONSTANTS.selectors.SURFACE);
    }
    setDialogProperty(property, value) {
        this._dialogElement[property] = value;
    }
    addDialogListener(type, listener) {
        this._dialogElement.addEventListener(type, listener);
    }
    removeDialogListener(type, listener) {
        this._dialogElement.removeEventListener(type, listener);
    }
    initialize() {
        this._component[setDefaultAria]({
            role: 'dialog',
            ariaModal: this._component.mode === 'modal' || this._component.mode === 'inline-modal' ? 'true' : 'false'
        }, { setAttribute: true });
    }
    open() {
        this._dialogElement.mode = this._component.mode;
        this._dialogElement.open = true;
    }
    close() {
        this._dialogElement.open = false;
    }
    trySetInitialFocus() {
        window.requestAnimationFrame(() => {
            if (this._component.open && this._component.isConnected) {
                const element = this._component.querySelector(BOTTOM_SHEET_CONSTANTS.selectors.INITIAL_FOCUS);
                element === null || element === void 0 ? void 0 : element.focus();
            }
        });
    }
    isScrollable() {
        const contentElement = this._getContentElement();
        return contentElement ? contentElement.scrollHeight > contentElement.offsetHeight : false;
    }
    isContentChild(el) {
        var _a;
        const contentElement = this._getContentElement();
        return (_a = contentElement === null || contentElement === void 0 ? void 0 : contentElement.contains(el)) !== null && _a !== void 0 ? _a : false;
    }
    setDragging(isDragging) {
        this._dialogElement.classList.toggle(BOTTOM_SHEET_CONSTANTS.classes.DRAGGING, isDragging);
    }
    setFullscreen(value) {
        this._dialogElement.classList.toggle(BOTTOM_SHEET_CONSTANTS.classes.FULLSCREEN, value);
    }
    isFullscreen() {
        return this._dialogElement.classList.contains(BOTTOM_SHEET_CONSTANTS.classes.FULLSCREEN);
    }
    setContainerHeight(height) {
        if (height != null) {
            const percent = Math.min(100, 100 * height / window.innerHeight);
            this._dialogElement.style.setProperty('--forge-dialog-max-height', `${percent}%`);
        }
        else {
            this._dialogElement.style.removeProperty('--forge-dialog-max-height');
        }
    }
    getContainerBounds() {
        return this._surfaceElement.getBoundingClientRect();
    }
    setDragTargetHandler(type, listener) {
        this._surfaceElement.addEventListener(type, listener);
    }
    removeDragTargetHandler(type, listener) {
        this._surfaceElement.removeEventListener(type, listener);
    }
    setBodyScrollHandler(listener) {
        const contentElement = this._getContentElement();
        contentElement === null || contentElement === void 0 ? void 0 : contentElement.addEventListener('scroll', listener);
    }
    _getContentElement() {
        var _a;
        const contentElements = Array.from(deepQuerySelectorAll(this._component, BOTTOM_SHEET_CONSTANTS.selectors.CONTENT_BODY, false));
        return (_a = contentElements[0]) !== null && _a !== void 0 ? _a : null;
    }
    setBodyListener(type, listener, options) {
        document.body.addEventListener(type, listener, options);
    }
    removeBodyListener(type, listener) {
        document.body.removeEventListener(type, listener);
    }
}
