/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { isDefined } from '@tylertech/forge-core';
import { BASE_DRAWER_CONSTANTS } from './base-drawer-constants';
export class BaseDrawerFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._open = true;
        this._direction = 'left';
        this._hasInitialized = false;
        this._openAnimationListener = () => this._onOpenComplete();
        this._closeAnimationListener = () => this._onCloseComplete();
    }
    connect() {
        if (this._open) {
            this._setOpened();
        }
        else {
            this._setClosed();
        }
        this._applyDirection();
        this._adapter.proxyScrollEvent();
        this._hasInitialized = true;
    }
    disconnect() {
        this._adapter.tryUnproxyScrollEvent();
        this._hasInitialized = false;
    }
    _applyDirection() {
        this._adapter.setDirection(this._direction);
        this._adapter.setHostAttribute(BASE_DRAWER_CONSTANTS.attributes.DIRECTION, isDefined(this._direction) ? this._direction.toString() : '');
    }
    _onOpenComplete() {
        if (!this._open) {
            return;
        }
        this._setOpened();
        this._adapter.emitHostEvent(BASE_DRAWER_CONSTANTS.events.AFTER_OPEN);
    }
    _onCloseComplete() {
        if (this._open) {
            return;
        }
        this._setClosed();
        this._adapter.emitHostEvent(BASE_DRAWER_CONSTANTS.events.AFTER_CLOSE);
    }
    _setOpened() {
        this._adapter.removeDrawerClass([BASE_DRAWER_CONSTANTS.classes.CLOSED, BASE_DRAWER_CONSTANTS.classes.CLOSING]);
        this._adapter.setHostAttribute(BASE_DRAWER_CONSTANTS.attributes.OPEN);
    }
    _setClosed() {
        this._adapter.removeDrawerClass([BASE_DRAWER_CONSTANTS.classes.CLOSING, BASE_DRAWER_CONSTANTS.classes.NO_TRANSITION]);
        this._adapter.setDrawerClass(BASE_DRAWER_CONSTANTS.classes.CLOSED);
        this._adapter.removeHostAttribute(BASE_DRAWER_CONSTANTS.attributes.OPEN);
    }
    _applyOpen() {
        if (this._open) {
            this._triggerDrawerOpen();
            this._adapter.setHostAttribute(BASE_DRAWER_CONSTANTS.attributes.OPEN);
        }
        else {
            this._triggerDrawerClose();
            this._adapter.removeHostAttribute(BASE_DRAWER_CONSTANTS.attributes.OPEN);
        }
    }
    _triggerDrawerOpen() {
        this._adapter.listenTransitionComplete(this._openAnimationListener);
        this._adapter.removeDrawerClass([BASE_DRAWER_CONSTANTS.classes.CLOSED, BASE_DRAWER_CONSTANTS.classes.CLOSING]);
    }
    _triggerDrawerClose() {
        this._adapter.listenTransitionComplete(this._closeAnimationListener);
        this._adapter.setDrawerClass(BASE_DRAWER_CONSTANTS.classes.CLOSING);
    }
    get open() {
        return this._open;
    }
    set open(value) {
        if (this._open !== value) {
            this._open = value;
            if (this._hasInitialized) {
                this._applyOpen();
            }
        }
    }
    get direction() {
        return this._direction;
    }
    set direction(value) {
        if (this._direction !== value) {
            this._direction = value;
            if (this._hasInitialized) {
                this._applyDirection();
            }
        }
    }
}
