import { getMemberSearchName } from '../utils';
import { useRawFilter } from './raw-filter';
export function useFilteredCubes(filterString, cubes, memberViewType) {
    // Search filters logic
    const rawFilterFn = useRawFilter();
    const cubesMap = cubes.reduce((acc, cube) => {
        acc[cube.name] = cube;
        return acc;
    }, {});
    const filterMemberFn = (items) => {
        return items.filter((item) => {
            const cubeName = item.name.split('.')[0];
            const cube = cubesMap[cubeName];
            const cubeId = cube ? getMemberSearchName(cube, memberViewType) : cubeName;
            const itemId = getMemberSearchName(item, memberViewType);
            return (rawFilterFn(cubeId ?? item.name, filterString) ||
                (itemId && rawFilterFn(itemId, filterString)));
        });
    };
    const members = [];
    const membersByCube = cubes.reduce((acc, cube) => {
        acc[cube.name] = {
            // search by member names
            measures: filterMemberFn(cube.measures ?? []).map((m) => m.name),
            dimensions: filterMemberFn(cube.dimensions ?? []).map((m) => m.name),
            segments: filterMemberFn(cube.segments ?? []).map((m) => m.name),
            // search by folder names
            folders: filterMemberFn(cube.folders ?? []).map((m) => m.name),
            // search by hierarchy names
            hierarchies: filterMemberFn(cube.hierarchies ?? []).map((m) => m.name),
        };
        members.push(...acc[cube.name].dimensions, ...acc[cube.name].measures, ...acc[cube.name].segments);
        return acc;
    }, {});
    return {
        isFiltered: !!filterString,
        cubes: cubes.filter((item) => {
            return (rawFilterFn(getMemberSearchName(item, memberViewType), filterString) ||
                membersByCube[item.name].dimensions.length ||
                membersByCube[item.name].measures.length ||
                membersByCube[item.name].segments.length ||
                membersByCube[item.name].folders.length ||
                membersByCube[item.name].hierarchies.length);
        }),
        membersByCube,
        members,
    };
}
//# sourceMappingURL=filtered-cubes.js.map