/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { EXPANSION_PANEL_CONSTANTS } from '../expansion-panel/expansion-panel-constants';
import { ACCORDION_CONSTANTS } from './accordion-constants';
/**
 * The foundation class behind the `AccordionComponent`.
 */
export class AccordionFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
    }
    initialize() {
        this._attachListeners();
    }
    disconnect() {
        this._detachListeners();
    }
    _attachListeners() {
        this._hostInteractionCallback = (evt) => this._hostInteraction(evt);
        this._adapter.addEventListener(EXPANSION_PANEL_CONSTANTS.events.TOGGLE, this._hostInteractionCallback, this._adapter.getHostElement());
    }
    _detachListeners() {
        if (this._hostInteractionCallback) {
            this._adapter.removeEventListener(EXPANSION_PANEL_CONSTANTS.events.TOGGLE, this._hostInteractionCallback, this._adapter.getHostElement());
            this._hostInteractionCallback = undefined;
        }
    }
    _hostInteraction(evt) {
        switch (evt.type) {
            case EXPANSION_PANEL_CONSTANTS.events.TOGGLE:
                if (evt.detail) {
                    evt.stopPropagation();
                    const evtTarget = evt.target;
                    if (this._adapter.isNestedPanel(evtTarget)) {
                        return;
                    }
                    this._adapter.getChildPanels(this._panelSelector).forEach(panel => {
                        if (evtTarget !== panel && !this._adapter.isNestedPanel(panel)) {
                            panel.open = false;
                        }
                    });
                }
                break;
            default:
                break;
        }
    }
    /** Gets/sets the selector to use for finding the child expansion panels. Defaults to searching the direct children for `<forge-expansion-panel>` elements. */
    get panelSelector() {
        return this._panelSelector;
    }
    set panelSelector(value) {
        if (this._panelSelector !== value) {
            this._panelSelector = value;
            this._adapter.setHostAttribute(ACCORDION_CONSTANTS.attributes.PANEL_SELECTOR, this._panelSelector);
        }
    }
}
