import { MindMapProjectInfo, TopicItem } from "@/types";

import { theme1 } from "./theme1";
import { theme2 } from "./theme2";
import { theme3 } from "./theme3";
import { theme4 } from "./theme4";
import { theme5 } from "./theme5";

/**
 * 获取主题配置
 * @param key 
 * @param parent 
 * @returns 
 */
export const getTheme = (key?: string, parent?: TopicItem): Record<string, any> => {
  const map = {
    // 默认主题
    default: theme1(parent),
    theme2: theme2(),
    theme3: theme3(),
    theme4: theme4(),
    theme5: theme5(),
  }

  return map[key as keyof typeof map] || {};
};

/**
 * 切换主题
 * @param themeKey 主题key
 * @param mindProjectInfo 当前脑图信息
 */
export const changeTheme = (themeKey: string, mindProjectInfo: MindMapProjectInfo) => {
  const theme = getTheme(themeKey);
  mindProjectInfo.pageSetting.fill = theme.background.fill;
  mindProjectInfo.pageSetting.fillType = theme.background.fillType;
  mindProjectInfo.pageSetting.fillImageUrl = theme.background.fillImageUrl;
  const traverse = (topics: TopicItem[], parent?: TopicItem) => {
    topics.forEach((item) => {
      const theme = getTheme(themeKey, parent);
      item.fill = {
        ...item.fill,
        ...theme[item.type].fill,
      };
      item.text = {
        ...item.text,
        ...theme[item.type].text,
      };
      item.stroke = {
        ...item.stroke,
        ...theme[item.type].stroke,
      };
      item.edge = {
        ...item.edge,
        ...theme[item.type].edge,
      };
      item.borderSize = theme[item.type].borderSize;
      if (item.children) {
        traverse(item.children, item);
      }
    });
  };

  traverse(mindProjectInfo?.topics || []);

  return mindProjectInfo;
}