import React from "react";
import { ActionSheetProvider } from "@expo/react-native-action-sheet";
import { SafeAreaProvider } from "react-native-safe-area-context";

import UserDataProvider from "./UserDataProvider";
import WebViewProvider from "./WebViewProvider";
import SplashAnimationProvider from "./SplashAnimationProvider";
import UniversalLinkProvider from "./UniversalLinkProvider";
import PremiumProvider from "./PremiumProvider";
import StripeEmbeddedProvider from "./StripeEmbeddedProvider";

const SharedProviders = ({ children }) => {
  const providers = [
    [SafeAreaProvider, {}],
    [ActionSheetProvider, {}],
    [SplashAnimationProvider, {}],
    [WebViewProvider, {}],
    [UserDataProvider, {}],
    [StripeEmbeddedProvider, {}],
    [PremiumProvider, {}],
    [UniversalLinkProvider, {}],
  ];

  // Dynamically nest the providers using reduce
  const Combined = providers.reduceRight(
    (AccumulatedChildren, [Provider, props]) => {
      return ({ children }) => (
        <Provider {...props}>
          <AccumulatedChildren>{children}</AccumulatedChildren>
        </Provider>
      );
    },
    ({ children }) => <>{children}</>
  );

  return <Combined>{children}</Combined>;
};

export default SharedProviders;
