import {TreeRoleMenuData} from '@models';
import {MENU_STORAGE} from '@utils';
import {createStore} from 'zustand/vanilla';

type State = {menus: TreeRoleMenuData[]};

type Action = {
  setMenu(state: TreeRoleMenuData[]): void;
  clear(): void;
};

export const menuStore = createStore<State & Action>(function (set) {
  return {
    menus: [],
    setMenu(menus) {
      sessionStorage.setItem(MENU_STORAGE, JSON.stringify(menus));
      set({menus});
    },
    clear() {
      set({menus: []});
    },
  };
});
