import api from "@/axios";
import { NTag } from "naive-ui";
import type { AuthMenuTree } from "@/stores/permission";
import type { TreeOption } from "naive-ui";
import { Icon } from "@/components";
import { isEmpty } from "lodash-es";
const convertTreeOptions = (data: AuthMenuTree[]): TreeOption[] => {
  return data.map((item) => {
    const opt: TreeOption = {
      label: item.name,
      key: item.snowFlakeId,
      prefix: () => <Icon name={item.icon}></Icon>,
      suffix: () => {
        switch (item.type.value) {
          case "dir":
            return <NTag type="info">目录</NTag>;
          case "menu":
            return <NTag type="success">菜单</NTag>;
          case "btn":
            return <NTag type="warning">按钮</NTag>;
        }
      },
      // item.type.value === "dir" ? (
      //   <NTag type="info">目录</NTag>
      // ) : (
      //   <NTag type="success">菜单</NTag>
      // ),
    };
    if (!isEmpty(item.children)) {
      opt.children = convertTreeOptions(item.children as AuthMenuTree[]);
    }
    return opt;
  });
};

export const getTreeOptions = async () => {
  const treeData = await api.get("/management/menu/menuTableTree");
  const menus: TreeOption[] = convertTreeOptions(
    treeData.data as AuthMenuTree[]
  );
  return menus;
};
