/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement, removeAllChildren } from '@tylertech/forge-core';
import { ICON_CLASS_NAME } from '../../constants';
import { BaseAdapter } from '../../core/base/base-adapter';
import { BUTTON_TOGGLE_CONSTANTS } from '../button-toggle/button-toggle-constants';
import { BUTTON_TOGGLE_GROUP_CONSTANTS } from './button-toggle-group-constants';
export class ButtonToggleGroupAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._rootElement = getShadowElement(component, BUTTON_TOGGLE_GROUP_CONSTANTS.selectors.ROOT);
        this._slotElement = this._rootElement.querySelector('slot');
    }
    addListener(type, listener) {
        this._rootElement.addEventListener(type, listener);
    }
    removeListener(type, listener) {
        this._rootElement.removeEventListener(type, listener);
    }
    addSlotChangeListener(listener) {
        this._slotElement.addEventListener('slotchange', listener);
    }
    removeSlotChangeListener(listener) {
        this._slotElement.addEventListener('slotchange', listener);
    }
    deselect(selectedToggle) {
        const toggles = this._getButtonToggleElements();
        toggles.filter(t => t !== selectedToggle).forEach(t => t.selected = false);
    }
    applyAdjacentSelections(isVertical) {
        const toggles = this._getButtonToggleElements();
        for (let i = toggles.length - 1; i > 0; i--) {
            toggles[i].removeAttribute(BUTTON_TOGGLE_CONSTANTS.attributes.SELECTED_ADJACENT);
            toggles[i].removeAttribute(BUTTON_TOGGLE_CONSTANTS.attributes.SELECTED_ADJACENT_VERTICAL);
            if (toggles[i].selected && toggles[i - 1].selected) {
                const attr = isVertical ? BUTTON_TOGGLE_CONSTANTS.attributes.SELECTED_ADJACENT_VERTICAL : BUTTON_TOGGLE_CONSTANTS.attributes.SELECTED_ADJACENT;
                toggles[i].setAttribute(attr, '');
            }
        }
    }
    setVertical(isVertical) {
        if (isVertical) {
            this._rootElement.classList.add(BUTTON_TOGGLE_GROUP_CONSTANTS.classes.VERTICAL);
        }
        else {
            this._rootElement.classList.remove(BUTTON_TOGGLE_GROUP_CONSTANTS.classes.VERTICAL);
        }
    }
    setStretch(value) {
        if (value) {
            this._rootElement.classList.add(BUTTON_TOGGLE_GROUP_CONSTANTS.classes.STRETCH);
        }
        else {
            this._rootElement.classList.remove(BUTTON_TOGGLE_GROUP_CONSTANTS.classes.STRETCH);
        }
        const toggles = this._getButtonToggleElements();
        toggles.forEach(toggle => {
            if (value) {
                toggle.setAttribute('stretch', '');
            }
            else {
                toggle.removeAttribute('stretch');
            }
        });
    }
    setDense(value) {
        const toggles = this._getButtonToggleElements();
        toggles.forEach(t => t.dense = value);
    }
    setDisabled(value) {
        const toggles = this._getButtonToggleElements();
        toggles.forEach(t => t.disabled = value);
    }
    getSelectedValues() {
        const toggles = this._getButtonToggleElements();
        return toggles.filter(t => t.selected).map(t => t.value);
    }
    applyValues(values) {
        const toggles = this._getButtonToggleElements();
        toggles.forEach(t => t.selected = values.indexOf(t.value) >= 0);
    }
    createOptions(options) {
        removeAllChildren(this._component);
        options.forEach(o => {
            this._component.appendChild(this._createButtonToggle(o));
        });
    }
    _createButtonToggle(option) {
        const buttonToggle = document.createElement(BUTTON_TOGGLE_CONSTANTS.elementName);
        buttonToggle.value = option.value;
        if (option.label) {
            buttonToggle.textContent = option.label;
        }
        else if (option.icon) {
            const icon = document.createElement('i');
            icon.textContent = option.icon;
            icon.classList.add(ICON_CLASS_NAME);
            icon.setAttribute('aria-hidden', 'true');
            buttonToggle.appendChild(icon);
        }
        if (option.leadingIcon) {
            let leadingIcon;
            if (option.leadingIconType === 'component') {
                leadingIcon = document.createElement('forge-icon');
                leadingIcon.name = option.leadingIcon;
            }
            else {
                leadingIcon = document.createElement('i');
                leadingIcon.textContent = option.leadingIcon;
                leadingIcon.classList.add(ICON_CLASS_NAME);
                leadingIcon.setAttribute('aria-hidden', 'true');
            }
            leadingIcon.slot = 'leading';
            buttonToggle.appendChild(leadingIcon);
        }
        if (option.trailingIcon) {
            let trailingIcon;
            if (option.trailingIconType === 'component') {
                trailingIcon = document.createElement('forge-icon');
                trailingIcon.name = option.trailingIcon;
            }
            else {
                trailingIcon = document.createElement('i');
                trailingIcon.textContent = option.trailingIcon;
                trailingIcon.classList.add(ICON_CLASS_NAME);
                trailingIcon.setAttribute('aria-hidden', 'true');
            }
            trailingIcon.slot = 'trailing';
            buttonToggle.appendChild(trailingIcon);
        }
        return buttonToggle;
    }
    _getButtonToggleElements() {
        return Array.from(this._component.querySelectorAll(BUTTON_TOGGLE_CONSTANTS.elementName));
    }
}
