"use client";
'use strict';

var chunkI2NZGVBG_js = require('./chunk-I2NZGVBG.js');
var chunkYXN2K77G_js = require('./chunk-YXN2K77G.js');
var chunkS7KHTUHA_js = require('./chunk-S7KHTUHA.js');
var react = require('react');
var outline = require('@heroicons/react/24/outline');
var client = require('@datatechsolutions/windsock/client');
var i18n = require('@datatechsolutions/shared-domain/i18n');
var jsxRuntime = require('react/jsx-runtime');

var PlatformStateContext = react.createContext(null);
function PlatformStateProvider({ children, normalizeRole }) {
  const { user } = client.useAuth();
  const state = react.useMemo(() => {
    const organizationId = user?.organizationId ?? "";
    const actor = user ? {
      ...user,
      role: normalizeRole(user.role),
      organizationId
    } : null;
    return {
      currentOrganizationId: organizationId,
      actor
    };
  }, [user, normalizeRole]);
  return /* @__PURE__ */ jsxRuntime.jsx(PlatformStateContext.Provider, { value: state, children });
}
function usePlatformState() {
  const context = react.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 chunkI2NZGVBG_js.LOCALE_FLAGS?.[code];
}
function PlatformAppShell({
  appName,
  appGradient,
  appLogo,
  appIcon,
  dashboardIcon,
  navigationItems,
  pageOrder,
  modulePermissions,
  hasPermission,
  settingsModal,
  children
}) {
  const tCommon = chunkYXN2K77G_js.useTranslations("common");
  const { user, status, logout } = client.useAuth();
  usePlatformState();
  const { resolvedTheme, setTheme, dockPosition, dockAutoHide, dockEnabled } = client.usePlatformPreferences();
  const pathname = chunkS7KHTUHA_js.usePathname();
  const router = chunkS7KHTUHA_js.useRouter();
  const { history, openHistory } = chunkI2NZGVBG_js.useNotifications();
  const currentLocale = chunkYXN2K77G_js.useLocale();
  const localeMeta = i18n.LANGUAGE_META[currentLocale];
  const items = react.useMemo(() => navigationItems.filter((item) => {
    const permission = modulePermissions[item.id];
    if (!permission) return true;
    return hasPermission(permission);
  }), [navigationItems, modulePermissions, hasPermission]);
  const handleSignOut = react.useCallback(() => {
    void logout("/login");
  }, [logout]);
  const handleToggleTheme = react.useCallback(
    () => {
      setTheme(resolvedTheme === "dark" ? "light" : "dark");
    },
    [resolvedTheme, setTheme]
  );
  const handleNavigate = react.useCallback((href) => router.push(href), [router]);
  const handlePrefetch = react.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__ */ jsxRuntime.jsx(
    chunkI2NZGVBG_js.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__ */ jsxRuntime.jsx(outline.RectangleStackIcon, { className: "h-full w-full" }),
      appLogo,
      appGradient,
      dashboardIcon: dashboardIcon ?? outline.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
    }
  );
}

exports.PlatformAppShell = PlatformAppShell;
exports.PlatformStateProvider = PlatformStateProvider;
exports.usePlatformState = usePlatformState;
//# sourceMappingURL=chunk-3JJWPOK6.js.map
//# sourceMappingURL=chunk-3JJWPOK6.js.map