/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { QUANTITY_FIELD_CONSTANTS } from './quantity-field-constants';
export class QuantityFieldFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._incrementValue = () => this._adapter.increment();
        this._decrementValue = () => this._adapter.decrement();
        this._incrementButtonSlotChanged = () => this._onIncrementButtonSlotChanged();
        this._decrementButtonSlotChanged = () => this._onDecrementButtonSlotChanged();
        this._adapter.addIncrementButtonSlotListener(this._incrementButtonSlotChanged);
        this._adapter.addDecrementButtonSlotListener(this._decrementButtonSlotChanged);
    }
    connect() {
        this._adapter.addInputDisabledAttributeChangeListener(() => this._syncDisabledStateOfButtons());
        this._syncDisabledStateOfButtons();
        this._adapter.initializeButtons();
    }
    disconnect() {
        this._removeIncrementEventListener();
        this._removeDecrementEventListener();
        this._removeIncrementButtonSlotListener();
        this._removeDecrementButtonSlotListener();
        this._adapter.removeInputDisabledAttributeChangeListener();
    }
    _onIncrementButtonSlotChanged() {
        this._addIncrementEventListener();
    }
    _onDecrementButtonSlotChanged() {
        this._addDecrementEventListener();
    }
    _addIncrementEventListener() {
        this._adapter.addIncrementEventListener('click', this._incrementValue);
    }
    _addDecrementEventListener() {
        this._adapter.addDecrementEventListener('click', this._decrementValue);
    }
    _removeIncrementEventListener() {
        this._adapter.removeIncrementEventListener('click', this._incrementValue);
    }
    _removeDecrementEventListener() {
        this._adapter.removeDecrementEventListener('click', this._decrementValue);
    }
    _removeIncrementButtonSlotListener() {
        this._adapter.removeIncrementButtonSlotListener(this._incrementButtonSlotChanged);
    }
    _removeDecrementButtonSlotListener() {
        this._adapter.removeDecrementButtonSlotListener(this._decrementButtonSlotChanged);
    }
    _syncDisabledStateOfButtons() {
        const isDisabled = this._adapter.inputHasAttribute(QUANTITY_FIELD_CONSTANTS.attributes.DISABLED);
        if (isDisabled) {
            this._adapter.addDecrementButtonAttribute(QUANTITY_FIELD_CONSTANTS.attributes.DISABLED);
            this._adapter.addIncrementButtonAttribute(QUANTITY_FIELD_CONSTANTS.attributes.DISABLED);
        }
        else {
            this._adapter.removeIncrementButtonAttribute(QUANTITY_FIELD_CONSTANTS.attributes.DISABLED);
            this._adapter.removeDecrementButtonAttribute(QUANTITY_FIELD_CONSTANTS.attributes.DISABLED);
        }
    }
    get required() {
        return this._required;
    }
    set required(value) {
        if (this._required === value) {
            return;
        }
        this._required = value;
        if (this._required) {
            this._adapter.setHostAttribute(QUANTITY_FIELD_CONSTANTS.attributes.REQUIRED);
            this._adapter.addRootClass(QUANTITY_FIELD_CONSTANTS.classes.REQUIRED);
        }
        else {
            this._adapter.removeHostAttribute(QUANTITY_FIELD_CONSTANTS.attributes.REQUIRED);
            this._adapter.removeRootClass(QUANTITY_FIELD_CONSTANTS.classes.REQUIRED);
        }
    }
    get invalid() {
        return this._invalid;
    }
    set invalid(value) {
        if (this._invalid === value) {
            return;
        }
        this._invalid = value;
        if (this._invalid) {
            this._adapter.setHostAttribute(QUANTITY_FIELD_CONSTANTS.attributes.INVALID);
            this._adapter.addRootClass(QUANTITY_FIELD_CONSTANTS.classes.INVALID);
            this._adapter.addTextFieldAttribute(QUANTITY_FIELD_CONSTANTS.attributes.INVALID);
        }
        else {
            this._adapter.removeHostAttribute(QUANTITY_FIELD_CONSTANTS.attributes.INVALID);
            this._adapter.removeRootClass(QUANTITY_FIELD_CONSTANTS.classes.INVALID);
            this._adapter.removeTextFieldAttribute(QUANTITY_FIELD_CONSTANTS.attributes.INVALID);
        }
    }
}
