/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { FormFieldComponentDelegate } from '../core/delegates/form-field-component-delegate';
import { TextFieldComponentDelegate } from '../text-field';
import { AUTOCOMPLETE_CONSTANTS } from './autocomplete-constants';
import { IconComponentDelegate } from '../icon';
export class AutocompleteComponentDelegate extends FormFieldComponentDelegate {
    constructor(config) {
        super(config);
    }
    _build() {
        const autocomplete = document.createElement(AUTOCOMPLETE_CONSTANTS.elementName);
        this._attachTextField(autocomplete);
        return autocomplete;
    }
    get textFieldDelegate() {
        return this._textFieldDelegate;
    }
    get value() {
        return this._element.value;
    }
    set value(value) {
        this._element.value = value;
    }
    get disabled() {
        return this._textFieldDelegate.inputElement.disabled;
    }
    set disabled(value) {
        this._textFieldDelegate.inputElement.disabled = value;
    }
    get invalid() {
        return this._textFieldDelegate.invalid || false;
    }
    set invalid(value) {
        this._textFieldDelegate.invalid = value;
    }
    onChange(listener) {
        this._element.addEventListener(AUTOCOMPLETE_CONSTANTS.events.CHANGE, (evt) => listener(evt.detail));
    }
    onFocus(listener) {
        this._textFieldDelegate.inputElement.addEventListener('focus', (evt) => listener(evt));
    }
    onBlur(listener) {
        this._textFieldDelegate.inputElement.addEventListener('blur', (evt) => listener(evt));
    }
    _attachTextField(autocomplete) {
        var _a, _b, _c, _d, _e, _f;
        const textFieldConfig = {
            props: Object.assign({}, (_b = (_a = this._config.options) === null || _a === void 0 ? void 0 : _a.textFieldDelegateConfig) === null || _b === void 0 ? void 0 : _b.props),
            options: Object.assign({}, (_d = (_c = this._config.options) === null || _c === void 0 ? void 0 : _c.textFieldDelegateConfig) === null || _d === void 0 ? void 0 : _d.options)
        };
        if (!textFieldConfig.options) {
            textFieldConfig.options = {};
        }
        if (((_e = this._config.options) === null || _e === void 0 ? void 0 : _e.useDropdownIcon) !== false && !((_f = textFieldConfig.options) === null || _f === void 0 ? void 0 : _f.trailingElement)) {
            textFieldConfig.options.trailingElement = this._createDropdownIconElement();
        }
        this._textFieldDelegate = new TextFieldComponentDelegate(textFieldConfig);
        autocomplete.appendChild(this._textFieldDelegate.element);
    }
    _createDropdownIconElement() {
        const config = {
            props: {
                slot: 'trailing',
                name: 'arrow_drop_down'
            },
            options: {
                attributes: { 'forge-dropdown-icon': '' }
            }
        };
        return new IconComponentDelegate(config).element;
    }
}
