import { areArraysEqual } from "../array";
import { stopEventBubbling } from "../form-input";
export class DropdownAutoFillUtil {
    constructor() {
        this.handleAutoFill = async (ev) => {
            stopEventBubbling(ev);
            if (this.isAutoFillAllowed()) {
                this.component.isAutoFilled = true;
                const autoFillValue = this.component.nativeEl.value;
                const newValue = await this.parseAutoFillValueWithOptions(autoFillValue);
                if (newValue === undefined) {
                    this.component.isAutoFilled = false;
                    return;
                }
                if (!areArraysEqual(newValue, this.component.rawValue)) {
                    this.component.valueUtil.updateRawValueBySelection(newValue, true);
                }
            }
        };
    }
    connectedCallback(component) {
        this.component = component;
    }
    isAutoFillAllowed() {
        return !this.component.multiple;
    }
    async parseAutoFillValueWithOptions(autoFillValue) {
        const options = await this.component.listEl.getOptions();
        const value = undefined;
        for (let index = 0; index < options.length; index++) {
            const option = options[index];
            if (option.value === autoFillValue || option.label === autoFillValue) {
                return [option.value];
            }
        }
        return value;
    }
}
