/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { EXPANSION_PANEL_CONSTANTS } from './expansion-panel-constants';
export class ExpansionPanelFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._open = false;
        this._orientation = 'vertical';
        this._animationType = 'default';
        this._clickListener = this._onClick.bind(this);
        this._keydownListener = this._onKeydown.bind(this);
        this._animationCompleteListener = this._onAnimationComplete.bind(this);
    }
    initialize() {
        this._adapter.addHeaderListener('click', this._clickListener);
        this._adapter.addHeaderListener('keydown', this._keydownListener);
        this._adapter.setAnimationCompleteListener(this._animationCompleteListener);
    }
    _onClick(evt) {
        const fromIgnoredEl = evt.composedPath().find((el) => el.nodeType === Node.ELEMENT_NODE && el.matches(EXPANSION_PANEL_CONSTANTS.selectors.IGNORE));
        if (fromIgnoredEl) {
            return;
        }
        evt.stopPropagation();
        this._toggle();
        this._dispatchToggleEvent();
    }
    _onKeydown(evt) {
        if (evt.key === ' ' || evt.key === 'Enter') {
            evt.stopPropagation();
            evt.preventDefault();
            this._toggle();
            this._dispatchToggleEvent();
        }
    }
    _onAnimationComplete() {
        if (!this._open) {
            this._adapter.setContentVisibility(false);
        }
        this._adapter.dispatchHostEvent(new CustomEvent(EXPANSION_PANEL_CONSTANTS.events.ANIMATION_COMPLETE, { detail: this._open }));
    }
    _togglePanel() {
        this._adapter.toggleHostAttribute(EXPANSION_PANEL_CONSTANTS.attributes.OPEN, this._open);
        this._adapter.tryToggleOpenIcon(this._open);
        if (this._open) {
            this._adapter.setContentVisibility(true);
        }
    }
    _dispatchToggleEvent() {
        const evt = new CustomEvent(EXPANSION_PANEL_CONSTANTS.events.TOGGLE, { detail: this._open, bubbles: true, composed: true });
        this._adapter.dispatchHostEvent(evt);
    }
    _toggle() {
        this.open = !this.open;
    }
    get open() {
        return this._open;
    }
    set open(value) {
        value = Boolean(value);
        if (this._open !== value) {
            this._open = value;
            this._togglePanel();
        }
    }
    get orientation() {
        return this._orientation;
    }
    set orientation(value) {
        if (this._orientation !== value) {
            this._orientation = value;
            this._adapter.setHostAttribute(EXPANSION_PANEL_CONSTANTS.attributes.ORIENTATION, this._orientation);
        }
    }
    get animationType() {
        return this._animationType;
    }
    set animationType(value) {
        if (this._animationType !== value) {
            this._animationType = value;
            this._adapter.setHostAttribute(EXPANSION_PANEL_CONSTANTS.attributes.ANIMATION_TYPE, this._animationType);
        }
    }
}
