
import {NFTService} from '@/service/nftService/nftService'

const nftService = new NFTService()
const stateData = {
    show:false,
    count:30,
    counter:null as any,
    value:null as any,
    resend:false,
    onAction:false,
  }
  
  export type CodeType = typeof stateData
  export const codeConfirmer = {
    namespaced: true,
    state: () => ({
      ...stateData,
    }),
    mutations: {
        CountDown(state:CodeType):void{
           state.counter = setInterval(()=>{
              if(state.count>0){
                state.resend = false
                state.count--
              }else{
                state.count = 30
                state.resend = true
                clearInterval(state.counter)
              }
            },1000)
        },
        SET_CODE(state:CodeType,payload:any){
          state.value = payload
        },
        ToggleCode(state:CodeType){
          state.show = !state.show;
        },

    },
    actions: {
      // async getVertiryCode(){
      //   const code = await nftService.sendTransferCode()
      //   return code 
      // },
      async getVertiryCode({commit,state} :any){
          if(state.count == 30){
            const req = await nftService.sendTransferCode()
            console.log(req);
            if(req){
              commit('CountDown')
            }
            return req 
          }
      },
      async doTransfer({commit,state} :any,payload:any){
          state.onAction =true
         const req =  await nftService.nftTransfer(payload)
         if(state.show){
          commit('ToggleCode')
         }
        //  console.log(req,'nftTransfer',req.code);
         if(req&&!req.message){
          clearInterval(state.counter)
          state.count=30
         }
         state.onAction = false
         return req
      },
      codeError({commit,state} :any){
        state.onAction = false
    }
    },
    getters: {},
  }

