import {
  USER_INFO,
  // USER_QUERY_INFO,
  USER_SIGNIN,
  ASSIGN_UNREADNUM,
  INCREASE_UNREADNUM,
  DECREASE_UNREADNUM,
  UPDATE_USER_INFO,
} from '@constants/user'

const INITIAL_STATE = {
  userInfo: {
    person: {},
    extraInfo: {},
    miniApp: {},
  },
  unReadNum: 0
}

export default function user(state = INITIAL_STATE, action) {
  switch (action.type) {
    case USER_INFO: {
      return {
        ...state,
        userInfo: {
          ...action.payload
        }
      }
    }
    // case USER_QUERY_INFO: {
    //   const { person } = state.userInfo
    //   return {
    //     ...state,
    //     userInfo: {
    //       person,
    //       extraInfo: {
    //         ...action.payload
    //       }

    //     }
    //   }
    // }
    case UPDATE_USER_INFO: {
      const { person, extraInfo, miniApp = {} } = state.userInfo
      // debugger
      return {
        ...state,
        userInfo: {
          extraInfo,
          person: {
            ...person,
            ...action.payload
          },
          miniApp,
        }
      }
    }
    case USER_SIGNIN: {
      let { person, extraInfo, miniApp = {} } = state.userInfo
      return {
        ...state,
        userInfo: {
          person: {
            ...person,
            points: (person.points + 1)
          },
          extraInfo: {
            ...extraInfo,
            havaSigned: 1
          },
          miniApp,
        }
      }
    }
    case ASSIGN_UNREADNUM: {
      let { unReadNum } = action.payload
      return {
        ...state,
        unReadNum
      }
    }
    case INCREASE_UNREADNUM: {
      let { unReadNum } = state
      return {
        ...state,
        unReadNum: unReadNum + 1
      }
    }
    case DECREASE_UNREADNUM: {
      return {
        ...state,
        unReadNum: 0
      }
    }
    default:
      return state
  }
}
