import Link from 'next/link'
import Image from 'react-bootstrap/Image'
import { abbrNum, numWithUnits } from '@/lib/format'
import styles from './item.module.css'
import userStyles from './user-header.module.css'
import React, { useEffect, useMemo, useState } from 'react'
import { useQuery } from '@apollo/client'
import MoreFooter from './more-footer'
import { useData } from './use-data'
import Badges from './badge'
import { useMe } from './me'
import { MEDIA_URL } from '@/lib/constants'
import { NymActionDropdown } from '@/components/user-header'
import classNames from 'classnames'
import CheckCircle from '@/svgs/checkbox-circle-fill.svg'
// all of this nonsense is to show the stat we are sorting by first
const Stacked = ({ user }) => (user.optional.stacked !== null && {abbrNum(user.optional.stacked)} stacked)
const Spent = ({ user }) => (user.optional.spent !== null && {abbrNum(user.optional.spent)} spent)
const Posts = ({ user }) => (
  
    {numWithUnits(user.nposts, { unitSingular: 'post', unitPlural: 'posts' })}
  )
const Comments = ({ user }) => (
  
    {numWithUnits(user.ncomments, { unitSingular: 'comment', unitPlural: 'comments' })}
  )
const Referrals = ({ user }) => (user.optional.referrals !== null && {numWithUnits(user.optional.referrals, { unitSingular: 'referral', unitPlural: 'referrals' })})
const Seperator = () => ( \ )
const STAT_POS = {
  stacked: 0,
  spent: 1,
  posts: 2,
  comments: 3,
  referrals: 4
}
const STAT_COMPONENTS = [Stacked, Spent, Posts, Comments, Referrals]
function seperate (arr, seperator) {
  return arr.flatMap((x, i) => i < arr.length - 1 ? [x, seperator] : [x])
}
export function UserListRow ({ user, stats, className, onNymClick, showHat = true, selected }) {
  return (
    
      
        
      
      
        
          @{user.name}{showHat && 
}{selected && 
}
        
        {stats && (
          
            {stats.map((Comp, i) => )}
          
        )}
      
 
     
  )
}
export function UserBase ({ user, className, children, nymActionDropdown }) {
  return (
    
      
        
      
      
        
          
            @{user.name}
          
          {nymActionDropdown && }
        
        {children}
      
 
     
  )
}
export function User ({ user, rank, statComps, className = 'mb-2', Embellish, nymActionDropdown = false }) {
  const { me } = useMe()
  const showStatComps = statComps && statComps.length > 0
  return (
    <>
      {rank
        ? (
          
            {rank}
          
)
        : }
      
        {showStatComps &&
          
            {statComps.map((Comp, i) => )}
          
}
        {Embellish && }
      
    >
  )
}
function UserHidden ({ rank, Embellish }) {
  return (
    <>
      {rank
        ? (
          
            {rank}
          
)
        : }
      
        
          
        
        
          
            stacker is in hiding
          
          {Embellish && 
}
        
 
       
    >
  )
}
export function ListUsers ({ users, rank, statComps = seperate(STAT_COMPONENTS, Seperator), Embellish, nymActionDropdown }) {
  return (
    
      {users.map((user, i) => (
        user
          ? 
          : 
      ))}
    
  )
}
export default function UserList ({ ssrData, query, variables, destructureData, rank, footer = true, nymActionDropdown, statCompsProp }) {
  const { data, fetchMore } = useQuery(query, { variables })
  const dat = useData(data, ssrData)
  const [statComps, setStatComps] = useState(seperate(STAT_COMPONENTS, Seperator))
  useEffect(() => {
    // shift the stat we are sorting by to the front
    const comps = [...STAT_COMPONENTS]
    setStatComps(seperate([...comps.splice(STAT_POS[variables?.by || 0], 1), ...comps], Seperator))
  }, [variables?.by])
  const { users, cursor } = useMemo(() => {
    if (!dat) return {}
    if (destructureData) {
      return destructureData(dat)
    } else {
      return dat
    }
  }, [dat])
  if (!dat) {
    return 
  }
  return (
    <>
      
      {footer &&
        }
    >
  )
}
export function UsersSkeleton () {
  const users = new Array(21).fill(null)
  return (
    {users.map((_, i) => (
      
        
          
          
          
          
        
      
    ))}