import { TAClient } from 'moons-ta-client';
import { writable } from 'svelte/store';

// @ts-ignore
function createPersistedStore(key, startValue) {
    const storedValue = localStorage.getItem(key);
    const store = writable(storedValue ? JSON.parse(storedValue) : startValue);
    
    store.subscribe(value => {
        localStorage.setItem(key, JSON.stringify(value));
    });

    return store;
}

// set stores and their inital values
export const discordDataStore = createPersistedStore('discordDataTAUI', null);

export const discordTokenStore = createPersistedStore('discordAuthTAUI', null);

export const authTokenStore = createPersistedStore('authTokenTAUI', null);

export const TABotTokenStore = createPersistedStore('TABotTokenTAUI', null);

export const TAServerUrl = createPersistedStore('TAServerUrl', "server.tournamentassistant.net");

export const TAServerAPIPort = createPersistedStore('TAServerAPIPort', "8678");

export const TAServerPort = createPersistedStore('TAServerPort', "8676");

export const TAServerPlayerPort = createPersistedStore('TAServerPlayerPort', "8675");

// In the future, use a store for TAClient, since svelte is neat :)))
export const client = new TAClient();