import React from "react";
import { View, StyleSheet } from "react-native";

import { Spinner } from "@applicaster/zapp-react-native-ui-components/Components/Spinner";
import { usePickFromState } from "@applicaster/zapp-react-native-redux/hooks";
import { useNavigation } from "@applicaster/zapp-react-native-utils/reactHooks/navigation";

type Props = {
  children: React.ReactChild;
};

const transparent = "rgba(0,0,0,0)";

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
    backgroundColor: transparent,
  },
});

export function Loader({ children }: Props) {
  const {
    appState: { appLaunched },
  } = usePickFromState(["appState"]);

  const { currentRoute } = useNavigation();

  if (appLaunched && currentRoute !== "/") {
    return children;
  }

  return (
    <View style={styles.container}>
      <Spinner size="large" />
    </View>
  );
}
