import * as R from "ramda";

import {
  getInflatedDataSourceUrl,
  getSearchContext,
} from "@applicaster/zapp-react-native-utils/reactHooks";

import { ZappPipesEntryContext } from "../../../Contexts";

export const getDatasourceUrl: (
  contexts: {
    screenData?: Record<string, any>;
    entryContext: ReturnType<
      typeof ZappPipesEntryContext.useZappPipesContext
    >[0];
    searchContext: string;
    screenContext: ZappRiver;
  },
  component?: ZappUIComponent
) => string | null = R.curry(
  (
    { screenData, entryContext, searchContext, screenContext },
    component: ZappUIComponent
  ) => {
    const { source, mapping } = R.propOr({}, ["data"], component);

    const contexts = {
      entry: entryContext,
      screen: screenContext || screenData,
      search: getSearchContext(searchContext, mapping),
    };

    return getInflatedDataSourceUrl({ source, mapping, contexts });
  }
);
