import { queenApi } from "queenjs";
import { ResourceModule } from "..";
import { cloneDeep } from "lodash";

export const editorActions = ResourceModule.action({
  async queryTplsDetail(id) {
    const res = await this.https.queryTplsDetail(id);
    this.store.setSourceDetail(res.result);
    this.actions.initTreeData();
  },

  initTreeData() {
    const data: any = [];
    this.store.sourceDetail.webEditor?.meshSlots?.forEach((mesh: any) => {
      mesh.children = this.store.sourceDetail.webEditor?.matSlots?.filter(
        (mat: any) => mat.meshSlotId == mesh.Id
      );
      data.push(cloneDeep(mesh));
    });
    this.store.setTreeData(data);
  },

  async submitRender(id: string, images: any, videos: any) {
    queenApi.showLoading("任务提交中");

    console.log("iamges=>", images, "videos->", videos);

    try {
      await this.https.sourceGen({
        genRequest: {
          templateId: id,
          videos: videos,
          images: images,
          matSlots: this.store.matSlots,
          meshSlots: this.store.meshSlots,
        },
      });
      queenApi.messageSuccess("任务提交成功");
      return true;
    } catch (error) {
      console.error(error);
    } finally {
      queenApi.hideLoading();
    }
  },
});
