import { shallowRef, onMounted } from 'vue';
import { queryOwnedMenuTree } from "@/api/system/menu";
import { PermissionViewTree } from "@/types/permission/type";
import { transformToViewTree } from '../utils/tree';

export default function useMenuPermissionTree() {
  const menuPermTreeData = shallowRef<PermissionViewTree>([]);

  /**
   * 生成权限树
   */
  const buildPermissionTree = async () => {
    try {
      let result = await queryOwnedMenuTree();
      menuPermTreeData.value = transformToViewTree(result, 'menuName', 'id');
    } catch (e) {
      console.error(e);
    }
  };

  onMounted(() => buildPermissionTree());

  return { menuPermTreeData };
}