import type { Accessor } from "solid-js";
import { createContext, useContext } from "solid-js";

/** Context shared between all Pagination parts. */
export interface PaginationContextValue {
  page: Accessor<number>;
  totalPages: Accessor<number>;
  onPageChange: (page: number) => void;
  siblingCount: Accessor<number>;
}

const PaginationContext = createContext<PaginationContextValue>();

/**
 * Returns the Pagination context. Throws if used outside <Pagination>.
 */
export function usePaginationContext(): PaginationContextValue {
  const ctx = useContext(PaginationContext);
  if (!ctx) {
    throw new Error(
      "[PettyUI] Pagination parts must be used inside <Pagination>.\n" +
        "  Fix: Wrap Pagination.Previous, Pagination.Next, etc. inside <Pagination>.",
    );
  }
  return ctx;
}

export const PaginationContextProvider = PaginationContext.Provider;
