import { EditorModule } from "..";
import { CompObject } from "../../controllers/SelectCtrl/compObj";
import { DesignComp } from "../../objects/DesignTemp/DesignComp";
import { createProxyEffect } from "../../objects/ProxyStore/create";
import { EditorMode } from "../../typings";
import { editActions } from "./edit";

export const initActions = EditorModule.action({
  // 模块初始化
  init() {
    const { historyCtrl } = this.controls;
    historyCtrl.bindActions(Object.keys(editActions));
    this.controls.compUICtrl.init();

    createProxyEffect(this.store, (type, paths, value, oldValue) => {
      if (paths[0] === "designData" || 
          paths[0] === "currCompId" || 
          paths[0] === "selected" || 
          paths[0] === "currStreamCardId" ||
          paths[0] === "selectId"
          ) {
        historyCtrl.record(this.store, type, paths, value, oldValue);
      }
    });
  },

  // 初始化数据
  async initDesign(id: string) {
    const ret = await this.https[
      this.store.isEditComp ? "getCompDetail" : "getDesignDetail"
    ](id);
    this.store.setDesignData(ret.result);
  },
  // 切换模式
  switchMode(v: EditorMode) {
    this.store.setMode(v);
  },

  onViewReady(pageEl, selEl, viewPort) {
    this.store.currStreamCardId = this.store.streamCardIds[0];
    this.controls.selectCtrl.initEvents(pageEl, selEl, viewPort);
  },

  onCompLayoutUpdated(comp: DesignComp) {
    if (this.helper.isCurrComp(comp.id)) {
      this.controls.selectCtrl.selecteObjs([new CompObject(comp)]);
    }
  },
});
