import type { FeedType } from '@/modules/feeds/types';

export const isValidJSON = (str: string): boolean => {
  try {
    const parsed = JSON.parse(str);
    return typeof parsed === 'object' && parsed !== null;
  } catch (_: unknown) {
    console.warn(_);
    return false;
  }
};

export const pick = <T extends object, K extends keyof T>(obj: T, keys: readonly K[]) =>
  Object.fromEntries(keys.filter((key) => key in obj).map((key) => [key, obj[key]]));

export const filterOptionsByType = (
  options: {
    shouldHideForTypes?: FeedType[];
    label: string;
    value: string;
  }[],
  feedType: FeedType,
) => options.filter((option) => !option.shouldHideForTypes?.includes(feedType));
