/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { BaseSelectFoundation } from '../core';
import { SELECT_CONSTANTS } from './select-constants';
export class SelectFoundation extends BaseSelectFoundation {
    constructor(adapter) {
        super(adapter);
        this._label = '';
        this._permanentlyFloatLabel = false;
        this._mousedownListener = this._onMouseDown.bind(this);
    }
    initialize() {
        super.initialize();
        super.initializeTarget();
        this._initializeLabel();
        this._initializeAccessibility();
        this._adapter.addTargetListener('mousedown', this._mousedownListener);
    }
    disconnect() {
        super.disconnect();
        this._adapter.removeTargetListener('mousedown', this._mousedownListener);
    }
    syncFloatingLabelState({ force } = {}) {
        if (force !== undefined) {
            this._permanentlyFloatLabel = force;
        }
        this._updateLabel();
        this._tryFloatLabel();
    }
    setDisabled(value) {
        this._adapter.setDisabled(value);
    }
    _initializeLabel() {
        this._updateLabel();
        this._tryFloatLabel();
    }
    _initializeValue() {
        super._initializeValue();
        this._tryFloatLabel();
    }
    _initializeAccessibility() {
        this._adapter.initializeAccessibility();
    }
    _onMouseDown(evt) {
        evt.preventDefault();
    }
    _onClick(evt) {
        if (this._adapter.fieldElement.disabled) {
            return;
        }
        // We ignore clicks events that originate from within the field accessory slot
        const composedPath = evt.composedPath();
        if (composedPath.find(el => { var _a; return (_a = el.matches) === null || _a === void 0 ? void 0 : _a.call(el, 'slot[name=accessory]'); })) {
            return;
        }
        this._adapter.focusHost();
        super._onClick(evt);
    }
    _openDropdown() {
        super._openDropdown();
        if (this._open) {
            this._adapter.toggleHostAttribute(SELECT_CONSTANTS.attributes.OPEN, true);
        }
    }
    _closeDropdown() {
        super._closeDropdown();
        if (!this._open) {
            this._adapter.toggleHostAttribute(SELECT_CONSTANTS.attributes.OPEN, false);
        }
    }
    _onDismiss() {
        super._onDismiss();
    }
    _onDropdownScrollEnd() {
        this._adapter.emitHostEvent(SELECT_CONSTANTS.events.SCROLLED_BOTTOM);
    }
    /**
     * Handles selecting an item in the dropdown.
     * @param {ISelectOption} option The selected option.
     * @param {number} optionIndex The index of the selected option.
     */
    async _onSelect(option, optionIndex, closeDropdown = true) {
        const result = await super._onSelect(option, optionIndex, closeDropdown);
        if (result) {
            this._adapter.setSelectedText(this._getSelectedText());
            if (closeDropdown && !this._multiple) {
                this._adapter.focusHost();
            }
        }
        this._tryFloatLabel();
        return result;
    }
    _reset() {
        super._reset();
        this._adapter.setSelectedText('');
        this._tryFloatLabel();
    }
    _tryFloatLabel() {
        if (this._permanentlyFloatLabel) {
            this._adapter.floatLabel(true);
            return;
        }
        const hasValue = !!this._getSelectedText() || !!this._selectedValues.length;
        const hasPlaceholder = !!this._placeholder;
        this._adapter.floatLabel(hasValue || hasPlaceholder);
    }
    _updateLabel() {
        this._adapter.setLabel(this._label);
    }
    _applyValue(value) {
        super._applyValue(value);
        // Update the state of the component based on the existence of a selected value
        const text = this._getSelectedText();
        this._adapter.setSelectedText(text);
        if (!this._open) {
            this._tryFloatLabel();
        }
    }
    /** Gets/sets the label text. */
    get label() {
        return this._label;
    }
    set label(value) {
        if (this._label !== value) {
            this._label = value;
            this._updateLabel();
            if (this._label) {
                this._initializeLabel();
            }
            this._adapter.toggleHostAttribute(SELECT_CONSTANTS.attributes.LABEL, !!this._label, this._label);
        }
    }
    get placeholder() {
        return this._placeholder;
    }
    set placeholder(value) {
        if (this._placeholder !== value) {
            this._placeholder = value;
            this._adapter.setPlaceholderText(this._placeholder);
            this._initializeLabel();
        }
    }
}
