import * as React from "react";
import * as R from "ramda";

import { getPathAttributes } from "@applicaster/zapp-react-native-utils/navigationUtils";
import { ScreenResolver } from "@applicaster/zapp-react-native-ui-components/Components/ScreenResolver";

import { useRoute } from "@applicaster/zapp-react-native-utils/reactHooks/navigation";

type Props = any;

/**
 * This component extracts the screenType & screenId based on location,
 * and reconciles data returned from hook plugins if needed
 * @param {Props}: see props above
 */
export const RouteManager = React.memo(function RouteManager(props: Props) {
  const { pathname, screenData } = useRoute();

  const pathAttributes = getPathAttributes({ pathname });

  if (!pathAttributes.length) {
    return null;
  }

  const { screenType, screenId } = R.last(pathAttributes);

  return (
    <ScreenResolver
      screenType={screenType}
      screenId={screenId}
      screenData={screenData}
      {...props}
    />
  );
});
