import React from "react";
import { WorkspaceSidebar } from "../WorkspaceSidebar/WorkspaceSidebar";
import DefaultSidebar from "./DefaultSidebar";
import { GlobalContext } from "@/context/Global";

const DashboardSidebar = ({
  list,
  setWorkspaces,
  activeLeftPanel,
  setActiveLeftPanel,
  activeWorkspaceId,
  setActiveWorkspaceId,
}) => {
  const {
    state: { path },
    dispatch: globalDispatch,
  } = React.useContext(GlobalContext);
  const [addWorkspaceOpen, setAddWorkspaceOpen] = React.useState(false);

  function handleSingleViewWorkspaceItemClick(path) {
    globalDispatch({
      type: "SETPATH",
      payload: {
        path,
      },
    });
  }

  function handleBackButton() {
    setActiveLeftPanel("default");
    globalDispatch({
      type: "SETPATH",
      payload: {
        path: "home",
      },
    });
  }

  const onCloseAddWorkspaceModal = React.useCallback(() => {
    setAddWorkspaceOpen(false);
  }, [addWorkspaceOpen]);

  function renderSidebar(activeState) {
    switch (activeState) {
      case "default":
        return (
          <DefaultSidebar
            list={list}
            setActiveLeftPanel={setActiveLeftPanel}
            setActiveWorkspaceId={setActiveWorkspaceId}
            setWorkspaces={setWorkspaces}
          />
        );
      case "workspace":
        return (
          <WorkspaceSidebar
            activeWorkspaceId={activeWorkspaceId}
            handleSidebarClick={handleSingleViewWorkspaceItemClick}
            handleBackButton={handleBackButton}
          />
        );
      case "hidden":
        return;

      default:
        return <DefaultSidebar />;
    }
  }

  return renderSidebar(activeLeftPanel);
};

export default DashboardSidebar;
