import hoistNonReactStatics from "hoist-non-react-statics";
import * as React from "react";
import { SlideshowContext, SlideshowContextType } from "./SlideshowContext";

export type SlideshowProps = {
  room: any;
};

const withSlideshow = <P extends object>(Component: React.ComponentType<P>) => {
  type WrappedComponentPropsExceptProvided = Exclude<
    keyof P,
    keyof SlideshowProps
  >;
  type ForwardedProps = Pick<P, WrappedComponentPropsExceptProvided>;
  return hoistNonReactStatics(
    class WithShortcutComponent extends React.Component<ForwardedProps> {
      render() {
        return (
          <SlideshowContext.Consumer>
            {(SlideshowContext: SlideshowContextType) => {
              const { room } = SlideshowContext;
              return (
                <>
                  <Component {...(this.props as P)} room={room} />
                </>
              );
            }}
          </SlideshowContext.Consumer>
        );
      }
    },
    Component
  );
};

export default withSlideshow;
