import { createStore } from "zustand/vanilla";
import { useStore } from "zustand";

import type { ChannelSocketState } from "./types";

export const channelSocketStore = createStore<ChannelSocketState>(() => ({
  status: "idle",
  transport: "unknown",
  connected: false,
  inFlight: 0,
  queued: 0,
  lastError: null,
}));

export function useChannelSocketState<T = ChannelSocketState>(
  selector: (state: ChannelSocketState) => T = (state) => state as T,
): T {
  return useStore(channelSocketStore, selector);
}
