"use client";

import { createContext, ReactNode, useContext, useState } from "react";

import { IFunnelPaymentVariant } from "@/entities/session/funnel/types";

interface ProductSelectionContextType {
  selectedProduct: IFunnelPaymentVariant | null;
  setSelectedProduct: (product: IFunnelPaymentVariant | null) => void;
}

const ProductSelectionContext = createContext<
  ProductSelectionContextType | undefined
>(undefined);

interface ProductSelectionProviderProps {
  children: ReactNode;
}

export function ProductSelectionProvider({
  children,
}: ProductSelectionProviderProps) {
  const [selectedProduct, setSelectedProduct] =
    useState<IFunnelPaymentVariant | null>(null);

  return (
    <ProductSelectionContext.Provider
      value={{ selectedProduct, setSelectedProduct }}
    >
      {children}
    </ProductSelectionContext.Provider>
  );
}

export function useProductSelection() {
  const context = useContext(ProductSelectionContext);
  if (!context) {
    throw new Error(
      "useProductSelection must be used within ProductSelectionProvider"
    );
  }
  return context;
}
