"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useModelPickerState: ()=>useModelPickerState
});
const external_react_namespaceObject = require("react");
const external_utils_cjs_namespaceObject = require("./utils.cjs");
function useModelPickerState(opts) {
    const { models, value, onChange, groupBy: initialGroupBy = "subscription", recommendedModelIds, previewModelIds, filter, initiallyCollapsedGroups, i18n } = opts;
    const reactId = (0, external_react_namespaceObject.useId)();
    const id = `apollo-model-picker-${reactId}`;
    const triggerRef = (0, external_react_namespaceObject.useRef)(null);
    const searchRef = (0, external_react_namespaceObject.useRef)(null);
    const [open, setOpen] = (0, external_react_namespaceObject.useState)(false);
    const [query, setQuery] = (0, external_react_namespaceObject.useState)('');
    const [activeIndex, setActiveIndex] = (0, external_react_namespaceObject.useState)(0);
    const [groupBy, setGroupBy] = (0, external_react_namespaceObject.useState)(initialGroupBy);
    const [collapsedSet, setCollapsedSet] = (0, external_react_namespaceObject.useState)(()=>new Set(initiallyCollapsedGroups ?? []));
    const visibleModels = (0, external_react_namespaceObject.useMemo)(()=>filter ? models.filter(filter) : models, [
        models,
        filter
    ]);
    const annotated = (0, external_react_namespaceObject.useMemo)(()=>{
        const groups = (0, external_utils_cjs_namespaceObject.groupModels)(visibleModels, groupBy, {
            recommendedModelIds,
            previewModelIds,
            i18n
        });
        return groups.flatMap((g)=>g.models.map((m)=>({
                    ...m,
                    groupKey: g.key,
                    groupLabel: g.label
                })));
    }, [
        visibleModels,
        groupBy,
        recommendedModelIds,
        previewModelIds,
        i18n
    ]);
    const filtered = (0, external_react_namespaceObject.useMemo)(()=>(0, external_utils_cjs_namespaceObject.filterModels)(annotated, query), [
        annotated,
        query
    ]);
    const groupCounts = (0, external_react_namespaceObject.useMemo)(()=>{
        const out = {};
        for (const m of annotated)out[m.groupKey] = (out[m.groupKey] ?? 0) + 1;
        return out;
    }, [
        annotated
    ]);
    const selected = (0, external_react_namespaceObject.useMemo)(()=>annotated.find((m)=>m.modelId === value) ?? models.find((m)=>m.modelId === value) ?? null, [
        annotated,
        models,
        value
    ]);
    const unknownValue = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!value) return null;
        if (selected) return null;
        if (0 === models.length) return null;
        return value;
    }, [
        models,
        selected,
        value
    ]);
    const collapsedGroups = (0, external_react_namespaceObject.useMemo)(()=>query.trim() ? new Set() : collapsedSet, [
        query,
        collapsedSet
    ]);
    const toggleGroup = (0, external_react_namespaceObject.useCallback)((groupKey)=>{
        setCollapsedSet((prev)=>{
            const next = new Set(prev);
            if (next.has(groupKey)) next.delete(groupKey);
            else next.add(groupKey);
            return next;
        });
    }, []);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (open) {
            const sel = filtered.findIndex((m)=>m.modelId === value);
            setActiveIndex(sel >= 0 ? sel : 0);
            requestAnimationFrame(()=>searchRef.current?.focus());
        } else setQuery('');
    }, [
        open
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (activeIndex >= filtered.length) setActiveIndex(0);
    }, [
        filtered.length,
        activeIndex
    ]);
    const choose = (0, external_react_namespaceObject.useCallback)((m)=>{
        onChange?.(m);
        setOpen(false);
        triggerRef.current?.focus();
    }, [
        onChange
    ]);
    const onSearchKeyDown = (0, external_react_namespaceObject.useCallback)((e)=>{
        if ('ArrowDown' === e.key) {
            e.preventDefault();
            setActiveIndex((i)=>Math.min(i + 1, Math.max(filtered.length - 1, 0)));
        } else if ('ArrowUp' === e.key) {
            e.preventDefault();
            setActiveIndex((i)=>Math.max(i - 1, 0));
        } else if ('Enter' === e.key) {
            e.preventDefault();
            const m = filtered[activeIndex];
            if (m) choose(m);
        } else if ('Escape' === e.key) {
            e.preventDefault();
            setOpen(false);
            triggerRef.current?.focus();
        }
    }, [
        filtered,
        activeIndex,
        choose
    ]);
    return {
        open,
        setOpen,
        query,
        setQuery,
        groupBy,
        setGroupBy,
        visibleModels,
        annotated,
        filtered,
        groupCounts,
        collapsedGroups,
        toggleGroup,
        selected,
        unknownValue,
        activeIndex,
        setActiveIndex,
        onSearchKeyDown,
        choose,
        id,
        triggerRef,
        searchRef
    };
}
exports.useModelPickerState = __webpack_exports__.useModelPickerState;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useModelPickerState"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
