import { computed, ref } from 'vue';
import { getShopSpaceList, getCamerasByWorkShopId } from '@/api/scene/scene';
import { CompanyInfoItem } from '@/types/scene/type.ts';
import { calculateTreeData, ElTreeItem, TreeProps } from '@/utils';

export function useSceneInfos() {
  const scenesInfos = ref<CompanyInfoItem[]>([]);
  const scenesTree = ref<ElTreeItem[]>([]);

  const flattenedWorkshops = computed(() => {
    return scenesInfos.value.reduce((total, next) => [...total, ...next.children], []);
  });

  const flattendWorkspaces = computed(() => {
    return scenesInfos.value.reduce((total, next) => {
      let newArr: any[] = [];
      if (next.children && next.children.length) {
        newArr = next.children.reduce((subTotal, subNext) => [...subTotal, ...subNext.children], []);
      }
      return [...total, ...newArr];
    }, []);
  });

  const getScenesTree = (treeProps: TreeProps) => {
    getShopSpaceList().then((res) => {
      scenesInfos.value = res;
      scenesTree.value = calculateTreeData(res, treeProps, 1);
    });
  };

  return {
    scenesInfos,
    scenesTree,
    flattenedWorkshops,
    flattendWorkspaces,
    getScenesTree,
    calculateTreeData,
  };
}

export default useSceneInfos;
