import { ProjectInfo } from "@/type";
import { DEFAULT_SETTING } from "@/constants";

export const initInfo = (info: ProjectInfo) => {
  // 配置
  info.setting = info?.setting
    ? JSON.parse(info.setting as unknown as string)
    : { ...DEFAULT_SETTING };

  info.tables.forEach((tableItem) => {
    // 名称多语言
    tableItem.table.langNameList = [
      { name: "en", value: tableItem.table?.enName || "" },
      { name: "zh-CN", value: tableItem.table?.cnName || "" },
    ];
    tableItem.table.langName = "";
    // 描述多语言
    tableItem.table.langDescriptionList = [
      { name: "en", value: tableItem.table?.enDescription || "" },
      { name: "zh-CN", value: tableItem.table?.cnDescription || "" },
    ];
    tableItem.table.langDescription = "";
    // 字段处理
    tableItem.tableColumnList.forEach((columnItem) => {
      // 名称多语言
      columnItem.langNameList = [
        { name: "en", value: columnItem?.enName || "" },
        { name: "zh-CN", value: columnItem?.cnName || "" },
      ];
      columnItem.langName = "";
    });
    tableItem.table.style = JSON.parse(
      tableItem.table.style as unknown as string
    );
    tableItem.tableColumnList = tableItem.tableColumnList.sort(
      (a, b) => a.isPreDefined ? -1 : (a.displayOrder || 0) - (b.displayOrder || 0)
    );
  });

  // json字符串转换
  info.relations.forEach((relationItem) => {
    relationItem.style = JSON.parse(relationItem.style as unknown as string);
  });
  info.topicAreas.forEach((topicAreaItem) => {
    topicAreaItem.style = JSON.parse(topicAreaItem.style as unknown as string);
  });
  info.remarkInfos.forEach((remarkItem) => {
    remarkItem.style = JSON.parse(remarkItem.style as unknown as string);
  });
};
