// The on/off preference for the ambient falling-petals layer (PetalFall). Some
// people find drifting petals distracting rather than cozy, so the whole effect
// is opt-out-able and the choice persists in localStorage so it survives reloads.
// Mirrors the tiny pub/sub used for the sound mute toggle (../audio/sounds).

const STORAGE_KEY = 'petal.petals'

let enabled = readEnabled()
const listeners = new Set<(on: boolean) => void>()

function readEnabled(): boolean {
  try {
    return localStorage.getItem(STORAGE_KEY) !== 'off'
  } catch {
    return true
  }
}

export function isPetalsEnabled(): boolean {
  return enabled
}

export function setPetalsEnabled(on: boolean): void {
  enabled = on
  try {
    localStorage.setItem(STORAGE_KEY, on ? 'on' : 'off')
  } catch {
    /* private mode — choice just won't persist */
  }
  listeners.forEach((fn) => fn(on))
}

export function onPetalsEnabledChange(fn: (on: boolean) => void): () => void {
  listeners.add(fn)
  return () => listeners.delete(fn)
}
