import { createContext, useContext } from "solid-js";
import type { Accessor } from "solid-js";

interface NavigationMenuContextValue {
  value: Accessor<string>;
  setValue: (value: string) => void;
  orientation: Accessor<"horizontal" | "vertical">;
  scheduleOpen: (itemValue: string) => void;
  cancelAndClose: () => void;
}

const NavigationMenuContext = createContext<NavigationMenuContextValue>();

/** Returns the NavigationMenu root context. Throws if used outside <NavigationMenu>. */
export function useNavigationMenuContext(): NavigationMenuContextValue {
  const ctx = useContext(NavigationMenuContext);
  if (!ctx) throw new Error("[PettyUI] NavigationMenu parts must be used within <NavigationMenu>.");
  return ctx;
}

export { NavigationMenuContext };

interface NavigationMenuItemContextValue {
  value: string;
  isActive: Accessor<boolean>;
}

const NavigationMenuItemContext = createContext<NavigationMenuItemContextValue>();

/** Returns the NavigationMenu item context. Throws if used outside <NavigationMenu.Item>. */
export function useNavigationMenuItemContext(): NavigationMenuItemContextValue {
  const ctx = useContext(NavigationMenuItemContext);
  if (!ctx) throw new Error("[PettyUI] NavigationMenu.Trigger/Content must be inside <NavigationMenu.Item>.");
  return ctx;
}

export { NavigationMenuItemContext };
