import { useSyncExternalStore } from 'react'

// Scroll minimize coordination. The active screen publishes a minimize flag
// (true when scrolled past a small threshold downward) and the MenuBar
// subscribes. Matches Apple's `.automatic` tabBarMinimizeBehavior default:
// OFF unless the screen opts in. See MENU_HOLISTIC_V3.md §3.2.5.

const listeners = new Set<() => void>()
let minimized = false

export const scrollStore = {
  setMinimized(next: boolean) {
    if (minimized === next) return
    minimized = next
    for (const l of listeners) l()
  },
  get() {
    return minimized
  },
  subscribe(l: () => void) {
    listeners.add(l)
    return () => {
      listeners.delete(l)
    }
  },
}

export function useMinimized(): boolean {
  return useSyncExternalStore(scrollStore.subscribe, scrollStore.get, scrollStore.get)
}
