/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { FIELD_CONSTANTS } from '../field';
import { BaseFieldCore } from '../field/base/base-field-core';
import { TEXT_FIELD_CONSTANTS } from './text-field-constants';
export class TextFieldCore extends BaseFieldCore {
    constructor(_adapter) {
        super(_adapter);
        this._adapter = _adapter;
        this._showClear = false;
        this._slotChangeListener = this._onSlotChange.bind(this);
        this._inputAttributeListener = this._onInputAttributeChange.bind(this);
        this._valueChangeListener = this._onValueChange.bind(this);
        this._inputListener = this._onValueChange.bind(this);
        this._clearButtonClickListener = (evt) => this._onClearButtonClick(evt);
    }
    initialize() {
        this._adapter.tryApplyGlobalConfiguration(['labelPosition']);
        this._adapter.addRootListener('slotchange', this._slotChangeListener);
        this._adapter.addRootListener('input', this._inputListener);
    }
    destroy() {
        this._adapter.removeRootListener('slotchange', this._slotChangeListener);
        this._adapter.removeRootListener('input', this._inputListener);
        this._adapter.removeValueChangeListener();
    }
    get popoverTargetElement() {
        return this._adapter.popoverTargetElement;
    }
    _onSlotChange(evt) {
        const target = evt.target;
        switch (target.name) {
            case 'label':
                this._adapter.tryConnectSlottedLabel(target);
                break;
            case '':
                this._adapter.handleDefaultSlotChange(target, this._inputAttributeListener);
                this._adapter.tryAddValueChangeListener(this, this._valueChangeListener);
                this._tryFloatLabel();
                break;
        }
    }
    _onInputAttributeChange(name, value) {
        switch (name) {
            case 'disabled':
                this.disabled = value !== null;
                break;
            case 'placeholder':
                this._tryFloatLabel();
                break;
        }
    }
    _onClearButtonClick(evt) {
        const event = new CustomEvent(TEXT_FIELD_CONSTANTS.events.CLEAR, {
            bubbles: true,
            cancelable: true,
            composed: true
        });
        this._adapter.dispatchHostEvent(event);
        if (!event.defaultPrevented) {
            this._adapter.clearInput();
        }
    }
    _onValueChange() {
        this._tryFloatLabel();
        this._toggleClearButtonVisibility();
    }
    _toggleClearButtonVisibility() {
        this._adapter.toggleClearButtonVisibility(this._showClear && this._hasValue && !this.disabled);
    }
    get showClear() {
        return this._showClear;
    }
    set showClear(value) {
        if (this._showClear !== value) {
            this._showClear = value;
            this._adapter.toggleHostAttribute(TEXT_FIELD_CONSTANTS.attributes.SHOW_CLEAR, value);
            if (value) {
                this._adapter.connectClearButton(this._clearButtonClickListener);
            }
            else {
                this._adapter.disconnectClearButton(this._clearButtonClickListener);
            }
            this._toggleClearButtonVisibility();
        }
    }
    get disabled() {
        return super.disabled;
    }
    set disabled(value) {
        if (this._disabled !== value) {
            this._disabled = value;
            this._adapter.toggleHostAttribute(FIELD_CONSTANTS.attributes.DISABLED, value);
            this._adapter.setFieldProperty('disabled', value);
            this._adapter.disableInput(value);
            this._toggleClearButtonVisibility();
        }
    }
    get labelPosition() {
        return super.labelPosition;
    }
    set labelPosition(value) {
        if (this._labelPosition !== value) {
            this._labelPosition = value;
            this._adapter.toggleHostAttribute(FIELD_CONSTANTS.attributes.LABEL_POSITION, true, value);
            this._adapter.setFieldProperty('labelPosition', value);
            this._tryFloatLabel();
        }
    }
}
