import { type Writable, writable } from 'svelte/store'

async function createStore(): Promise<Writable<number>> {
	const days: number = parseInt(localStorage.getItem('lookbackDays') || '2')
	const { subscribe, update, set } = writable(days)
	return {
		subscribe,
		update,
		set(value: number) {
			if (value > 365)
				value = 365
			if (value < 1)
				value = 1
			localStorage.setItem('lookbackDays', value.toString())
			update(() => value)
		}
	}
}

export const lookbackDaysStore = await createStore()
