/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement, removeElement, deepQuerySelectorAll, toggleClass } from '@tylertech/forge-core';
import { BACKDROP_CONSTANTS } from '../backdrop';
import { CHECKBOX_CONSTANTS } from '../checkbox';
import { BaseAdapter } from '../core/base/base-adapter';
import { ICON_BUTTON_CONSTANTS } from '../icon-button';
import { RADIO_CONSTANTS } from '../radio';
import { SWITCH_CONSTANTS } from '../switch';
import { BOTTOM_SHEET_CONSTANTS } from './bottom-sheet-constants';
/**
 * Provides facilities for interacting with the internal DOM of `BottomSheetComponent`.
 */
export class BottomSheetAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._backdropElement = getShadowElement(component, BOTTOM_SHEET_CONSTANTS.selectors.BACKDROP);
        this._containerElement = getShadowElement(component, BOTTOM_SHEET_CONSTANTS.selectors.CONTAINER);
    }
    initializeAccessibility() {
        if (!this._component.hasAttribute('role')) {
            this._component.setAttribute('role', 'dialog');
            this._component.setAttribute('aria-modal', 'true');
        }
    }
    setVisibility(visible) {
        toggleClass(this._containerElement, visible, BOTTOM_SHEET_CONSTANTS.classes.OPEN);
    }
    attach() {
        const hostElement = document.querySelector(BOTTOM_SHEET_CONSTANTS.selectors.HOST) || document.body;
        hostElement.appendChild(this._component);
    }
    detach() {
        removeElement(this._component);
    }
    registerTransitionEndHandler(handler) {
        this._containerElement.addEventListener('transitionend', handler);
    }
    deregisterTransitionEndHandler(handler) {
        this._containerElement.removeEventListener('transitionend', handler);
    }
    setBodyListener(type, listener, options) {
        document.body.addEventListener(type, listener, options);
    }
    removeBodyListener(type, listener) {
        document.body.removeEventListener(type, listener);
    }
    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);
    }
    getOpenBottomSheets(selector) {
        return document.querySelectorAll(selector);
    }
    trySetInitialFocus() {
        const elements = deepQuerySelectorAll(this._component, BOTTOM_SHEET_CONSTANTS.selectors.INITIAL_FOCUS);
        if (elements && elements.length) {
            const initialElement = elements[elements.length - 1];
            initialElement.focus();
        }
    }
    isScrollable() {
        const contentElement = this._getContentElement();
        return contentElement ? contentElement.scrollHeight > contentElement.offsetHeight : false;
    }
    isContentChild(el) {
        const contentElement = this._getContentElement();
        return contentElement ? contentElement.contains(el) : false;
    }
    initScrollable() {
        const isScrollable = this.isScrollable();
        toggleClass(this._component, isScrollable, BOTTOM_SHEET_CONSTANTS.classes.SCROLLABLE);
        toggleClass(this._containerElement, isScrollable, BOTTOM_SHEET_CONSTANTS.classes.SCROLLABLE);
    }
    setDragging(isDragging) {
        toggleClass(this._containerElement, isDragging, BOTTOM_SHEET_CONSTANTS.classes.DRAGGING);
        toggleClass(this._component, isDragging, BOTTOM_SHEET_CONSTANTS.classes.DRAGGING);
    }
    setBackdropVisiblity(value) {
        this._backdropElement.hidden = !value;
    }
    setFullscreen(value) {
        toggleClass(this._containerElement, value, BOTTOM_SHEET_CONSTANTS.classes.FULLSCREEN);
    }
    tryLayoutChildren() {
        const layoutChildren = [ICON_BUTTON_CONSTANTS.elementName, SWITCH_CONSTANTS.elementName, CHECKBOX_CONSTANTS.elementName, RADIO_CONSTANTS.elementName];
        const commonLayoutElements = Array.from(this._component.querySelectorAll(layoutChildren.join(',')));
        commonLayoutElements.filter(el => typeof el.layout === 'function').forEach(el => el.layout());
    }
    setContainerHeight(height) {
        if (height != null) {
            const percent = Math.min(100, 100 * height / window.innerHeight);
            this._containerElement.style.maxHeight = `${percent}%`;
        }
        else {
            this._containerElement.style.removeProperty('max-height');
        }
    }
    getContainerBounds() {
        return this._containerElement.getBoundingClientRect();
    }
    setDragTargetHandler(type, listener) {
        this._containerElement.addEventListener(type, listener);
    }
    removeDragTargetHandler(type, listener) {
        this._containerElement.removeEventListener(type, listener);
    }
    setBodyScrollHandler(listener) {
        const contentElement = this._getContentElement();
        if (contentElement) {
            contentElement.addEventListener('scroll', listener);
        }
    }
    _getContentElement() {
        const contentElements = Array.from(deepQuerySelectorAll(this._component, BOTTOM_SHEET_CONSTANTS.selectors.CONTENT_BODY, false));
        return contentElements.length ? contentElements[0] : null;
    }
}
