/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement } from '@tylertech/forge-core';
import { getFormState, getFormValue, getValidationMessage, internals, isFocusable } 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._defaultSlotElement = this._rootElement.querySelector('slot');
    }
    addListener(type, listener) {
        this._rootElement.addEventListener(type, listener);
    }
    removeListener(type, listener) {
        this._rootElement.removeEventListener(type, listener);
    }
    addSlotChangeListener(listener) {
        this._defaultSlotElement.addEventListener('slotchange', listener);
    }
    removeSlotChangeListener(listener) {
        this._defaultSlotElement.addEventListener('slotchange', listener);
    }
    deselect(selectedToggle) {
        const toggles = this._getButtonToggleElements();
        toggles.filter(t => t !== selectedToggle).forEach(t => t.selected = false);
    }
    setDisabled(value) {
        this._component[isFocusable] = !value;
        const toggles = this._getButtonToggleElements();
        toggles.forEach(t => t.disabled = value);
    }
    setReadonly(value) {
        const toggles = this._getButtonToggleElements();
        toggles.forEach(t => t.readonly = 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);
    }
    setFormValue() {
        if (!this._component.form) {
            return;
        }
        const data = this._component[getFormValue]();
        const state = this._component[getFormState]();
        this._component[internals].setFormValue(data, state);
    }
    setFormValidity() {
        if (!this._component.form || !this._component.required) {
            return;
        }
        const required = this._component.multiple ? !this._component.value.length : !this._component.value;
        if (required) {
            const validationMessage = this._component[getValidationMessage]({ required });
            this._component[internals].setValidity({ valueMissing: required }, validationMessage, this._getButtonToggleElements()[0]);
        }
        else {
            this._component[internals].setValidity({});
        }
    }
    _getButtonToggleElements() {
        return Array.from(this._component.querySelectorAll(BUTTON_TOGGLE_CONSTANTS.elementName));
    }
}
