import { useCallback, useSyncExternalStore } from 'react';

import { createStore } from './createStore';

type MaintenanceStoreState = {
  isConversationsMaintenance: boolean;
};

const maintenanceStore = createStore<MaintenanceStoreState>({
  isConversationsMaintenance: false,
});

export const useConversationsMaintenance = () => {
  const isConversationsMaintenance = useSyncExternalStore(
    maintenanceStore.subscribe,
    () => maintenanceStore.getState().isConversationsMaintenance,
  );

  const setConversationsMaintenance = useCallback((value: boolean) => {
    maintenanceStore.setState({ isConversationsMaintenance: value });
  }, []);

  return { isConversationsMaintenance, setConversationsMaintenance };
};
