import React, { useContext } from "react";
import hoistNonReactStatics from "hoist-non-react-statics";
import { AnalyticsContext } from "./context";
import { AnalyticsContextValue } from "./types";

export function withAnalytics<P extends object>(
  WrappedComponent: React.ComponentType<P>
) {
  const WithAnalytics = (props: P & AnalyticsContextValue): JSX.Element => {
    const context = useContext(AnalyticsContext);
    return <WrappedComponent {...props} {...context} />;
  };
  return hoistNonReactStatics(WithAnalytics, WrappedComponent, {});
}
