import localForage from 'localforage';
import axios from 'axios';
// 单实例，避免冲突
const materialsStore = localForage.createInstance({
  name: 'meta2d-materials',
  storeName: 'materials',
  version: 1.0,
  description: '存储的是系统组件',
});

// subclass - subclass
export async function GetDBMaterials(subclass: string) {
  return materialsStore.getItem(subclass);
}

// subclass - subclass
// materials - the data of subclass
export function SetDBMaterials(subclass: string, materials: any[]) {
  return materialsStore.setItem(subclass, materials);
}

export function MaterialsGroup(data: any[], folders?: any[]) {
  const obj: any = {};
  if (!folders) {
    if (data.length === 0) {
      // 长度为0
      obj['Uncategorized(未分类)'] = [];
    } else {
      data.forEach((item: { folder: string }) => {
        if (!item.folder) {
          item.folder = 'Uncategorized(未分类)';
        }

        if (!obj[item.folder]) {
          obj[item.folder] = [item];
        } else {
          obj[item.folder].push(item);
        }
      });
    }
  } else {
    folders.forEach((name: string) => {
      obj[name] = [];
    });
    obj['Uncategorized(未分类)'] = [];
    data.forEach((item: { folder: string }) => {
      if (!obj[item.folder]) {
        obj['Uncategorized(未分类)'].push(item);
      } else {
        obj[item.folder].push(item);
      }
    });
  }

  const list: any[] = [];
  // tslint:disable-next-line:forin
  for (const key in obj) {
    list.push({ name: key, list: obj[key], show: true });
  }

  return list;
}

export async function getNetJsDiagram() {
  let arr: any[] = [
    { name: '箭头', id: 'f955205' },
    { name: '拓扑图未分类', id: '6cd3b234' },
    { name: '云', id: '406687' },
    { name: '网络设备', id: '3f264892' },
    { name: '电子产品', id: '768107e4' },
    { name: '楼宇', id: 'c109d4d' },
    { name: '物联网未分类', id: '33253fe' },
    { name: '逻辑门电路', id: '334c1c88' },
    { name: '电阻', id: '597dab3' },
    { name: '电容', id: '2181f192' },
    { name: '电感', id: '1dba554' },
    { name: '开关、转换器', id: '6b39d7ca' },
    { name: '二极管', id: '3180efea' },
    { name: '信号源', id: '6bd86063' },
    { name: '晶体管', id: '017c4d4' },
    { name: '仪表', id: '4e0df5d5' },
    { name: '电子显示设备', id: '68559dd4' },
    { name: '真空电子管', id: '181c247d' },
    { name: '电子波形', id: '42daf6' },
    { name: '转动与机械设备', id: '2a5e218e' },
    { name: '电子逻辑计算器', id: 'f9beb15' },
    { name: '电子传输/传播', id: '37f12dd3' },
    { name: '电子未分类', id: '54aa6d1' },
    { name: '液压符号', id: 'b03ed4e' },
    { name: '工具', id: '108095a' },
  ];
  globalThis.jsPensMap = {};
  const lists = await Promise.all(
    arr.map((item) => openNewMaterial(item.name, item))
  );
  arr.forEach((item, index) => {
    item.list = lists[index];
    item.show = true;
  });
  return arr;
}

async function openNewMaterial(subclass: string, group) {
  // const [{ GetDBMaterials, SetDBMaterials }] = await Promise.all([
  //   import('../services/material'),
  // ]);
  let materials: any = await GetDBMaterials(subclass);
  if (!materials || !materials.length) {
    materials = await axios.get('/api/tools', {
      params: {
        subClass: (group as any).id,
        pageCount: 1000,
      },
    });
    if (materials) {
      SetDBMaterials(group.name, materials);
    }
  }

  // const newList: any[] = [];
  materials.forEach((material: any) => {
    if (!material.fullname) {
      // fullname 不存在，采用其他方式
      // newList.push(material);
    } else {
      // newList.push({
      //   subClassName: material.subClassName,
      //   id: material.id,
      //   name: material.name,
      //   fullname: material.fullname,
      //   data: material.data,
      //   svg: material.svg,
      // });
    }
    globalThis.jsPensMap[material.fullname] = material.svg;
  });

  // (window as any).meta2dTools = materials;
  // (window as any).registerToolsNew();

  // (window as any).meta2dTools = undefined;

  // return newList;
}