/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { task } from '../../core/utils/event-utils';
import { BUTTON_TOGGLE_CONSTANTS } from './button-toggle-constants';
export class ButtonToggleFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._selected = false;
        this._disabled = false;
        this._readonly = false;
        this._clickListener = (evt) => this._onClick(evt);
        this._keydownListener = (evt) => this._onKeyDown(evt);
    }
    initialize() {
        this._adapter.initialize();
        if (!this._disabled && !this._readonly) {
            this._applyListeners();
        }
    }
    destroy() {
        this._removeListeners();
    }
    focus(options = { focusVisible: true }) {
        this._adapter.focusHost(options);
        if (options.focusVisible) {
            this._adapter.forceFocusVisible();
        }
    }
    click() {
        this._adapter.clickHost();
    }
    _applyListeners() {
        this._adapter.addHostListener('click', this._clickListener);
        this._adapter.addHostListener('keydown', this._keydownListener);
    }
    _removeListeners() {
        this._adapter.removeHostListener('click', this._clickListener);
        this._adapter.removeHostListener('keydown', this._keydownListener);
    }
    async _onClick(_evt) {
        const originalSelected = this._selected;
        this._selected = !this._selected;
        const detail = {
            value: this._value,
            selected: this._selected
        };
        const evt = new CustomEvent(BUTTON_TOGGLE_CONSTANTS.events.SELECT, { detail, bubbles: true, cancelable: true });
        this._adapter.dispatchHostEvent(evt);
        await task();
        this._selected = originalSelected;
        if (evt.defaultPrevented) {
            return;
        }
        this.selected = !originalSelected;
    }
    _onKeyDown(evt) {
        if (evt.key === ' ') {
            evt.preventDefault();
            this.click();
        }
    }
    get value() {
        return this._value;
    }
    set value(value) {
        if (this._value !== value) {
            this._value = value;
            this._adapter.setHostAttribute(BUTTON_TOGGLE_CONSTANTS.attributes.VALUE, `${this._value}`);
        }
    }
    get selected() {
        return this._selected;
    }
    set selected(value) {
        value = !!value;
        if (this._selected !== value) {
            this._selected = value;
            this._adapter.setSelected(this._selected);
            this._adapter.toggleHostAttribute(BUTTON_TOGGLE_CONSTANTS.attributes.SELECTED, this._selected);
        }
    }
    get disabled() {
        return this._disabled;
    }
    set disabled(value) {
        value = !!value;
        if (this._disabled !== value) {
            this._disabled = value;
            if (this._disabled) {
                this._removeListeners();
            }
            else {
                this._applyListeners();
            }
            this._adapter.setDisabled(this._disabled);
            this._adapter.toggleHostAttribute(BUTTON_TOGGLE_CONSTANTS.attributes.DISABLED, this._disabled);
        }
    }
    get readonly() {
        return this._readonly;
    }
    set readonly(value) {
        value = !!value;
        if (this._readonly !== value) {
            this._readonly = value;
            if (this._readonly) {
                this._removeListeners();
            }
            else {
                this._applyListeners();
            }
            this._adapter.toggleHostAttribute(BUTTON_TOGGLE_CONSTANTS.attributes.READONLY, this._readonly);
        }
    }
}
