import { j as jsxRuntimeExports } from "./jsx-runtime-ClFauRgV.esm.js";
import { useMemo, useState, useCallback, useEffect } from "react";
import { u as useTheme, f as Box, a as AppBarWrapper, A as ActivityArea, C as ChatBot } from "./appbar-CF_DgfSS.esm.js";
import { S as Sidebar } from "./sidebar-pKJm8Xef.esm.js";
import { d as drawerWidth } from "./mui.constants-BFBzxaiA.esm.js";
import { u as useLanguage } from "./useLangauge-BaiyBPxd.esm.js";
import { at as getToken } from "./common-B0qL-cCR.esm.js";
import { m as getV1Settings } from "./api-BQ_tS-9F.esm.js";
import { useLocation } from "react-router-dom";
const DRAWER_STATE_KEY = "_is_drawer_open";
const SidebarLayout = (props) => {
  const theme = useTheme();
  const { currentLanguage } = useLanguage();
  const isRtl = useMemo(
    () => (currentLanguage == null ? void 0 : currentLanguage.direction) === "rtl",
    [currentLanguage]
  );
  const [forcedOpen, setForcedOpen] = useState(true);
  const [hoverOpen, setHoverOpen] = useState(false);
  const [isInitialized, setIsInitialized] = useState(false);
  const location = useLocation();
  const curPathName = useMemo(() => location.pathname, [location.pathname]);
  const isDrawerOpen = forcedOpen || !forcedOpen && hoverOpen;
  const handleDrawerOpen = useCallback(() => {
    setForcedOpen(true);
    setHoverOpen(false);
    try {
      window.localStorage.setItem(DRAWER_STATE_KEY, "true");
    } catch (error) {
      console.warn("Failed to save drawer state to localStorage:", error);
    }
  }, []);
  const handleDrawerClose = useCallback(() => {
    setForcedOpen(false);
    window.localStorage.setItem("_is_drawer_open", "false");
  }, []);
  const [wipSettingsData, setWipSettingsData] = useState(null);
  const getSettings = useCallback(async () => {
    var _a, _b;
    const tokenData = getToken();
    const apiParams = {
      "x-token": tokenData["x-token"],
      "x-timezone": String(tokenData["x-timezone"])
    };
    const response = await getV1Settings(apiParams);
    const data = response && ((_b = (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.settings) == null ? void 0 : _b[0]) || null;
    setWipSettingsData(data);
    localStorage.setItem("wip_settings_data", JSON.stringify(data));
  }, []);
  useEffect(() => {
    if (curPathName == null ? void 0 : curPathName.includes("manufacturing")) {
      getSettings();
    }
  }, [getSettings, curPathName]);
  const [manufacturingSubmenu, setManufacturingSubmenu] = useState(
    props.menu
  );
  useEffect(() => {
    if (wipSettingsData && Array.isArray(props == null ? void 0 : props.menu)) {
      console.log("wipSettingsData: ", wipSettingsData);
      const _new = props.menu.filter((item) => {
        if (!(wipSettingsData == null ? void 0 : wipSettingsData.enable_production_planning)) {
          return item.label !== "Plans";
        } else {
          return item;
        }
      });
      setManufacturingSubmenu(_new);
    }
  }, [wipSettingsData, props.menu]);
  const handleMouseEnter = () => {
    if (!forcedOpen) {
      window.localStorage.setItem(DRAWER_STATE_KEY, "true");
    }
  };
  const handleMouseLeave = useCallback(() => {
    if (!forcedOpen && isInitialized) {
      setHoverOpen(false);
      window.localStorage.setItem(DRAWER_STATE_KEY, "false");
    }
  }, [forcedOpen, isInitialized]);
  const toggleDrawer = useCallback(() => {
    if (forcedOpen) {
      handleDrawerClose();
    } else {
      handleDrawerOpen();
    }
  }, [forcedOpen, handleDrawerClose, handleDrawerOpen]);
  useEffect(() => {
    const initializeDrawerState = () => {
      try {
        if (typeof window !== "undefined" && window.localStorage) {
          const savedState = window.localStorage.getItem(DRAWER_STATE_KEY);
          const shouldBeOpen = savedState === "true";
          setForcedOpen(shouldBeOpen);
          setHoverOpen(false);
          setIsInitialized(true);
        } else {
          setForcedOpen(true);
          setIsInitialized(true);
        }
      } catch (error) {
        console.warn("Failed to read drawer state from localStorage:", error);
        setForcedOpen(true);
        setIsInitialized(true);
      }
    };
    initializeDrawerState();
  }, []);
  useEffect(() => {
    const handleWindowFocus = () => {
      try {
        if (typeof window !== "undefined" && window.localStorage) {
          const savedState = window.localStorage.getItem(DRAWER_STATE_KEY);
          const shouldBeOpen = savedState === "true";
          if (shouldBeOpen !== forcedOpen) {
            setForcedOpen(shouldBeOpen);
            setHoverOpen(false);
          }
        }
      } catch (error) {
        console.warn("Failed to sync drawer state on window focus:", error);
      }
    };
    window.addEventListener("focus", handleWindowFocus);
    return () => {
      window.removeEventListener("focus", handleWindowFocus);
    };
  }, [forcedOpen]);
  if (!isInitialized) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      Box,
      {
        sx: {
          display: "flex",
          minHeight: "100vh",
          alignItems: "center",
          justifyContent: "center"
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { children: "Loading..." })
      }
    );
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    Box,
    {
      sx: {
        display: "flex",
        minHeight: "100vh",
        direction: isRtl ? "rtl" : "ltr"
      },
      children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          AppBarWrapper,
          {
            open: forcedOpen,
            handleDrawerOpen,
            handleDrawerClose,
            toggleDrawer,
            width: drawerWidth,
            forcedOpen,
            hoverOpen
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          Sidebar,
          {
            open: forcedOpen,
            forcedOpen,
            hoverOpen,
            handleDrawerClose,
            handleDrawerOpen,
            toggleDrawer,
            width: drawerWidth,
            theme,
            menu: manufacturingSubmenu,
            handleMouseEnter,
            handleMouseLeave,
            isInitialized,
            dashboardPath: props.dashboardPath
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          ActivityArea,
          {
            isDrawerOpen,
            drawerWidth,
            forcedOpen,
            children: props.children
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(ChatBot, {})
      ]
    }
  );
};
export {
  SidebarLayout as S
};
//# sourceMappingURL=sidebarScreen-_Y6VGfuI.esm.js.map
