/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { BACKDROP_CONSTANTS } from './backdrop-constants';
export class BackdropFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._delay = BACKDROP_CONSTANTS.numbers.DELAY;
        this._maxOpacity = BACKDROP_CONSTANTS.numbers.OPACITY;
        this._adapter.addBackdropEventListener('click', this._onClick.bind(this));
    }
    initialize() {
        this._adapter.setBackdropOpacity(0);
        this._applyAppearance();
        this._adapter.setHostAttribute(BACKDROP_CONSTANTS.attributes.DELAY, this._delay.toString());
        this.fadeIn();
    }
    /**
     * Starts the fade-out animation to animate the backdrop to 0% opacity.
     */
    fadeOut() {
        return new Promise(resolve => {
            this._adapter.setBackdropOpacity(0);
            setTimeout(() => resolve(), BACKDROP_CONSTANTS.numbers.TRANSITION_DURATION);
        });
    }
    /**
     * Starts the fade-in animation to animate the backdrop to max opacity.
     */
    fadeIn() {
        return new Promise(resolve => {
            setTimeout(() => {
                this._adapter.setBackdropOpacity(this._maxOpacity);
                setTimeout(() => resolve(), BACKDROP_CONSTANTS.numbers.TRANSITION_DURATION);
            }, this.delay);
        });
    }
    _onClick(evt) {
        evt.stopPropagation();
        this._adapter.emitHostEvent(BACKDROP_CONSTANTS.events.BACKDROP_CLICK, undefined, true, true);
    }
    disconnect() {
        this._adapter.removeBackdropEventListener('click', this._onClick);
    }
    _applyAppearance() {
        if (this._appearance) {
            this._adapter.setHostAttribute(BACKDROP_CONSTANTS.attributes.APPEARANCE, String(this._appearance));
        }
        else {
            this._adapter.removeHostAttribute(BACKDROP_CONSTANTS.attributes.APPEARANCE);
        }
    }
    set delay(value) {
        if (this._delay !== value) {
            this._delay = value;
            this._adapter.setHostAttribute(BACKDROP_CONSTANTS.attributes.DELAY, this._delay.toString());
        }
    }
    get delay() {
        return this._delay;
    }
    set maxOpacity(value) {
        if (this._maxOpacity !== value) {
            this._maxOpacity = value;
            this._adapter.setHostAttribute(BACKDROP_CONSTANTS.attributes.MAX_OPACITY, this._maxOpacity.toString());
        }
    }
    get maxOpacity() {
        return this._maxOpacity;
    }
    get appearance() {
        return this._appearance;
    }
    set appearance(value) {
        if (this._appearance !== value) {
            this._appearance = value;
            this._applyAppearance();
        }
    }
}
