/**
 * @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() {
        var _a, _b, _c, _d, _e, _f, _g;
        if ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.id) {
            this._element.id = this._config.options.id;
        }
        if ((_b = this._config.options) === null || _b === void 0 ? void 0 : _b.label) {
            this._createLabel(this._config.options.label);
        }
        if ((_c = this._config.options) === null || _c === void 0 ? void 0 : _c.startElement) {
            this._config.options.startElement.slot = 'start';
            this._element.append(this._config.options.startElement);
        }
        if ((_d = this._config.options) === null || _d === void 0 ? void 0 : _d.leadingElement) {
            this._config.options.leadingElement.slot = 'start';
            this._element.append(this._config.options.leadingElement);
        }
        if ((_e = this._config.options) === null || _e === void 0 ? void 0 : _e.endElement) {
            this._config.options.endElement.slot = 'end';
            this._element.append(this._config.options.endElement);
        }
        if ((_f = this._config.options) === null || _f === void 0 ? void 0 : _f.trailingElement) {
            this._config.options.trailingElement.slot = 'end';
            this._element.append(this._config.options.trailingElement);
        }
        if ((_g = this._config.options) === null || _g === void 0 ? void 0 : _g.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) {
        var _a;
        if (text) {
            if (this._labelElement) {
                this._labelElement.textContent = text;
            }
            else {
                this._createLabel(text);
            }
            return;
        }
        (_a = this._labelElement) === null || _a === void 0 ? void 0 : _a.remove();
        this._labelElement = undefined;
    }
    setSupportTextStart(text) {
        var _a;
        if (text) {
            if (!this._supportTextStartElement) {
                this._supportTextStartElement = document.createElement('span');
                this._supportTextStartElement.slot = 'support-text-start';
                this._element.append(this._supportTextStartElement);
            }
            this._supportTextStartElement.textContent = text;
            return;
        }
        (_a = this._supportTextStartElement) === null || _a === void 0 ? void 0 : _a.remove();
        this._supportTextStartElement = undefined;
    }
    // Deprecated alias for `setSupportTextStart`
    setHelperText(text) {
        this.setSupportTextStart(text);
    }
    setSupportTextEnd(text) {
        var _a;
        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;
        }
        (_a = this._supportTextEndElement) === null || _a === void 0 ? void 0 : _a.remove();
        this._supportTextEndElement = undefined;
    }
    floatLabel(value) {
        this._element.floatLabel = value;
    }
    _createLabel(text) {
        var _a;
        this._labelElement = document.createElement('label');
        this._labelElement.textContent = text;
        this._labelElement.slot = 'label';
        if ((_a = this._config.options) === null || _a === void 0 ? void 0 : _a.id) {
            this._labelElement.htmlFor = this._config.options.id;
        }
        this._element.append(this._labelElement);
    }
    _buildInputElement(textField) {
        var _a, _b, _c, _d, _e;
        const inputElement = document.createElement('input');
        inputElement.type = (_b = (_a = this._config.options) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : 'text';
        if (((_c = this._config.options) === null || _c === void 0 ? void 0 : _c.value) !== undefined) {
            inputElement.value = this._config.options.value;
        }
        if ((_d = this._config.options) === null || _d === void 0 ? void 0 : _d.id) {
            inputElement.id = this._config.options.id;
        }
        if (((_e = this._config.options) === null || _e === void 0 ? void 0 : _e.placeholder) !== undefined) {
            inputElement.placeholder = this._config.options.placeholder;
        }
        textField.append(inputElement);
        return inputElement;
    }
}
