type RuntimeStoreBridge = {
  getState: () => unknown;
  subscribe: (listener: (...args: any[]) => void) => () => void;
};

let runtimeStore: RuntimeStoreBridge | null = null;

/** Register the shell store without making renderer chunks import Zustand. */
export function setRuntimeStoreBridge(store: RuntimeStoreBridge | null | undefined): void {
  runtimeStore = store && typeof store.getState === 'function' && typeof store.subscribe === 'function'
    ? store
    : null;
}

export function hasRuntimeStoreBridge(): boolean {
  return runtimeStore !== null;
}

export function getRuntimeStoreState(): unknown {
  return runtimeStore?.getState?.();
}

export function subscribeRuntimeStore(listener: (...args: any[]) => void): () => void {
  return runtimeStore?.subscribe?.(listener) ?? (() => {});
}
