import { signal } from '@preact/signals';
import { parseHash, routeToHash } from './routes.js';
export const route = signal(parseHash(typeof window !== 'undefined' ? window.location.hash : ''));
export const demoMode = signal('seeded');
export const apiKey = signal(typeof localStorage !== 'undefined' ? localStorage.getItem('tuberosa.v2.apiKey') ?? '' : '');
export const currentProject = signal('acme-billing');
export const tour = signal({ playing: false, index: 0 });
export const toasts = signal([]);
let toastSeq = 0;
export function pushToast(text, tone = 'info') {
    const id = ++toastSeq;
    toasts.value = [...toasts.value, { id, tone, text }];
    setTimeout(() => {
        toasts.value = toasts.value.filter((t) => t.id !== id);
    }, 4500);
}
export function setRoute(next, replace = false) {
    route.value = next;
    const hash = routeToHash(next);
    if (typeof history === 'undefined')
        return;
    if (replace)
        history.replaceState(null, '', hash);
    else
        history.pushState(null, '', hash);
}
export function setApiKey(v) {
    apiKey.value = v;
    if (typeof localStorage === 'undefined')
        return;
    if (v)
        localStorage.setItem('tuberosa.v2.apiKey', v);
    else
        localStorage.removeItem('tuberosa.v2.apiKey');
}
if (typeof window !== 'undefined') {
    window.addEventListener('hashchange', () => {
        route.value = parseHash(window.location.hash);
    });
}
