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

import { getPathAttributes } from "@applicaster/zapp-react-native-utils/navigationUtils";
import { useRoute } from "@applicaster/zapp-react-native-utils/reactHooks";
import { ScreenResolver } from "../ScreenResolver";
import { TestId } from "./TestId";

type Props = { pathname: string; screenData: Record<string, 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 function RouteManager(props: Props) {
  const { pathname, screenData } = useRoute();
  const pathAttributes = getPathAttributes({ pathname });

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

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