export type NonEmptyArray<T> = [T, ...T[]];

export function isNonEmptyArray<T>(value: T[]): value is NonEmptyArray<T> {
  return value.length > 0;
}

export function isEmptyArray<T>(value: T[]): value is [] {
  return value.length === 0;
}

export function NonEmptyExpectError<T>(error: Error, array: T[]): NonEmptyArray<T> {
  if (!isNonEmptyArray(array)) {
    throw error;
  }
  return array;
}

export function NonEmpty<T>(array: T[]): NonEmptyArray<T> {
  return NonEmptyExpectError(new Error("Expected array to be non-empty, but it was not."), array);
}

export function NonEmptyMaybe<T>(array: T[]): NonEmptyArray<T> | undefined {
  if (isNonEmptyArray(array)) {
    return array;
  }

  return undefined;
}
