import { useState } from "react"
import Taro from '@tarojs/taro'
import { signIn, authPhone } from '@/services/person'

export default () => {
  const [person, setPerson] = useState()
  const [peronRoles, setPersonRoles] = useState([])

  const login = (params) => {
    signIn(params).then((res) => {
      const { person : taPerson, personRoleList, sessionKey, qrcode } = res;
      setPerson(taPerson);
      setPersonRoles(personRoleList);

      if (sessionKey) {
        Taro.setStorage({ key: 'sessionKey', data: sessionKey })
      }
      if (qrcode) {
        Taro.setStorage({ key: 'qrcode', data: qrcode })
      }
    })
  }

  const getPhone = (data) => {
    return authPhone(data).then((res) => {
      setPerson(res);
    })
  }

  return {
    person,
    peronRoles,
    setPerson,
    login,
    getPhone,
  }
}
