"use client";
'use strict';

var chunkMT66VKLS_js = require('./chunk-MT66VKLS.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 i18n = require('@datatechsolutions/shared-domain/i18n');
var jsxRuntime = require('react/jsx-runtime');

var PlatformStateContext = react.createContext(null);
function PlatformStateProvider({ children, normalizeRole }) {
  const { user } = chunkMT66VKLS_js.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 chunkMT66VKLS_js.LOCALE_FLAGS?.[code];
}
function PlatformAppShell({
  appName,
  appGradient,
  appLogo,
  appIcon,
  appIconRaw,
  dashboardIcon,
  navigationItems,
  pageOrder,
  modulePermissions,
  hasPermission,
  settingsModal,
  children
}) {
  const tCommon = chunkYXN2K77G_js.useTranslations("common");
  const { user, status, logout } = chunkMT66VKLS_js.useAuth();
  usePlatformState();
  const { resolvedTheme, setTheme, dockPosition, dockAutoHide, dockEnabled } = chunkMT66VKLS_js.usePlatformPreferences();
  const pathname = chunkS7KHTUHA_js.usePathname();
  const router = chunkS7KHTUHA_js.useRouter();
  const { history, openHistory } = chunkMT66VKLS_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(
    chunkMT66VKLS_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" }),
      appIconRaw,
      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-WJENX6KB.js.map
//# sourceMappingURL=chunk-WJENX6KB.js.map