import { resolveNavigationPlugin } from "@applicaster/zapp-react-native-utils/navigationUtils";

export const BOTTOM_TABS_PLUGIN_ID = "quick-brick-bottom-tabs";

export const SIDE_MENU_PLUGIN_ID = "quick_brick_side_menu";

export function getMenuPlugin(screenData, plugins) {
  return resolveNavigationPlugin({
    category: "menu",
    navigations:
      screenData?.navigations || screenData?.targetScreen?.navigations,
    plugins,
  });
}

function shouldShowNavigation(route, screenData) {
  if (route.includes("playable")) {
    return false;
  }

  if (
    screenData.type === "qb_search_screen" ||
    screenData?.targetScreen?.type === "qb_search_screen"
  ) {
    return false;
  }

  if (screenData?.hookPlugin) {
    const hookPlugin = screenData?.hookPlugin?.module;

    return hookPlugin?.showNavBar || hookPlugin?.presentFullScreen !== true;
  }

  if (
    screenData?.general?.allow_screen_plugin_presentation ||
    screenData?.targetScreen?.general?.allow_screen_plugin_presentation
  ) {
    return false;
  }

  return true;
}

/**
 * This function helps to decide whether the menu should be presented on the screen
 * based on route and / or screen Data
 *
 * is similar to the navbar, except that it covers scenarios where only the navbar (and not)
 * the menu will be hidden
 *
 * - playable screens
 * - qb_search_screen
 * - screen hooks that specify showNavBar or presentFullScreen
 * - screens or nested screens that have allow_screen_plugin_presentation set to true
 *
 * @param {String} route current route of the screen
 * @param {Object} screenData payload associated with the currently presented screen
 * @returns {Boolean}
 */
export function isMenuVisible(route, screenData, plugins) {
  const plugin = getMenuPlugin(screenData, plugins);

  const isBottomTabsPlugin = plugin?.identifier === BOTTOM_TABS_PLUGIN_ID;
  const isSideMenuPlugin = plugin?.identifier === SIDE_MENU_PLUGIN_ID;

  if (!isBottomTabsPlugin && !isSideMenuPlugin) {
    return false;
  }

  return shouldShowNavigation(route, screenData);
}

export function isBottomTabVisible(route, screenData, plugins) {
  const plugin = getMenuPlugin(screenData, plugins);

  const isBottomTabsPlugin = plugin?.identifier === BOTTOM_TABS_PLUGIN_ID;

  if (!isBottomTabsPlugin) {
    return false;
  }

  return shouldShowNavigation(route, screenData);
}
