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

export default function useFunctionPermissionTree() {
  const funcPermTreeData = shallowRef<PermissionViewTree>([]);

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

  onMounted(() => buildPermissionTree());

  return { funcPermTreeData };
}