"use strict";
const jsxRuntime = require("./jsx-runtime-B6vEU3CM.js");
const React = require("react");
const appbar = require("./appbar-BjvK6O9Q.js");
const sidebar = require("./sidebar-D1jgDNK3.js");
const mui_constants = require("./mui.constants-CJEIUmWO.js");
const useLangauge = require("./useLangauge-DXuEM0oW.js");
const common = require("./common-B47boGYI.js");
const api = require("./api-BygscvSz.js");
const reactRouterDom = require("react-router-dom");
const DRAWER_STATE_KEY = "_is_drawer_open";
const SidebarLayout = (props) => {
  const theme = appbar.useTheme();
  const { currentLanguage } = useLangauge.useLanguage();
  const isRtl = React.useMemo(
    () => (currentLanguage == null ? void 0 : currentLanguage.direction) === "rtl",
    [currentLanguage]
  );
  const [forcedOpen, setForcedOpen] = React.useState(true);
  const [hoverOpen, setHoverOpen] = React.useState(false);
  const [isInitialized, setIsInitialized] = React.useState(false);
  const location = reactRouterDom.useLocation();
  const curPathName = React.useMemo(() => location.pathname, [location.pathname]);
  const isDrawerOpen = forcedOpen || !forcedOpen && hoverOpen;
  const handleDrawerOpen = React.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 = React.useCallback(() => {
    setForcedOpen(false);
    window.localStorage.setItem("_is_drawer_open", "false");
  }, []);
  const [wipSettingsData, setWipSettingsData] = React.useState(null);
  const getSettings = React.useCallback(async () => {
    var _a, _b;
    const tokenData = common.getToken();
    const apiParams = {
      "x-token": tokenData["x-token"],
      "x-timezone": String(tokenData["x-timezone"])
    };
    const response = await api.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));
  }, []);
  React.useEffect(() => {
    if (curPathName == null ? void 0 : curPathName.includes("manufacturing")) {
      getSettings();
    }
  }, [getSettings, curPathName]);
  const [manufacturingSubmenu, setManufacturingSubmenu] = React.useState(
    props.menu
  );
  React.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 = React.useCallback(() => {
    if (!forcedOpen && isInitialized) {
      setHoverOpen(false);
      window.localStorage.setItem(DRAWER_STATE_KEY, "false");
    }
  }, [forcedOpen, isInitialized]);
  const toggleDrawer = React.useCallback(() => {
    if (forcedOpen) {
      handleDrawerClose();
    } else {
      handleDrawerOpen();
    }
  }, [forcedOpen, handleDrawerClose, handleDrawerOpen]);
  React.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();
  }, []);
  React.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__ */ jsxRuntime.jsxRuntimeExports.jsx(
      appbar.Box,
      {
        sx: {
          display: "flex",
          minHeight: "100vh",
          alignItems: "center",
          justifyContent: "center"
        },
        children: /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(appbar.Box, { children: "Loading..." })
      }
    );
  }
  return /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsxs(
    appbar.Box,
    {
      sx: {
        display: "flex",
        minHeight: "100vh",
        direction: isRtl ? "rtl" : "ltr"
      },
      children: [
        /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(
          appbar.AppBarWrapper,
          {
            open: forcedOpen,
            handleDrawerOpen,
            handleDrawerClose,
            toggleDrawer,
            width: mui_constants.drawerWidth,
            forcedOpen,
            hoverOpen
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(
          sidebar.Sidebar,
          {
            open: forcedOpen,
            forcedOpen,
            hoverOpen,
            handleDrawerClose,
            handleDrawerOpen,
            toggleDrawer,
            width: mui_constants.drawerWidth,
            theme,
            menu: manufacturingSubmenu,
            handleMouseEnter,
            handleMouseLeave,
            isInitialized,
            dashboardPath: props.dashboardPath
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(
          appbar.ActivityArea,
          {
            isDrawerOpen,
            drawerWidth: mui_constants.drawerWidth,
            forcedOpen,
            children: props.children
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(appbar.ChatBot, {})
      ]
    }
  );
};
exports.SidebarLayout = SidebarLayout;
//# sourceMappingURL=sidebarScreen-l04QZolj.js.map
