/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { APP_BAR_SEARCH_CONSTANTS } from './app-bar-search-constants';
/**
 * Provides facilities and helper methods for creating an app-bar-search component.
 */
export class AppBarSearchFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._disabled = false;
        this._combined = false;
        this._combinedContext = APP_BAR_SEARCH_CONSTANTS.strings.DEFAULT_CONTEXT;
        this._combinedOptions = [];
        this._global = false;
        this._keydownListener = evt => this._onKeyDown(evt);
        this._focusListener = evt => this._onFocus(evt);
        this._blurListener = evt => this._onBlur(evt);
    }
    initialize() {
        this._adapter.initialize();
        if (!this._adapter.hasInputElement()) {
            throw new Error('An input element is required as a child of the app-bar-search component.');
        }
        this._adapter.setGlobalIconVisibility(this._global);
        this._adapter.setContextVisibility(this._combined);
        this._adapter.addSearchInputEventListener('keydown', this._keydownListener);
        this._adapter.addSearchInputEventListener('focus', this._focusListener);
        this._adapter.addSearchInputEventListener('blur', this._blurListener);
    }
    /**
     * Handles keydown events on our search input.
     * @param {KeyboardEvent} evt
     */
    _onKeyDown(evt) {
        if (evt.key === 'Enter' || evt.keyCode === 13) {
            const data = {
                value: evt.target.value,
                combined: this._combined,
                context: this._combinedContext
            };
            this._adapter.emitHostEvent(APP_BAR_SEARCH_CONSTANTS.events.INPUT, data, true);
        }
    }
    _onFocus(evt) {
        this._adapter.addRootClass(APP_BAR_SEARCH_CONSTANTS.classes.FOCUSED);
    }
    _onBlur(evt) {
        this._adapter.removeRootClass(APP_BAR_SEARCH_CONSTANTS.classes.FOCUSED);
    }
    disconnect() {
        this._adapter.removeSearchInputEventListener('keydown', this._keydownListener);
        this._adapter.removeSearchInputEventListener('focus', this._focusListener);
        this._adapter.removeSearchInputEventListener('blur', this._blurListener);
    }
    /** Gets/sets the disabled state of the input. */
    get disabled() {
        return this._disabled;
    }
    set disabled(value) {
        if (value !== this._disabled) {
            this._disabled = value;
            this._adapter.setDisabled(this._disabled);
            this._adapter.toggleHostAttribute(APP_BAR_SEARCH_CONSTANTS.attributes.DISABLED, this._disabled);
        }
    }
    /** Gets/sets the input value. */
    get value() {
        return this._adapter.getInputValue();
    }
    set value(value) {
        this._adapter.setInputValue(value);
    }
    /** Sets the placeholder text. */
    set placeholder(value) {
        this._adapter.setInputPlaceholder(value);
    }
    /** Gets/sets the combined bool. */
    get combined() {
        return this._combined;
    }
    set combined(value) {
        if (this._combined !== value) {
            this._combined = value;
            this._adapter.setContextVisibility(this._combined);
        }
    }
    /** Gets/sets options for the combined search dropdown. */
    // Note: this is currently the end of the combinedOptions implemention.
    //   In the future this will feed into a select or menu type dropdown.
    get combinedOptions() {
        if (!this._combinedOptions) {
            return [];
        }
        return this._combinedOptions.map(co => (Object.assign({}, co)));
    }
    set combinedOptions(value) {
        if (!value) {
            this._combinedOptions = [];
        }
        this._combinedOptions = value.map(v => (Object.assign({}, v)));
    }
    /** Gets/sets selected option for the combined search dropdown. */
    // Note: this is currently the end of the selectedCombinedOption implemention.
    //   In the future this will hold the selected select or menu type dropdown value.
    get selectedCombinedOption() {
        return this._selectedCombinedOption;
    }
    set selectedCombinedOption(value) {
        if (this._selectedCombinedOption !== value) {
            this._selectedCombinedOption = value;
        }
    }
    /** Gets/sets the input value. */
    get global() {
        return this._global;
    }
    set global(value) {
        if (this._global !== value) {
            this._global = value;
            this._adapter.setGlobalIconVisibility(this._global);
        }
    }
}
