export function addOrRemove<T>(array: T[], element: T) {
  if (array.includes(element)) {
    return array.filter(e => e !== element);
  }
  return array.concat(element);
}

export function addOrRemoveById<T extends { id: number | string }>(
  array: T[],
  element: T,
) {
  if (array.some(e => e.id === element.id)) {
    return array.filter(e => e.id !== element.id);
  }
  return array.concat(element);
}

export const insertIf = <T>(condition: boolean, element: T): T[] =>
  condition ? [element] : ([] as T[]);

export const emptyArrayToNull = <T>(arr: T[]): T[] | null => {
  return arr.length === 0 ? null : arr;
};
