/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { addClass, getShadowElement, toggleAttribute, toggleClass } from '@tylertech/forge-core';
import { BaseAdapter } from '../../core/base';
import { STEP_CONSTANTS } from './step-constants';
import { STATE_LAYER_CONSTANTS } from '../../state-layer';
export class StepAdapter extends BaseAdapter {
    constructor(_component) {
        super(_component);
        this.unbounded = false;
        this._buttonElement = getShadowElement(_component, STEP_CONSTANTS.selectors.STEP);
        this._container = getShadowElement(_component, STEP_CONSTANTS.selectors.STEP_CONTAINER);
        this._stateLayerElement = getShadowElement(this._component, STATE_LAYER_CONSTANTS.elementName);
    }
    get root() {
        return this._buttonElement;
    }
    get disabled() {
        return this._buttonElement.disabled;
    }
    get component() {
        return this._component;
    }
    setRootTabIndex(value) {
        this._buttonElement.tabIndex = value;
    }
    initialize() {
        this._component.setAttribute('role', 'tab');
    }
    setIndex(value) {
        this._buttonElement.querySelector(STEP_CONSTANTS.selectors.INDEX).innerHTML = (value + 1 || '').toString();
    }
    toggleRootClass(className, on) {
        toggleClass(this._buttonElement, on, className);
    }
    setRootAttribute(attribute, value) {
        this._buttonElement.setAttribute(attribute, value);
    }
    focusButton() {
        this._buttonElement.focus();
    }
    toggleDisabled(disabled) {
        toggleClass(this._buttonElement, disabled, STEP_CONSTANTS.classes.DISABLED);
        toggleAttribute(this._buttonElement, disabled, 'aria-disabled');
        this._stateLayerElement.disabled = disabled;
        this._buttonElement.disabled = disabled;
    }
    toggleIcon(name, show) {
        const index = this._buttonElement.querySelector(STEP_CONSTANTS.selectors.INDEX);
        const icon = this._buttonElement.querySelector(STEP_CONSTANTS.selectors.ICON);
        if (!show) {
            index.style.display = 'inherit';
            icon.style.display = 'none';
            return;
        }
        index.style.display = 'none';
        icon.style.display = 'inherit';
        icon.name = name;
    }
    slotHasContent() {
        return this._expansionSlot.assignedElements({ flatten: true }).length > 0;
    }
    setClickListener(listener) {
        this._buttonElement.addEventListener('click', listener);
    }
    removeClickListener(listener) {
        this._buttonElement.removeEventListener('click', listener);
    }
    setSlotListener(listener) {
        this._expansionSlot.addEventListener('slotchange', listener);
    }
    removeSlotListener(listener) {
        if (this._expansionSlot) {
            this._expansionSlot.removeEventListener('slotchange', listener);
        }
    }
    setExpanded(expanded) {
        if (this._expansionPanel) {
            this._expansionPanel.open = expanded;
            toggleAttribute(this._expansionPanel, !expanded, 'tabindex', '-1');
        }
        toggleClass(this._buttonElement, expanded, STEP_CONSTANTS.classes.EXPANDED);
    }
    setExpansionPanelAnimations(animate) {
        this._expansionPanel.animationType = animate ? 'default' : 'none';
    }
    addExpansionPanel() {
        const panel = this._container.querySelector(STEP_CONSTANTS.selectors.EXPANSION_PANEL);
        if (panel) {
            return;
        }
        this._expansionPanel = this._container.appendChild(this._createExpansionPanel());
        this._expansionSlot = this._expansionPanel.querySelector(STEP_CONSTANTS.selectors.EXPANSION_SLOT);
    }
    removeExpansionPanel() {
        const panel = this._container.querySelector(STEP_CONSTANTS.selectors.EXPANSION_PANEL);
        if (panel) {
            this._container.removeChild(panel);
        }
    }
    addExpansionIcon() {
        const icon = this._buttonElement.querySelector(STEP_CONSTANTS.selectors.EXPANSION_ICON);
        if (icon) {
            return;
        }
        this._buttonElement.appendChild(this._createExpansionIcon());
    }
    removeExpansionIcon() {
        const icon = this._buttonElement.querySelector(STEP_CONSTANTS.selectors.EXPANSION_ICON);
        if (icon) {
            this._buttonElement.removeChild(icon);
        }
    }
    addExpansionPanelListener(event, listener) {
        this._expansionPanel.addEventListener(event, listener);
    }
    removeExpansionPanelListener(event, listener) {
        if (this._expansionPanel) {
            this._expansionPanel.removeEventListener(event, listener);
        }
    }
    isExpandedContentInFocus(checkElement) {
        return this._expansionSlot.assignedElements().some(element => element.contains(checkElement || document.activeElement));
    }
    _createExpansionPanel() {
        const panel = document.createElement('forge-expansion-panel');
        panel.animationType = 'none';
        panel.setAttribute('part', 'expansion-panel');
        addClass(STEP_CONSTANTS.classes.EXPANSION_PANEL, panel);
        const slot = document.createElement('slot');
        slot.setAttribute('name', 'expansion-content');
        addClass(STEP_CONSTANTS.classes.EXPANSION_CONTENT, slot);
        panel.appendChild(slot);
        return panel;
    }
    _createExpansionIcon() {
        const icon = document.createElement('forge-icon');
        addClass(STEP_CONSTANTS.classes.EXPANSION_ICON, icon);
        icon.name = 'keyboard_arrow_down';
        icon.setAttribute('part', 'expanded-icon');
        return icon;
    }
}
