import FlexSearch from 'flexsearch';
import { useEffect, useRef, useState } from 'react';
import { useDeepEffect } from '../../hooks';
import { getNameMemberPairs } from '../../shared/members';
export function useCubeMemberSearch(memberTypeCubeMap) {
    const index = useRef(new FlexSearch.Index({ tokenize: 'forward' })).current;
    const [keys, setFilteredKeys] = useState([]);
    const [search, setSearch] = useState('');
    useDeepEffect(() => {
        const nameMemberPairs = getNameMemberPairs([
            ...memberTypeCubeMap.measures,
            ...memberTypeCubeMap.dimensions,
            ...memberTypeCubeMap.timeDimensions,
            ...memberTypeCubeMap.segments,
        ]);
        nameMemberPairs.forEach(([name, { title }]) => index.add(name, title));
    }, [memberTypeCubeMap]);
    useEffect(() => {
        let currentSearch = search;
        (async () => {
            const results = await index.search(search);
            if (currentSearch !== search) {
                return;
            }
            setFilteredKeys(results);
        })();
        return () => {
            currentSearch = '';
        };
    }, [index, search]);
    return {
        keys,
        search,
        inputProps: {
            value: search,
            onChange(event) {
                setSearch(event.target.value);
            },
        },
    };
}
//# sourceMappingURL=cube-member-search.js.map