import { useState, useEffect } from 'react'

export default function useAuth(person, page) {
  const [authPhone, setAuthPhone] = useState(false)
  const [authAvatar, setAuthAvatar] = useState(false)
  const [authPage, setAuthPage] = useState(false)

  useEffect(() => {
    if (page && page.auth && page.auth.length) {
      // 如果需要授权手机
      if (page.auth.indexOf('phone') > -1) {
        if (person.inited && !person.phone) {
          setAuthPhone(true)
        } else {
          setAuthPhone(false)
        }
      }

      // 如果需要授权头像
      if (page.auth.indexOf('avatar') > -1) {
        if (person.inited && (!person.avatarurl || person.avatarurl.indexOf('default_avatar') > -1)) {
          setAuthAvatar(true)
        } else {
          setAuthAvatar(false)
        }
      }

      // 如果需要授权头像
      if (page.auth.indexOf('page') > -1) {
        if (person.inited && !person.phone) {
          setAuthPage(true)
        } else {
          setAuthPage(false)
        }
      }
    } else {
      setAuthPhone(false)
      setAuthAvatar(false)
      setAuthPage(false)
    }
  }, [person, page])

  return [authPhone, authAvatar, authPage]
}
