import Vue from 'vue'
import Vuex from 'vuex'
import { mapState } from 'vuex'

Vue.use(Vuex)

const upload ={
    state: {
        status: {},
        options: {
            request_payment: true,
            request_payment_amount: 1,
            payment_currency: 'SAT'
        },
    },
    mutations:{
        setPayAmount (state, val){
            state.options.request_payment_amount = val
        },
        setUpStatus (state, status) {
            state.status = status
        },
        setCurrency(state, val){
            state.options.payment_currency = val
        },
        setRequestPay(state, val){
            state.options.request_payment = val
        }
    }
}

const down = {
    state: {
        files: [],
    },
    mutations: {
        setFiles(state, val){
            state.files = val
        }
    }
}

const base = {
    state: {
        invoice: {
            settled: false,
        },
        uploadId: 0
    },
    mutations: {
        setUploadId (state, id) {
            state.uploadId = id
        },
        setInvoice (state, invoice) {
            state.invoice = invoice
        }
    },
    getters: {
        paid(state) {
            return state.invoice.settled
        },
        expired (state){
            return ((new Date(state.invoice.expires_at*1000) - new Date() <= 0) &&
                        !state.paid)
        },
        unpaid (state){
            return !state.invoice.settled
        }
    }
}

export default new Vuex.Store({
    modules: {
        upload,
        down,
        base
    },
    strict: true
})
