import { ElementType, PropsWithChildren } from "react";

type ProvidersType = [ElementType, Record<string, unknown>] | [ElementType];

export const combineProviders = (
    ...componentsWithProps: Array<ProvidersType>
): ElementType => {
    const getInitial = ({ children }: PropsWithChildren) => children;

    return componentsWithProps.reduce(
        (
            AccumulatedComponents: ElementType,
            [Provider, props = {}]: ProvidersType
        ) => {
            return ({ children }: PropsWithChildren) => {
                return (
                    <AccumulatedComponents>
                        <Provider {...props}>{children}</Provider>
                    </AccumulatedComponents>
                );
            };
        },
        getInitial
    );
};
