import { useMemo } from "react";
import type { NavParams, ScreenId } from "./types";
import type { WebLang } from "@/pages/web/types";
import { useProductStore } from "@/pages/product/store";

export const TAB_SCREENS: ScreenId[] = ["home", "businesses", "products", "chats", "profile"];

/**
 * Mobile / tablet app shell API.
 * Backed by the shared, URL-driven product store so the app and the
 * desktop website always stay on the same screen.
 */
export function useApp() {
  const store = useProductStore();

  return useMemo(
    () => ({
      screen: store.appScreen,
      params: store.appParams,
      navigate: (screen: ScreenId, params: NavParams = {}) => store.appNavigate(screen, params),
      jump: (screen: ScreenId, params: NavParams = {}) => store.appJump(screen, params),
      goBack: store.goBack,
      canGoBack: store.canGoBack,
      activeTab: TAB_SCREENS.includes(store.appScreen) ? store.appScreen : null,
      isGuest: store.isGuest,
      setIsGuest: (v: boolean) => store.setRole(v ? null : "customer"),
      cartCount: store.cartCount,
      addToCart: store.addToCart,
      favorites: store.favorites,
      toggleFavorite: store.toggleFavorite,
      products: store.products,
      saveProduct: store.saveProduct,
      setProductStatus: store.setProductStatus,
      requireAuth: (action?: () => void) => store.requireAuthApp(action),
      startAuth: (target: "login" | "signup") => store.startAuth(target),
      completeAuth: () => store.completeAuth("customer", "/home"),
      hasReturnTarget: store.returnTo !== null,
      guestOpen: store.guestOpen,
      closeGuest: store.closeGuest,
      lang: store.lang,
      setLang: (lang: WebLang) => store.setLang(lang),
      dir: store.dir,
    }),
    [store],
  );
}