// store/modules/app.js
import Cookies from "js-cookie";
import { defineStore } from "pinia";

const useAppStore = defineStore("app", {
  state: () => ({
    sidebar: {
      opened: Cookies.get("sidebarStatus")
        ? !!+Cookies.get("sidebarStatus")
        : true,
      withoutAnimation: false,
      isCollapse: false,
      hide: false,
    },
    size: Cookies.get("size") || "default",
  }),

  actions: {
    // 切换侧边栏的展开/收起
    toggleSideBar(withoutAnimation) {
      if (this.sidebar.hide) {
        return false;
      }
      this.sidebar.opened = !this.sidebar.opened;
      this.sidebar.isCollapse = !this.sidebar.isCollapse;
      this.sidebar.withoutAnimation = withoutAnimation;

      if (this.sidebar.opened) {
        Cookies.set("sidebarStatus", 1);
      } else {
        Cookies.set("sidebarStatus", 0);
      }
    },

    // 收起侧边栏
    collapseSideBar({ withoutAnimation }) {
      if (this.sidebar.hide) return false;
      this.sidebar.isCollapse = true;
      this.sidebar.opened = false;
      this.sidebar.withoutAnimation = withoutAnimation;
      Cookies.set("sidebarStatus", 0);
    },

    // 展开侧边栏
    expandSideBar({ withoutAnimation }) {
      if (this.sidebar.hide) return false;
      this.sidebar.isCollapse = false;
      this.sidebar.opened = true;
      this.sidebar.withoutAnimation = withoutAnimation;
      Cookies.set("sidebarStatus", 1);
    },

    toggleSideBarHide(status) {
      this.sidebar.hide = status;
    },
  },

  getters: {
    isSidebarCollapse: (state) => state.sidebar.isCollapse,
  },
});

export default useAppStore;
