import { AppObjectUC } from "@vived/core";
import { DispatchStateChangeUC } from "../../Dispatcher";
import { Hideable } from "../Entities";
import { UndoRedoEntity } from "../../UndoRedo/Entities";
export class HideUC extends AppObjectUC {
    static get(id, appObjects) {
        const appObj = appObjects.get(id);
        if (appObj === undefined) {
            return undefined;
        }
        return appObj.getComponent(HideUC.type);
    }
}
HideUC.type = "HideUC";
export function makeHideUC(appObject) {
    return new HideUCImp(appObject);
}
class HideUCImp extends HideUC {
    get hideable() {
        return this.getCachedLocalComponent(Hideable.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type)?.doDispatch;
    }
    get undoRedo() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    constructor(appObject) {
        super(appObject, HideUC.type);
        this.hide = () => {
            if (!this.hideable || this.hideable.isHidden) {
                return;
            }
            this.hideable.isHidden = true;
            if (this.dispatchStateChange)
                this.dispatchStateChange();
            this.submitUndo();
        };
        this.toggleIsHidden = () => {
            if (!this.hideable) {
                return;
            }
            this.hideable.isHidden = !this.hideable.isHidden;
            if (this.dispatchStateChange)
                this.dispatchStateChange();
            this.submitUndo();
        };
        this.submitUndo = () => {
            const hideable = this.hideable;
            const dispatcher = this.dispatchStateChange;
            if (!hideable || !dispatcher || !this.undoRedo) {
                return;
            }
            const undoState = !hideable.isHidden;
            const action = {
                undoAction: () => {
                    hideable.isHidden = undoState;
                    dispatcher();
                },
                redoAction: () => {
                    hideable.isHidden = !undoState;
                    dispatcher();
                },
            };
            this.undoRedo.submitAction(action);
        };
    }
}
//# sourceMappingURL=HideUC.js.map