import { writable, type Writable } from 'svelte/store';
import { browser } from '$app/environment';

export const welcome = writable(fromLocalStorage('welcome', '0'));
toLocalStorage(welcome, 'welcome');

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

function toLocalStorage(store: Writable<any>, storageKey: string) {
  if (browser) {
    store.subscribe((value: string) => {
      localStorage.setItem(storageKey, value);
    });
  }
}
