
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,
  }
  
  export type CodeType = typeof stateData
  export const codeConfirmer = {
    namespaced: true,
    state: () => ({
      ...stateData,
    }),
    mutations: {
        CountDown(state:CodeType):void{
          console.log(state.count);
          if(!state.counter){
           state.counter = setInterval(()=>{
              if(state.count>-1){
                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 openCodeConfirm({commit,state} :any){
        if(!state.show){
          commit('ToggleCode') 
          console.log(state.count);
          
          commit('CountDown')
          await nftService.sendTransferCode()
        }  
      },
    },
    getters: {},
  }

