/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { BUTTON_TOGGLE_CONSTANTS } from '../button-toggle/button-toggle-constants';
import { BUTTON_TOGGLE_GROUP_CONSTANTS } from './button-toggle-group-constants';
export class ButtonToggleGroupFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._isInitialized = false;
        this._multiple = false;
        this._mandatory = false;
        this._vertical = false;
        this._stretch = false;
        this._dense = false;
        this._disabled = false;
        this._options = [];
        this._values = [];
        this._selectListener = evt => this._onSelect(evt);
        this._slotListener = () => this._synchronize();
    }
    initialize() {
        if (this._options && this._options.length) {
            this._applyOptions(false);
        }
        this._adapter.addListener(BUTTON_TOGGLE_CONSTANTS.events.SELECT, this._selectListener);
        this._adapter.addSlotChangeListener(this._slotListener);
        this._adapter.setVertical(this._vertical);
        this._adapter.setStretch(this._stretch);
        this._adapter.setDense(this._dense);
        this._adapter.setDisabled(this._disabled);
        this._adapter.applyAdjacentSelections(this._vertical);
        this._isInitialized = true;
    }
    disconnect() {
        this._isInitialized = false;
        this._adapter.removeListener(BUTTON_TOGGLE_CONSTANTS.events.SELECT, this._selectListener);
        this._adapter.removeSlotChangeListener(this._slotListener);
    }
    _synchronize() {
        if (!this._multiple) {
            const selectedValues = this._adapter.getSelectedValues().concat(this._values);
            this.value = selectedValues.length ? selectedValues[selectedValues.length - 1] : null;
        }
        else {
            const selectedValues = new Set(this._adapter.getSelectedValues().concat(this._values));
            this._applyValue(Array.from(selectedValues));
        }
    }
    _onSelect(evt) {
        const target = evt.target;
        // When in mandatory mode we need to ensure at least one element is selected. If there are no selections
        // then we need to reselect the target toggle because it was deselected
        if (this._mandatory) {
            const values = this._adapter.getSelectedValues();
            if (!values.length) {
                target.selected = true;
                return;
            }
        }
        // When not in multiple mode, we deselect all toggles, except for the one that was just changed
        if (!this._multiple) {
            this._adapter.deselect(target);
        }
        this._adapter.applyAdjacentSelections(this._vertical);
        this._adapter.emitHostEvent(BUTTON_TOGGLE_GROUP_CONSTANTS.events.CHANGE, this._getValue());
    }
    _getValue() {
        var _a;
        const selections = this._adapter.getSelectedValues();
        return this._multiple ? Array.from(new Set(selections)) : (_a = selections.slice(0, 1)[0]) !== null && _a !== void 0 ? _a : null;
    }
    _applyValue(value) {
        let values = value instanceof Array ? value : [value];
        if (!this._multiple && values.length > 1) {
            values = values[0];
        }
        this._values = values;
        this._adapter.applyValues(values);
        if (this._multiple) {
            this._adapter.applyAdjacentSelections(this._vertical);
        }
    }
    _applyOptions(init = true) {
        if (this._options) {
            this._adapter.createOptions(this._options);
            if (init) {
                this._adapter.setStretch(this._stretch);
                this._adapter.setDense(this._dense);
                this._adapter.setDisabled(this._disabled);
                this._adapter.applyAdjacentSelections(this._vertical);
            }
        }
    }
    get value() {
        if (!this._isInitialized) {
            return this._originalValue;
        }
        return this._getValue();
    }
    set value(value) {
        this._originalValue = value;
        this._applyValue(value);
    }
    get multiple() {
        return this._multiple;
    }
    set multiple(value) {
        if (this._multiple !== value) {
            this._multiple = value;
            this._adapter.setHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.MULTIPLE, this._multiple);
        }
    }
    get mandatory() {
        return this._mandatory;
    }
    set mandatory(value) {
        if (this._mandatory !== value) {
            this._mandatory = value;
            if (this._mandatory) {
                this._adapter.setHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.MANDATORY);
            }
            else {
                this._adapter.removeHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.MANDATORY);
            }
        }
    }
    get vertical() {
        return this._vertical;
    }
    set vertical(value) {
        if (this._vertical !== value) {
            this._vertical = value;
            this._adapter.setVertical(this._vertical);
            this._adapter.applyAdjacentSelections(this._vertical);
            if (this._vertical) {
                this._adapter.setHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.VERTICAL);
            }
            else {
                this._adapter.removeHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.VERTICAL);
            }
        }
    }
    get stretch() {
        return this._stretch;
    }
    set stretch(value) {
        if (this._stretch !== value) {
            this._stretch = value;
            this._adapter.setStretch(this._stretch);
            if (this._stretch) {
                this._adapter.setHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.STRETCH);
            }
            else {
                this._adapter.removeHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.STRETCH);
            }
        }
    }
    get dense() {
        return this._dense;
    }
    set dense(value) {
        if (this._dense !== value) {
            this._dense = value;
            this._adapter.setDense(this._dense);
            if (this._dense) {
                this._adapter.setHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.DENSE);
            }
            else {
                this._adapter.removeHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.DENSE);
            }
        }
    }
    get disabled() {
        return this._disabled;
    }
    set disabled(value) {
        if (this._disabled !== value) {
            this._disabled = value;
            this._adapter.setDisabled(this._disabled);
            if (this._disabled) {
                this._adapter.setHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.DISABLED);
            }
            else {
                this._adapter.removeHostAttribute(BUTTON_TOGGLE_GROUP_CONSTANTS.attributes.DISABLED);
            }
        }
    }
    get options() {
        return this._options.map(o => (Object.assign({}, o)));
    }
    set options(value) {
        this._options = value.map(o => (Object.assign({}, o)));
        if (this._isInitialized) {
            this._applyOptions();
        }
    }
}
