import React from 'react'

import { Dict } from './types'

/** Is value an array. */
export function isArray<T>(value: any): value is T[] {
  return Array.isArray(value)
}

/** Is value a function. */
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function isFunction<T extends Function = Function>(value: any): value is T {
  return typeof value === 'function'
}

/** Is value JSX / React Element */
export function isJsx<T = JSX.Element>(value: any): value is T {
  return React.isValidElement(value)
}

/** Is value a number. */
export function isNumber(value: any): value is number {
  return typeof value === 'number'
}

/** Is value a regular object (functions, arrays and React elements excluded). */
export function isObject<T = Dict>(value: any): value is T {
  return !!value && typeof value === 'object' && !isFunction(value) && !isArray(value) && !React.isValidElement(value)
}

/** Is value a valid ReactText - string or number. */
export function isReactText(value: any): value is React.ReactText {
  return isNumber(value) || isString(value)
}

/** Is value a string. */
export function isString(value: any): value is string {
  return Object.prototype.toString.call(value) === '[object String]'
}
