/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { APP_BAR_SEARCH_CONSTANTS } from './app-bar-search-constants';
export class AppBarSearchFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._disabled = false;
        this._keydownListener = (evt) => this._onKeydown(evt);
    }
    initialize() {
        this._adapter.initialize();
        this._tryForwardInputState();
        this._adapter.addInputEventListener('keydown', this._keydownListener);
    }
    _tryForwardInputState() {
        if (this._disabled) {
            this._adapter.setInputProperty('disabled', this._disabled);
        }
        if (this._adapter.hasHostAttribute(APP_BAR_SEARCH_CONSTANTS.attributes.VALUE)) {
            this._adapter.setInputProperty('value', this._adapter.getHostAttribute(APP_BAR_SEARCH_CONSTANTS.attributes.VALUE));
        }
        if (this._adapter.hasHostAttribute(APP_BAR_SEARCH_CONSTANTS.attributes.PLACEHOLDER)) {
            this._adapter.setInputProperty('placeholder', this._adapter.getHostAttribute(APP_BAR_SEARCH_CONSTANTS.attributes.PLACEHOLDER));
        }
    }
    _onKeydown({ key, target }) {
        if (this._disabled) {
            return;
        }
        if (key === 'Enter') {
            const data = {
                value: target.value
            };
            this._adapter.emitHostEvent(APP_BAR_SEARCH_CONSTANTS.events.INPUT, data, true);
        }
    }
    disconnect() {
        this._adapter.removeInputEventListener('keydown', this._keydownListener);
    }
    get disabled() {
        return this._disabled;
    }
    set disabled(value) {
        if (value !== this._disabled) {
            this._disabled = value;
            this._adapter.setInputProperty('disabled', this._disabled);
            this._adapter.toggleHostAttribute(APP_BAR_SEARCH_CONSTANTS.attributes.DISABLED, this._disabled);
        }
    }
    get value() {
        return this._adapter.getInputProperty('value');
    }
    set value(value) {
        this._adapter.setInputProperty('value', value);
    }
    get placeholder() {
        return this._adapter.getInputProperty('placeholder');
    }
    set placeholder(value) {
        this._adapter.setInputProperty('placeholder', value);
    }
}
