'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { useAsyncEffect } from '../../../../fd-toolbox/hooks/use-async-effect.js';
import { initialize, setupErrorHandling } from './ClientRoot.funcs.js';
import { useSidebarItems } from '../sidebar/sidebar-resources-provider.js';
import { useGlobalConfig } from '../../../meta/providers/global-provider/GlobalContext.js';
import { Toolbar } from '../toolbar/Toolbar.js';
import Sidebar from '../sidebar/Sidebar.js';
import { NarrowNavbar } from '../navbar/NarrowNavbar.js';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { toolbarConfig, toolbarLayoutTypes } from '../client-layout/client-layout-config.js';
import { useOptionalPromise } from '../../../../fd-toolbox/hooks/use-optional-promise.js';
import { defaultGlobalConfig } from '../../../meta/providers/global-provider/default-config.js';
import { refreshDefaultEditResourceConfigs } from '../../../meta/edit-resource/config/internal-edit-resource-configs.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function ClientRoot(t0) {
  const $ = c(20);
  const {
    children,
    userPermissions,
    sidebarItems,
    userSettings,
    visibleWorkspaces,
    notifications,
    userWorkspaces
  } = t0;
  const userPermissionValue = useOptionalPromise(userPermissions);
  const sidebarItemsValue = useOptionalPromise(sidebarItems);
  const [isLoading, setIsLoading] = useState(true);
  const {
    globalConfig,
    setGlobalConfig
  } = useGlobalConfig();
  const {
    initSidebarItems
  } = useSidebarItems();
  let t1;
  let t2;
  if ($[0] !== setGlobalConfig || $[1] !== userPermissionValue) {
    t1 = () => {
      setGlobalConfig(prev => ({
        ...prev,
        userPermissionList: userPermissionValue
      }));
    };
    t2 = [userPermissionValue, setGlobalConfig];
    $[0] = setGlobalConfig;
    $[1] = userPermissionValue;
    $[2] = t1;
    $[3] = t2;
  } else {
    t1 = $[2];
    t2 = $[3];
  }
  useEffect(t1, t2);
  let t3;
  let t4;
  if ($[4] !== initSidebarItems || $[5] !== sidebarItemsValue) {
    t3 = () => {
      initSidebarItems(sidebarItemsValue);
    };
    t4 = [sidebarItemsValue, initSidebarItems];
    $[4] = initSidebarItems;
    $[5] = sidebarItemsValue;
    $[6] = t3;
    $[7] = t4;
  } else {
    t3 = $[6];
    t4 = $[7];
  }
  useEffect(t3, t4);
  let t5;
  if ($[8] !== globalConfig.resourceListConfigs || $[9] !== setGlobalConfig || $[10] !== userPermissionValue) {
    t5 = async () => {
      await initialize(globalConfig.resourceListConfigs, setIsLoading, userPermissionValue);
      refreshDefaultEditResourceConfigs();
      setGlobalConfig(_temp);
    };
    $[8] = globalConfig.resourceListConfigs;
    $[9] = setGlobalConfig;
    $[10] = userPermissionValue;
    $[11] = t5;
  } else {
    t5 = $[11];
  }
  let t6;
  if ($[12] === Symbol.for("react.memo_cache_sentinel")) {
    t6 = [];
    $[12] = t6;
  } else {
    t6 = $[12];
  }
  useAsyncEffect(t5, t6);
  useEffect(_temp2);
  let t7;
  if ($[13] !== children || $[14] !== isLoading || $[15] !== notifications || $[16] !== userSettings || $[17] !== userWorkspaces || $[18] !== visibleWorkspaces) {
    t7 = !isLoading && jsxs(Fragment, {
      children: [jsx(Toolbar, {
        className: "flex-none",
        config: toolbarConfig,
        userSettings,
        visibleWorkspaces,
        notifications,
        userWorkspaces
      }), jsxs("div", {
        className: "relative flex grow-0 basis-full overflow-hidden",
        children: [jsx(Sidebar, {
          toolbarLayoutType: toolbarConfig.toolbarLayout
        }), jsx("div", {
          className: cn("mb-14 grow overflow-x-hidden overflow-y-auto sm:mb-0", toolbarConfig.toolbarLayout === toolbarLayoutTypes.default && "md:ml-14"),
          children
        })]
      }), jsx(NarrowNavbar, {
        className: "flex-none"
      })]
    });
    $[13] = children;
    $[14] = isLoading;
    $[15] = notifications;
    $[16] = userSettings;
    $[17] = userWorkspaces;
    $[18] = visibleWorkspaces;
    $[19] = t7;
  } else {
    t7 = $[19];
  }
  return t7;
}
function _temp2() {
  setupErrorHandling();
}
function _temp(prev_0) {
  return {
    ...prev_0,
    editResourceConfigs: defaultGlobalConfig.editResourceConfigs
  };
}
__name(ClientRoot, "ClientRoot");

export { ClientRoot };
