/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { RADIO_CONSTANTS } from './radio-constants';
export class RadioFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._dense = false;
    }
    connect() {
        this._adapter.connect();
        this._adapter.deferRippleInitialization();
        this._focusListenerCallback = () => this._adapter.syncFocusedStateWithInput();
        this._disabledListenerCallback = () => this._adapter.syncDisabledStateWithInput();
        this._syncRadiogroupCheckStyles = () => this._adapter.syncRadiogroupCheckStyles();
        this._addFocusEventListeners();
        this._addInputDisabledListener();
        this._extendNativeInputSetter();
        this._addRadioChangeListener();
        this._adapter.syncCheckedStateWithInput();
        this._adapter.syncDisabledStateWithInput();
        this._adapter.syncFocusedStateWithInput();
        this._applyDense();
        this.syncCheckedState();
    }
    disconnect() {
        this._removeFocusEventListeners();
        this._removeInputDisabledListener();
        this._adapter.revertNativeInputSetter();
        this._removeRadioChangeListener();
        this._adapter.destroyRipple();
    }
    syncCheckedState() {
        this._adapter.syncCheckedStateWithInput();
    }
    _applyDense() {
        if (this._dense) {
            this._adapter.addRootClass(RADIO_CONSTANTS.classes.RADIO_DENSE);
            this._adapter.setHostAttribute(RADIO_CONSTANTS.attributes.DENSE);
        }
        else {
            this._adapter.removeRootClass(RADIO_CONSTANTS.classes.RADIO_DENSE);
            this._adapter.removeHostAttribute(RADIO_CONSTANTS.attributes.DENSE);
        }
    }
    _extendNativeInputSetter() {
        this._adapter.extendNativeInputSetter(this._syncRadiogroupCheckStyles);
    }
    _addRadioChangeListener() {
        this._adapter.addInputEventListener('change', this._syncRadiogroupCheckStyles);
    }
    _removeRadioChangeListener() {
        this._adapter.removeInputEventListener('change', this._syncRadiogroupCheckStyles);
    }
    _addFocusEventListeners() {
        this._adapter.addInputEventListener('focus', this._focusListenerCallback);
        this._adapter.addInputEventListener('blur', this._focusListenerCallback);
    }
    _removeFocusEventListeners() {
        this._adapter.removeInputEventListener('focus', this._focusListenerCallback);
        this._adapter.removeInputEventListener('blur', this._focusListenerCallback);
    }
    _addInputDisabledListener() {
        this._adapter.addInputDisabledAttributeChangeListener(this._disabledListenerCallback);
    }
    _removeInputDisabledListener() {
        this._adapter.removeInputDisabledAttributeChangeListener();
    }
    get dense() {
        return this._dense;
    }
    set dense(value) {
        if (this._dense !== value) {
            this._dense = value;
            this._applyDense();
        }
    }
}
