import * as R from "ramda";
import {
  ORIENTATIONS,
  getScreenOrientation,
} from "@applicaster/zapp-react-native-utils/appUtils/orientationHelper";

const isSceneWebview = (scene): boolean => {
  const type = R.pathOr(
    "unknown",
    ["children", "props", "screenData", "targetScreen", "type"],
    scene
  );

  return type === "webview_screen_qb";
};

const isSceneLandscape = ({ scene, layoutData }) => {
  const screenData = scene?.children?.props?.screenData;

  const orientation = getScreenOrientation({
    screenData,
    layoutData,
  });

  return [
    ORIENTATIONS.landscapeLeft,
    ORIENTATIONS.landscapeRight,
    ORIENTATIONS.landscapeSensor,
  ].includes(orientation);
};

export const shouldSkipAnimationForPreviousWebViewScene = ({
  previousScene,
  nextScene,
  layoutData,
}): boolean => {
  // while opening web-based screen via hooks we manipulate scenes-state and replace last scene
  // it leads that we loose this next scene from scenes-state

  return (
    isSceneWebview(previousScene) &&
    R.isNil(nextScene) &&
    isSceneLandscape({ scene: previousScene, layoutData })
  );
};

export const getSceneByRoute = ({ route, scenes }) =>
  R.find(R.propEq("key", route), scenes);
