import { shallowRef, onMounted } from 'vue';
import { getFullCameraTree } from "@/api/camera/camera-preview";
import { PermissionViewTree } from "@/types/permission/type";
import { transformToViewTree } from '../utils/tree';

export default function useCameraPermissionTree() {
  const cameraPermTreeData = shallowRef<PermissionViewTree>([]);

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

  onMounted(() => buildPermissionTree());

  return { cameraPermTreeData };
}