/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement } from '@tylertech/forge-core';
import { internals, setDefaultAria } from '../../constants';
import { BaseAdapter } from '../../core/base/';
import { RadioGroupManager } from '../core/radio-group-manager';
import { RADIO_CONSTANTS } from './radio-constants';
export class RadioAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._rootElement = getShadowElement(component, RADIO_CONSTANTS.selectors.ROOT);
        this._labelElement = getShadowElement(component, RADIO_CONSTANTS.selectors.LABEL);
        this._stateLayerElement = getShadowElement(component, RADIO_CONSTANTS.selectors.STATE_LAYER);
    }
    setChecked(checked, value) {
        this._component[setDefaultAria]({ ariaChecked: checked ? 'true' : 'false' });
        if (checked) {
            RadioGroupManager.setSelectedRadioInGroup(this._component);
        }
        // Update tab indices
        RadioGroupManager.syncRadioFocusableState(this._component);
        // Update the form value, state, and validity
        const formValue = checked ? value : null;
        const formState = checked ? 'checked' : 'unchecked';
        this._component[internals].setFormValue(formValue, formState);
        RadioGroupManager.setRadioGroupValidity(this._component);
    }
    /**
     * Attempts to set the disabled state of the radio. If the radio is in a disabled radio group, it
     * can't be enabled.
     *
     * @param value Whether the radio should be disabled.
     * @returns Whether the disabled state was set.
     */
    trySetDisabled(value) {
        const group = this._component.closest('forge-radio-group');
        if (!value && group && group.disabled) {
            return false;
        }
        this._component[setDefaultAria]({ ariaDisabled: `${!!value}` });
        RadioGroupManager.syncRadioFocusableState(this._component);
        return true;
    }
    setRequired(value) {
        this._component[setDefaultAria]({
            ariaRequired: value ? 'true' : 'false'
        }, { setAttribute: true });
        RadioGroupManager.setRadioGroupValidity(this._component);
    }
    setReadonly(value) {
        this._component[setDefaultAria]({ ariaDisabled: `${!!value}` });
    }
    disableStateLayer(value) {
        this._stateLayerElement.disabled = value;
    }
    setLabelPosition(value) {
        this._labelElement.remove();
        if (value === 'start') {
            this._rootElement.prepend(this._labelElement);
        }
        else {
            this._rootElement.append(this._labelElement);
        }
    }
    setUncheckedRadioGroupFocus(event) {
        RadioGroupManager.setUncheckedRadioGroupFocus(this._component, event);
    }
    focusNext() {
        RadioGroupManager.focusNextRadioInGroup(this._component);
    }
    focusPrevious() {
        RadioGroupManager.focusPreviousRadioInGroup(this._component);
    }
}
