/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { BaseDrawerFoundation } from '../base';
import { MODAL_DRAWER_CONSTANTS } from './modal-drawer-constants';
export class ModalDrawerFoundation extends BaseDrawerFoundation {
    constructor(_adapter) {
        super(_adapter);
        this._adapter = _adapter;
        this._open = false;
        this._isInitialized = false;
        this._backdropClickListener = evt => this._onBackdropClick(evt);
    }
    connect() {
        super.connect();
        this._adapter.setBackdropCloseListener(this._backdropClickListener);
        this._setBackdrop(this._open);
        this._isInitialized = true;
    }
    disconnect() {
        this._isInitialized = false;
    }
    _triggerDrawerOpen() {
        super._triggerDrawerOpen();
        this._setBackdrop(true);
    }
    _triggerDrawerClose() {
        super._triggerDrawerClose();
        this._setBackdrop(false);
    }
    _onBackdropClick(evt) {
        const canClose = this._adapter.emitHostEvent(MODAL_DRAWER_CONSTANTS.events.CLOSE, undefined, true, true);
        if (canClose) {
            this.open = false;
        }
    }
    _setBackdrop(open) {
        if (open) {
            this._adapter.toggleBackdropClass(false, MODAL_DRAWER_CONSTANTS.classes.SCRIM_CLOSED);
            this._adapter.setBackdropVisibility(true);
        }
        else if (this._isInitialized) {
            this._adapter.setBackdropVisibility(false).then(() => {
                if (!this._open) {
                    this._adapter.toggleBackdropClass(true, MODAL_DRAWER_CONSTANTS.classes.SCRIM_CLOSED);
                }
            });
        }
    }
}
