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 = queriesObj[key].queries;
      const includes = queries.filter((queryKey) =>
        queryKey.toLowerCase().includes(searchStr.toLowerCase())
      );
      if (includes.length) {
        filteredObj[key] = { ...queriesObj[key], queries };
      }
    }
  }

  return filteredObj;
}
