function parseIntString (s: string | string[] | undefined) :number | null {
  if (typeof s !== 'string') return null
  const regex = /^[+-]?\d+$/
  return regex.test(s) ? Number(s) : null 
}

type RemoveNullKeysReturn<T, K extends keyof T = never> = 
  Partial<Pick<T, Exclude<keyof T, K>>> & Pick<T, K>

const removeNullKeys = <T extends object, K extends keyof T = never>(
  obj: T,
  exceptions?: readonly K[]
): RemoveNullKeysReturn<T, K> => {
  const result: Partial<T> = {}
  
  for (const [key, value] of Object.entries(obj) as [keyof T, T[keyof T]][]) {
    if (exceptions?.includes(key as K) || value !== null) {
      result[key] = value
    }
  }
  
  return result as RemoveNullKeysReturn<T, K>
}

const getFieldValue = (mod: string, props: undefined | null | string ) => {
  return mod !== '' ? mod : props === null || props === undefined ? null : ''
}

const formatNumber = (number: string | number) => {
  return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ")
}

import type { WebAppUser } from '@twa-dev/types'

const nameTgUser = (name: WebAppUser | undefined) => {
  if (!name || !name.first_name) return 'Error'
  return name.first_name + 
        (name.first_name && name.last_name ? ' ' : '') +
        name.last_name +
        (!(name.first_name || name.last_name) ? name.username : '')
}

export {
  parseIntString,
  removeNullKeys,
  getFieldValue,
  formatNumber,
  nameTgUser
}
