import { createContext, useContext } from "react";
import { IDataSource } from "../../lib/data-source/datasource.interface";

const DataSourceContext = createContext<IDataSource | null>(null);

export const DataSourceProvider = ({
  dataSource,
  children,
}: {
  dataSource: IDataSource;
  children: React.ReactNode;
}) => <DataSourceContext.Provider value={dataSource}>{children}</DataSourceContext.Provider>;

export const useDataSource = (): IDataSource => {
  const context = useContext(DataSourceContext);
  if (!context) {
    throw new Error("useDataSource debe usarse dentro de <DataSourceProvider>");
  }
  return context;
};
