/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { FormFieldComponentDelegate } from '../core/delegates/form-field-component-delegate';
import { TEXT_FIELD_CONSTANTS } from './text-field-constants';
export class TextFieldComponentDelegate extends FormFieldComponentDelegate {
    constructor(config) {
        super(config);
    }
    _build() {
        const textField = document.createElement(TEXT_FIELD_CONSTANTS.elementName);
        this._inputElement = this._buildInputElement(textField);
        textField.append(this._inputElement);
        return textField;
    }
    _configure() {
        if (this._config.options?.id) {
            this._element.id = this._config.options.id;
        }
        if (this._config.options?.label) {
            this._createLabel(this._config.options.label);
        }
        if (this._config.options?.startElement) {
            this._config.options.startElement.slot = 'start';
            this._element.append(this._config.options.startElement);
        }
        if (this._config.options?.leadingElement) {
            this._config.options.leadingElement.slot = 'start';
            this._element.append(this._config.options.leadingElement);
        }
        if (this._config.options?.endElement) {
            this._config.options.endElement.slot = 'end';
            this._element.append(this._config.options.endElement);
        }
        if (this._config.options?.trailingElement) {
            this._config.options.trailingElement.slot = 'end';
            this._element.append(this._config.options.trailingElement);
        }
        if (this._config.options?.accessoryElement) {
            this._config.options.accessoryElement.slot = 'accessory';
            this._element.append(this._config.options.accessoryElement);
        }
    }
    get value() {
        return this._inputElement.value;
    }
    set value(value) {
        this._inputElement.value = value;
    }
    get disabled() {
        return this._inputElement.disabled;
    }
    set disabled(value) {
        this._inputElement.disabled = value;
    }
    get invalid() {
        return this._element.invalid;
    }
    set invalid(value) {
        this._element.invalid = value;
    }
    get inputElement() {
        return this._inputElement;
    }
    get labelElement() {
        return this._labelElement;
    }
    onChange(listener) {
        this._inputElement.addEventListener('input', evt => listener(evt.target.value));
    }
    onFocus(listener) {
        this._inputElement.addEventListener('focus', evt => listener(evt));
    }
    onBlur(listener) {
        this._inputElement.addEventListener('blur', evt => listener(evt));
    }
    setLabel(text) {
        if (text) {
            if (this._labelElement) {
                this._labelElement.textContent = text;
            }
            else {
                this._createLabel(text);
            }
            return;
        }
        this._labelElement?.remove();
        this._labelElement = undefined;
    }
    setSupportText(text) {
        if (text) {
            if (!this._supportTextElement) {
                this._supportTextElement = document.createElement('span');
                this._supportTextElement.slot = 'support-text';
                this._element.append(this._supportTextElement);
            }
            this._supportTextElement.textContent = text;
            return;
        }
        this._supportTextElement?.remove();
        this._supportTextElement = undefined;
    }
    // Deprecated alias for `setSupportText`
    setHelperText(text) {
        this.setSupportText(text);
    }
    setSupportTextEnd(text) {
        if (text) {
            if (!this._supportTextEndElement) {
                this._supportTextEndElement = document.createElement('span');
                this._supportTextEndElement.slot = 'support-text-end';
                this._element.append(this._supportTextEndElement);
            }
            this._supportTextEndElement.textContent = text;
            return;
        }
        this._supportTextEndElement?.remove();
        this._supportTextEndElement = undefined;
    }
    floatLabel(value) {
        this._element.floatLabel = value;
    }
    _createLabel(text) {
        this._labelElement = document.createElement('label');
        this._labelElement.textContent = text;
        this._labelElement.slot = 'label';
        if (this._config.options?.id) {
            this._labelElement.htmlFor = this._config.options.id;
        }
        this._element.append(this._labelElement);
    }
    _buildInputElement(textField) {
        const inputElement = document.createElement('input');
        inputElement.type = this._config.options?.type ?? 'text';
        if (this._config.options?.value !== undefined) {
            inputElement.value = this._config.options.value;
        }
        if (this._config.options?.id) {
            inputElement.id = this._config.options.id;
        }
        if (this._config.options?.placeholder !== undefined) {
            inputElement.placeholder = this._config.options.placeholder;
        }
        textField.append(inputElement);
        return inputElement;
    }
}
