/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { SWITCH_CONSTANTS } from './switch-constants';
export class SwitchFoundation {
    get _submittedValue() {
        return this._on ? this._value : null;
    }
    constructor(_adapter) {
        this._adapter = _adapter;
        // State
        this._on = false;
        this._defaultOn = false;
        this._value = 'on';
        this._dense = false;
        this._disabled = false;
        this._required = false;
        this._readonly = false;
        this._icon = 'on';
        this._labelPosition = 'end';
        // Listeners
        this._clickListener = () => this._handleChange();
        this._keydownListener = (evt) => this._handleKeydown(evt);
        this._keyupListener = (evt) => this._handleKeyup(evt);
    }
    initialize() {
        this._adapter.addHostListener('click', this._clickListener, { capture: true });
        this._adapter.addHostListener('keydown', this._keydownListener);
        this._adapter.addHostListener('keyup', this._keyupListener, { capture: true });
        this._adapter.setIconVisibility(this._icon);
        this._adapter.syncValue(this._submittedValue);
    }
    _handleKeydown(evt) {
        if (evt.key === ' ') {
            evt.preventDefault();
        }
    }
    _handleKeyup(evt) {
        if (evt.key === ' ') {
            this._handleChange();
        }
    }
    _handleChange() {
        if (this._readonly) {
            return;
        }
        const oldValue = this._on;
        const newValue = !this._on;
        this._on = newValue;
        const event = new Event('change', { cancelable: true, bubbles: true });
        const forgeEvent = new CustomEvent(SWITCH_CONSTANTS.events.CHANGE, { detail: newValue, bubbles: true, cancelable: true });
        this._adapter.dispatchHostEvent(event);
        this._adapter.dispatchHostEvent(forgeEvent);
        this._on = oldValue;
        if (event.defaultPrevented || forgeEvent.defaultPrevented) {
            return;
        }
        this.on = newValue;
    }
    _setOnAttribute() {
        this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.ON, this._on);
        // Also set selected for backwards compatibility
        this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.SELECTED, this._on);
    }
    get on() {
        return this._on;
    }
    set on(value) {
        if (this._on !== value) {
            this._on = value;
            this._adapter.setOn(this._on);
            this._adapter.syncValue(this._submittedValue);
            this._setOnAttribute();
        }
    }
    get defaultOn() {
        return this._defaultOn;
    }
    set defaultOn(value) {
        if (this._defaultOn !== value) {
            this._defaultOn = value;
            this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.DEFAULT_ON, this._defaultOn);
        }
    }
    get value() {
        return this._value;
    }
    set value(value) {
        if (this._value !== value) {
            this._value = value;
            this._adapter.syncValue(this._submittedValue);
            this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.VALUE, true, this._value);
        }
    }
    get dense() {
        return this._dense;
    }
    set dense(value) {
        if (this._dense !== value) {
            this._dense = value;
            this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.DENSE, this._dense);
        }
    }
    get disabled() {
        return this._disabled;
    }
    set disabled(value) {
        if (this._disabled !== value) {
            this._disabled = value;
            this._adapter.setDisabled(this._disabled);
            this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.DISABLED, this._disabled);
        }
    }
    get required() {
        return this._required;
    }
    set required(value) {
        if (this._required !== value) {
            this._required = value;
            this._adapter.setRequired(this._required);
            this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.REQUIRED, this._required);
        }
    }
    get readonly() {
        return this._readonly;
    }
    set readonly(value) {
        if (this._readonly !== value) {
            this._readonly = value;
            this._adapter.setReadonly(this._readonly);
            this._adapter.toggleHostAttribute(SWITCH_CONSTANTS.attributes.READONLY, this._readonly);
        }
    }
    get icon() {
        return this._icon;
    }
    set icon(value) {
        if (this._icon !== value) {
            this._icon = value;
            this._adapter.setIconVisibility(this._icon);
            this._adapter.setHostAttribute(SWITCH_CONSTANTS.attributes.ICON, this._icon);
        }
    }
    get labelPosition() {
        return this._labelPosition;
    }
    set labelPosition(value) {
        if (this._labelPosition !== value) {
            this._labelPosition = value;
            this._adapter.setLabelPosition(this._labelPosition);
            this._adapter.setHostAttribute(SWITCH_CONSTANTS.attributes.LABEL_POSITION, this._labelPosition);
        }
    }
}
