"use client";

import type { Dispatch, ReactNode } from "react";
import { createActorContext, shallowEqual } from "@xstate/react";
import type { SnapshotFrom } from "xstate";

import { privateRoomMachine } from "./private-room-machine";
import type {
  PrivateRoomEvent,
  PrivateRoomState as MachineContext,
} from "./private-room-machine";

export interface PrivateRoomContextState {
  status: string;
  items: MachineContext["items"];
  creditBalance: number;
  errorMessage: string | null;
  unlockingAlbumId: string | null;
  unlockErrorMessage: string | null;
  pendingConfirmAlbumId: string | null;
  unlockPaywallRequest: MachineContext["unlockPaywallRequest"];
  unlockSuccessNonce: number;
  isLoading: boolean;
  isUnlocking: boolean;
}

type PrivateRoomSnapshot = SnapshotFrom<typeof privateRoomMachine>;
type PrivateRoomSelector<T> = (snapshot: PrivateRoomSnapshot) => T;

const PrivateRoomActorContext = createActorContext(privateRoomMachine);

export interface PrivateRoomProviderProps {
  children: ReactNode;
}

export function PrivateRoomProvider({ children }: PrivateRoomProviderProps) {
  return (
    <PrivateRoomActorContext.Provider>{children}</PrivateRoomActorContext.Provider>
  );
}

export function usePrivateRoomState(): PrivateRoomContextState {
  return usePrivateRoomSelector(selectPrivateRoomState, shallowEqual);
}

export function usePrivateRoomDispatch(): Dispatch<PrivateRoomEvent> {
  return PrivateRoomActorContext.useActorRef().send;
}

export function usePrivateRoomSelector<T>(
  selector: PrivateRoomSelector<T>,
  compare?: (previous: T, next: T) => boolean,
): T {
  return PrivateRoomActorContext.useSelector(selector, compare);
}

function selectPrivateRoomState(
  state: PrivateRoomSnapshot,
): PrivateRoomContextState {
  return {
    status: String(state.value),
    items: state.context.items,
    creditBalance: state.context.creditBalance,
    errorMessage: state.context.errorMessage,
    unlockingAlbumId: state.context.unlockingAlbumId,
    unlockErrorMessage: state.context.unlockErrorMessage,
    pendingConfirmAlbumId: state.context.pendingConfirmAlbumId,
    unlockPaywallRequest: state.context.unlockPaywallRequest,
    unlockSuccessNonce: state.context.unlockSuccessNonce,
    isLoading: state.matches("loading"),
    isUnlocking: state.matches("unlocking"),
  };
}
