import { CompUI } from "@/modules/editor/components/CompUI";
import { addCacheToMap } from "@/modules/editor/components/CompUI/defines/createCompId";
import { set } from "lodash";
import { DesignComp } from "../DesignComp";

export function dataTransform(data: any) {
  if (data.version == "1.0.0") {
    return data;
  }
  data.version = "1.0.0";

  if (!data.compMap) {
    data.compMap = {
        root: CompUI.Page.createComp({
            id: "root",
            compKey: "Page",
            layout: data.pageStyle,
            children: {
              default: [],
            },
          })
    }
    const card = CompUI.Page.createComp({
        compKey: "Container",
        children: {
          default: []
        },
        layout: {
            size: [750, 200],
            background: {
              color: "#ffffff",
            },
        },
    });
    data.compMap[card.id] = card;
    data.compMap.root.children.default = [card.id]
    return data
  }

  data.compMap || (data.compMap = {});
  const compMap: Record<string, DesignComp> = data.compMap;

  const childrens = compMap.root.children.default || []

  const roots = []
  for (const item of childrens) {
    let card = compMap[item]
    if (card.compKey != "Container" ) {
        card.layout.position = "absolute";
        card = CompUI.Page.createComp({
            compKey: "Container",
            children: {
              default: [card.id]
            },
        });
        compMap[card.id] = card;
    }
    roots.push( card.id );
  }
  compMap.root.children.default = roots;
  return data;
}