import { type UrlQueryMap } from '@grafana/data';

import { SECTION_STORAGE_KEY } from './constants';
import { type SearchState } from './types';

/**
 * Check if search query has filters enabled. Excludes folderId
 * @param query
 */
export const hasFilters = (query: SearchState) => {
  if (!query) {
    return false;
  }
  return Boolean(query.query || query.tag?.length > 0 || query.ownerReference?.length || query.starred || query.sort);
};

/** Cleans up old local storage values that remembered many open folders */
export const cleanupOldExpandedFolders = () => {
  const keyPrefix = SECTION_STORAGE_KEY + '.';

  for (let index = 0; index < window.localStorage.length; index++) {
    const lsKey = window.localStorage.key(index);
    if (lsKey?.startsWith(keyPrefix)) {
      window.localStorage.removeItem(lsKey);
    }
  }
};

/**
 * Get a storage key for a dashboard folder by its title
 * @param title
 */
export const getSectionStorageKey = (title = 'General') => {
  return `${SECTION_STORAGE_KEY}.${title.toLowerCase()}`;
};

/**
 * Remove undefined keys from url params object and format non-primitive values
 * @param params
 */
export const parseRouteParams = (params: UrlQueryMap) => {
  const cleanedParams = Object.entries(params).reduce<Partial<SearchState>>((obj, [key, val]) => {
    if (!val) {
      return obj;
    } else if (key === 'tag' && !Array.isArray(val)) {
      return { ...obj, tag: [val.toString()] };
    } else if (key === 'ownerReference' && !Array.isArray(val)) {
      return { ...obj, ownerReference: [val.toString()] };
    }

    return { ...obj, [key]: val };
  }, {});

  if (params.folder) {
    const folderStr = `folder:${params.folder}`;
    return {
      ...cleanedParams,
      query: `${folderStr} ${(cleanedParams.query ?? '').replace(folderStr, '')}`,
    };
  }

  return { ...cleanedParams };
};

/**
 * If we have filter, we can only show the results in the list layout as the tree layout does not work well
 */
export function needsListLayout(q: Partial<SearchState>) {
  return (
    q.query ||
    q.sort ||
    q.starred ||
    q.tag?.length ||
    q.createdBy ||
    q.ownerReference?.length ||
    q.panel_type ||
    q.datasource
  );
}
