import * as _ from 'lodash';

export function removeFalsy<T extends object>(obj: T): Partial<T> {
  return _.pickBy(obj, Boolean) as Partial<T>;
}

export function removeNullish<T extends object>(obj: T): Partial<T> {
  return _.pickBy(
    obj,
    (value) => value !== null && value !== undefined,
  ) as Partial<T>;
}

export async function urlToBase64(url: string) {
  const response = await fetch(url);
  if (!response.ok) {
    throw new Error(`HTTP error! Status: ${response.status}`);
  }

  const buffer = Buffer.from(await response.arrayBuffer());
  const contentType =
    response.headers.get('content-type') || 'application/octet-stream';
  return `data:${contentType};base64,${buffer.toString('base64')}`;
}
