import { DesignComp } from "./DesignComp";
import { dataTransform } from "./versions/1.0.0";

export class DesignTemp {
  version?: string;
  _id!: string;
  title = "";
  desc = "";
  pageStyle?: any;
  content: string[] = ["root"];
  thumbnail?: string;
  compMap: { [compId: string]: DesignComp } = {};

  constructor(data?: Partial<DesignTemp>) {
    if (!data) return;
    Object.assign(this, dataTransform(data));
    // 初始化DesignComp
    Object.entries(this.compMap).forEach(([key, value]) => {
      this.compMap[key] = new DesignComp(value);
    });
    // 初始化DesignComp的pid
    Object.values(this.compMap).forEach((comp) => {
      if (comp.children.default) {
        // comp.children.default = Array.from(new Set(comp.children.default));
      }
      const childIds = comp.getChildIds();
      childIds.forEach((cid) => {
        const childComp = this.compMap[cid];
        // Object.defineProperty(childComp, "pid", { value: comp.id });
      });
    });
  }
}
