"use client";
import { useAuth, usePlatformPreferences, useNotifications, PlatformShell, LOCALE_FLAGS } from './chunk-ZQRUAXGP.mjs';
import { useTranslations, useLocale } from './chunk-7VJ7CMMT.mjs';
import { usePathname, useRouter } from './chunk-QWG2FMUN.mjs';
import { createContext, useMemo, useContext, useCallback } from 'react';
import { HomeIcon, RectangleStackIcon } from '@heroicons/react/24/outline';
import { LANGUAGE_META } from '@datatechsolutions/shared-domain/i18n';
import { jsx } from 'react/jsx-runtime';

var PlatformStateContext = createContext(null);
function PlatformStateProvider({ children, normalizeRole }) {
  const { user } = useAuth();
  const state = useMemo(() => {
    const organizationId = user?.organizationId ?? "";
    const actor = user ? {
      ...user,
      role: normalizeRole(user.role),
      organizationId
    } : null;
    return {
      currentOrganizationId: organizationId,
      actor
    };
  }, [user, normalizeRole]);
  return /* @__PURE__ */ jsx(PlatformStateContext.Provider, { value: state, children });
}
function usePlatformState() {
  const context = useContext(PlatformStateContext);
  if (!context) throw new Error("usePlatformState must be used inside PlatformStateProvider");
  return context;
}
function resolveFlag(locale) {
  const code = locale === "pt-BR" ? "br" : locale;
  return LOCALE_FLAGS?.[code];
}
function PlatformAppShell({
  appName,
  appGradient,
  appLogo,
  appIcon,
  appIconRaw,
  dashboardIcon,
  navigationItems,
  pageOrder,
  modulePermissions,
  hasPermission,
  settingsModal,
  children
}) {
  const tCommon = useTranslations("common");
  const { user, status, logout } = useAuth();
  usePlatformState();
  const { resolvedTheme, setTheme, dockPosition, dockAutoHide, dockEnabled } = usePlatformPreferences();
  const pathname = usePathname();
  const router = useRouter();
  const { history, openHistory } = useNotifications();
  const currentLocale = useLocale();
  const localeMeta = LANGUAGE_META[currentLocale];
  const items = useMemo(() => navigationItems.filter((item) => {
    const permission = modulePermissions[item.id];
    if (!permission) return true;
    return hasPermission(permission);
  }), [navigationItems, modulePermissions, hasPermission]);
  const handleSignOut = useCallback(() => {
    void logout("/login");
  }, [logout]);
  const handleToggleTheme = useCallback(
    () => {
      setTheme(resolvedTheme === "dark" ? "light" : "dark");
    },
    [resolvedTheme, setTheme]
  );
  const handleNavigate = useCallback((href) => router.push(href), [router]);
  const handlePrefetch = useCallback((href) => router.prefetch(href), [router]);
  const shellUser = user ? {
    name: user.name ?? null,
    email: user.email ?? null,
    image: user.image ?? null,
    role: user.role ?? null,
    permissions: user.permissions,
    organizationId: user.organizationId ?? null
  } : null;
  return /* @__PURE__ */ jsx(
    PlatformShell,
    {
      items,
      launchpadPageOrder: pageOrder,
      user: shellUser,
      authStatus: status,
      onSignOut: handleSignOut,
      dockPosition,
      dockAutoHide,
      dockEnabled,
      resolvedTheme,
      onToggleTheme: handleToggleTheme,
      pathname,
      onNavigate: handleNavigate,
      onPrefetch: handlePrefetch,
      appName,
      appIcon: appIcon ?? /* @__PURE__ */ jsx(RectangleStackIcon, { className: "h-full w-full" }),
      appIconRaw,
      appLogo,
      appGradient,
      dashboardIcon: dashboardIcon ?? HomeIcon,
      notificationCount: history.length,
      onNotifications: openHistory,
      labels: {
        home: tCommon("home"),
        profile: tCommon("profile"),
        signOut: tCommon("signOut"),
        signOutConfirmTitle: tCommon("signOutConfirmTitle"),
        cancel: tCommon("cancel"),
        notifications: tCommon("notifications"),
        theme: tCommon("theme"),
        currentTheme: resolvedTheme === "dark" ? tCommon("darkMode") : tCommon("lightMode"),
        language: tCommon("language"),
        currentLocale: localeMeta?.nativeName ?? currentLocale,
        localeFlag: resolveFlag(currentLocale),
        actions: tCommon("actions")
      },
      ...settingsModal ? { profileDrawer: settingsModal } : {},
      children
    }
  );
}

export { PlatformAppShell, PlatformStateProvider, usePlatformState };
//# sourceMappingURL=chunk-OVQ4MKCV.mjs.map
//# sourceMappingURL=chunk-OVQ4MKCV.mjs.map