"use client";
import HeaderWithSidebar from "@/components/layouts/dashboard/headerWithSidebar";
import CallWidget from "@/components/widget/call";
import WithWidgetMiddleware from "@/core/middlewares/withWidget";
import { SocketProvider } from "@/lib/contexts/socket";
import { usePermissions } from "@/lib/hooks/usePermissions";
import { Stack } from "@mui/material";
import { SWRConfig } from "swr";

const Layout = ({ children }) => {
  const { data: permissions } = usePermissions();
  const isCallWidgetEnabled = permissions?.some(
    (permission) => permission === "manage_calls",
  );

  return (
    <SocketProvider>
      <SWRConfig value={{ provider: () => new Map() }}>
        <Stack sx={{ width: "100%", height: "100%" }}>
          <HeaderWithSidebar>{children}</HeaderWithSidebar>
        </Stack>
      </SWRConfig>
      <WithWidgetMiddleware enable={isCallWidgetEnabled}>
        <CallWidget />
      </WithWidgetMiddleware>
    </SocketProvider>
  );
};
export default Layout;
