import { triggerManualChangeEvent } from "../form/utils.js";
import { noopAutocompleteFilter } from "./defaults.js";
/**
 * @since 6.0.0
 * @internal
 */ export function getDefaultValue(options) {
    const { query, filter, multiselect, defaultQuery, defaultValue, options: values, getOptionLabel } = options;
    if (typeof defaultValue !== "undefined") {
        return defaultValue;
    }
    // do not support determining the default value for a multiselect
    // autocomplete if only a query was provided.
    if (multiselect) {
        return [];
    }
    // do not support a default value out of the box for this case
    if (filter === noopAutocompleteFilter) {
        return null;
    }
    return ()=>{
        let q = "";
        if (query) {
            q = query;
        } else if (defaultQuery) {
            q = defaultQuery instanceof Function ? defaultQuery() : defaultQuery;
        }
        if (!q) {
            return null;
        }
        const filtered = filter({
            list: values,
            query: q,
            extractor: getOptionLabel
        });
        return filtered[0] ?? null;
    };
}
/**
 * @since 6.0.0
 * @internal
 */ export function getDefaultQuery(options) {
    const { value, getOptionLabel, defaultQuery } = options;
    if (defaultQuery) {
        return defaultQuery;
    }
    return ()=>{
        if (typeof value === "string") {
            return value;
        }
        if (value && typeof value === "object" && !("length" in value)) {
            return getOptionLabel(value);
        }
        return "";
    };
}
/**
 * This enforces that if the user clicks away, touches somewhere else on the
 * page, tabs to the next element, or focuses another element programmatically
 * the autocomplete will set the `value` back to the previous selected value if
 * there was one. i.e.
 *
 * Case 1:
 * - User selects "Apple"
 * - User hits backspace twice. Input displays "App"
 * - User clicks somewhere else on the page
 * - Input now displays "Apple" again
 *
 * Case 2:
 * - User selects "Apple"
 * - User clears the input
 * - User types "app"
 * - User clicks somewhere else on the page
 * - Input now displays "" and the value is set to `null`
 *
 * NOTE: This mutates the {@link EnforceSelectedValueOptions.prevAvailableOptions}
 *
 * @since 6.0.0
 * @internal
 */ export function enforceSelectedValue(options) {
    const { value, visible, container, popupRef, comboboxRef, getOptionLabel, availableOptions, prevAvailableOptions } = options;
    if (!container) {
        return;
    }
    window.requestAnimationFrame(()=>{
        if (container.contains(document.activeElement) || popupRef.current?.contains(document.activeElement)) {
            return;
        }
        if (visible) {
            // this makes it so that the options are not filtered again while closing
            prevAvailableOptions.current = availableOptions;
        }
        let label = "";
        if (typeof value === "string") {
            label = value;
        } else if (typeof value === "object" && value && !("length" in value)) {
            label = getOptionLabel(value);
        }
        triggerManualChangeEvent(comboboxRef.current, label);
    });
}
/**
 * @since 6.0.0
 * @internal
 */ export function isMultipleValues(value) {
    return !!value && typeof value === "object" && "length" in value;
}

//# sourceMappingURL=utils.js.map