import type { ResponseQueries } from '@/api/cache/types';

export function filterQueries(queriesObj: ResponseQueries, searchStr: string): ResponseQueries {
  const filteredObj: ResponseQueries = {};

  for (const key in queriesObj) {
    if (key.includes(searchStr)) {
      filteredObj[key] = queriesObj[key];
    } else {
      const queries: string[] = [];
      queriesObj[key].queries.forEach((queryKey) => {
        if (queryKey.toLowerCase().includes(searchStr.toLowerCase())) {
          queries.push(queryKey);
        }
      });
      if (queries.length) {
        filteredObj[key] = { ...queriesObj[key], queries };
      }
    }
  }

  return filteredObj;
}
