import { IKnowledge, Level } from '../../src/types';

const data = [
  {
    id: '1',
    name: '扇形面积1',
    parentId: '123123123',
    folded: 0,
    photoUrl: '',
    children: [
      {
        id: '11',
        name: '公式',
        parentId: '1',
        folded: 0,
        photoUrl: '',
        children: [
          {
            id: '111',
            name: '$S=\\frac{n\\pi R^2}{360}$',
            parentId: '11',
            folded: 1,
            photoUrl: '',
            children: [],
          },
          {
            id: '112',
            name: '$S=\\frac{lr}{2}$',
            parentId: '11',
            folded: 0,
            photoUrl: '',
            children: [],
          },
        ],
      },
    ],
  },
  {
    id: '2',
    name: '扇形面积2',
    parentId: 'qwe',
    folded: 0,
    photoUrl: '',
    children: [
      {
        id: '21',
        name: '公式',
        parentId: '2',
        folded: 0,
        photoUrl: '',
        children: [
          {
            id: '211',
            name: '$S=\\frac{n\\pi R^2}{360}$',
            parentId: '21',
            folded: 0,
            photoUrl: '',
            children: [],
          },
          {
            id: '212',
            name: '$S=\\frac{lr}{2}$',
            parentId: '21',
            folded: 0,
            photoUrl: '',
            children: [],
          },
        ],
      },
      {
        id: '22',
        name: '公式2',
        parentId: '2',
        folded: 0,
        photoUrl: '',
        children: [
          {
            id: '221',
            name: '$S=\\frac{n\\pi R^2}{360}$',
            parentId: '22',
            folded: 0,
            photoUrl: '',
            children: [],
          },
          {
            id: '222',
            name: '$S=\\frac{lr}{2}$',
            parentId: '22',
            folded: 1,
            photoUrl: '',
            children: [],
          },
        ],
      },
    ],
  },
];

interface INode {
  id: string;
  name: string;
  parentId: string;
  folded: number;
  photoUrl: string;
  children: INode[];
}

export function add(currentLevel: Level) {
  switch (currentLevel) {
    case Level.First:
      return Level.Second;
    case Level.Second:
      return Level.Third;
    case Level.Third:
    default:
      return Level.Third;
  }
}

function formatNodeTree(node: INode, currentLevel: Level): IKnowledge {
  const knowledge = {
    id: node.id,
    topic: node.name || node.photoUrl,
    level: currentLevel,
    direction: 'side',
    needHighlight: node.folded === 1 && currentLevel === Level.Third ? true : false,
    children: [],
    expanded: false,
  } as IKnowledge;
  if (node.children && node.children.length > 0) {
    // 先序遍历生成节点
    knowledge.children = node.children.map((cNode) => formatNodeTree(cNode, add(currentLevel)));
  }
  // 根据子节点判断父节点是否展开
  // 若当前层级的子节点存在高亮状态或者存在展开的，则当前层级展开
  // 其余情况收起
  if (
    knowledge.children &&
    knowledge.children.length > 0 &&
    knowledge.children.some((knowledge) => knowledge.expanded === true || knowledge.needHighlight === true)
  ) {
    knowledge.expanded = true;
  } else {
    knowledge.expanded = false;
  }
  return knowledge;
}

export default data.map((n) => formatNodeTree(n, Level.First));
