import { useEffect, useState } from 'react'

import {
    CLIENT_SETTINGS_EVENT,
    getClientBehaviorSettings,
} from '@/lib/settings'

import type { ClientBehaviorSettings } from '@/lib/settings'


export const useClientBehaviourSettings = (): ClientBehaviorSettings => {
  const [settings, setSettings] =
      useState<ClientBehaviorSettings> (() => getClientBehaviorSettings ())

  useEffect (() => {
    const handleClientSettingsChange = () => {
      setSettings (getClientBehaviorSettings ())
    }

    window.addEventListener (CLIENT_SETTINGS_EVENT, handleClientSettingsChange)
    return () =>
	window.removeEventListener (CLIENT_SETTINGS_EVENT, handleClientSettingsChange)
  }, [])

  return settings
}
