import {defineStore} from 'pinia'
import {config} from '@/config'
import {useNotificationStore} from "@/store/notification.js";
import performRequest from "@/backend.js";

export const useStagedRatesStore = defineStore('stagedRates', {
    state() {
        return {
            stagedChanges: {staged_changes: false, expires: null}
        }
    },
    getters: {
        hasStagedChanges(state) {
            return state.stagedChanges.staged_changes;
        },
        expiresSoon(state) {
            return !state.stagedChanges.staged_changes && state.stagedChanges.expires !== null && (state.stagedChanges.expires < 10);
        }
    },
    actions: {
        async checkStagedChanges() {
            const url = `${config.backendUrl}/rates/staged_changes`;
            const resp = await performRequest(this, 'GET', url, null,);
            this.stagedChanges = resp.data;
        },
        async applyChanges() {
            const url = `${config.backendUrl}/rates/staged_changes`;
            await performRequest(this, 'PUT', url, null, false);
            this.stagedChanges = {staged_changes: false, expires: null};
        },
    }

});