/**
 * @license
 * Copyright 2023 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() {
        var _a, _b;
        const textField = document.createElement(TEXT_FIELD_CONSTANTS.elementName);
        this._inputElement = this._buildInputElement(textField);
        if ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.leadingElement) {
            this._config.options.leadingElement.slot = 'leading';
            textField.appendChild(this._config.options.leadingElement);
        }
        if ((_b = this._config.options) === null || _b === void 0 ? void 0 : _b.trailingElement) {
            this._config.options.trailingElement.slot = 'trailing';
            textField.appendChild(this._config.options.trailingElement);
        }
        textField.appendChild(this._inputElement);
        return textField;
    }
    _configure() {
        var _a, _b;
        if ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.helperText) {
            this.setHelperText(this._config.options.helperText);
        }
        if (typeof ((_b = this._config.options) === null || _b === void 0 ? void 0 : _b.label) === 'string') {
            this._createLabel(this._config.options.label);
        }
    }
    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 || false;
    }
    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);
            }
        }
        else if (this._labelElement) {
            this._element.removeChild(this._labelElement);
            this._labelElement = undefined;
        }
    }
    setHelperText(text) {
        if (text) {
            if (!this._helperTextElement) {
                this._helperTextElement = document.createElement('span');
                this._helperTextElement.slot = 'helper-text';
                this._element.appendChild(this._helperTextElement);
            }
            this._helperTextElement.textContent = text;
        }
        else if (this._helperTextElement) {
            this._element.removeChild(this._helperTextElement);
            this._helperTextElement = undefined;
        }
    }
    floatLabel(value) {
        this._element.floatLabel(value);
    }
    _createLabel(text) {
        var _a;
        this._labelElement = document.createElement('label');
        this._labelElement.textContent = text;
        if ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.id) {
            this._labelElement.setAttribute('for', this._config.options.id);
        }
        this._element.appendChild(this._labelElement);
    }
    _buildInputElement(textField) {
        var _a, _b, _c, _d, _e;
        const inputElement = document.createElement('input');
        inputElement.type = ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.type) || 'text';
        if (((_b = this._config.options) === null || _b === void 0 ? void 0 : _b.value) !== undefined) {
            inputElement.value = this._config.options.value;
        }
        if ((_c = this._config.options) === null || _c === void 0 ? void 0 : _c.id) {
            inputElement.id = this._config.options.id;
        }
        if (typeof ((_d = this._config.options) === null || _d === void 0 ? void 0 : _d.placeholder) === 'string') {
            inputElement.placeholder = (_e = this._config.options) === null || _e === void 0 ? void 0 : _e.placeholder;
        }
        textField.appendChild(inputElement);
        return inputElement;
    }
}
