import { set } from "lodash";
import { EditorModule } from "..";
import { DesignComp } from "../../objects/DesignTemp/DesignComp";
import { CompObject } from "../../controllers/SelectCtrl/compObj";

export const manualActions = EditorModule.action({
  pickComp(compId: string) {
    if (this.store.currCompId == compId) return;

    const { historyCtrl } = this.controls;
    const isActionRoot = !historyCtrl.historyActionDoing;

    if (isActionRoot) historyCtrl.historyActionDoing = true;
    historyCtrl.historyCombine = true;
    this.store.setCurrComp(compId);
    historyCtrl.historyCombine = false;
    if (isActionRoot) historyCtrl.historyActionDoing = false;
    
    const paths = this.helper.getCompTrees(compId)
    const cardChilds = paths[2];
    if (cardChilds) {
        this.controls.selectCtrl.selecteObjs([new CompObject(cardChilds)]);
    }
  },
  updateCompData(comp: DesignComp, path: string, value: any) {
    const { historyCtrl } = this.controls;
    historyCtrl.historyActionDoing = true;
    historyCtrl.historyCombine = true;
    set(comp, path, value);
    historyCtrl.historyCombine = false;
    historyCtrl.historyActionDoing = false;
  },
  submitUpdate() {
    this.controls.historyCtrl.history.submit();
  },
});
