import { writable } from "svelte/store";
import { browser } from "$app/environment";

export const uuid = writable(fromLocalStorage("uuid", null));
toLocalStorage(uuid, "uuid");

function fromLocalStorage(storageKey, fallback) {
    if (browser) {
        const storedValue = localStorage.getItem(storageKey);
        if (storedValue !== "undefined") {
            return storedValue;
        }
    }
    return fallback;
}

function toLocalStorage(store, storageKey) {
    if (browser) {
        store.subscribe((value) => {
            localStorage.setItem(storageKey, value);
        });
    }
}
