import BigNumber from "bignumber.js";
import { Bid } from "./interfaces/bid.interface";

export function tryParseJSON(str: string): any {
  try {
    return JSON.parse(str);
  } catch (e) {
    return null;
  }
}

export function isAnyKeyword(str: string, keywordList = ["any", "all"]): boolean {
  return !str || keywordList.includes(str?.toLowerCase());
}

export const caseInsensitiveCompare = (left: string | null | undefined, right: string | null | undefined) =>
  (left && right && left.toLocaleLowerCase() === right.toLocaleLowerCase()) || (left ?? "") === (right ?? "");

export const isPoolConnectedToAggregatedPool = (bid: Bid) => {
  const { maxCapacityLimit, marketCapacityLimit, capacityAllowance, aggregatedPoolId } = bid;
  return [maxCapacityLimit, marketCapacityLimit, capacityAllowance, aggregatedPoolId]
    .map((value) => !new BigNumber(value || 0).isZero())
    .every((value) => value);
};
