"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HideUC = void 0;
exports.makeHideUC = makeHideUC;
const core_1 = require("@vived/core");
const Dispatcher_1 = require("../../Dispatcher");
const Entities_1 = require("../Entities");
const Entities_2 = require("../../UndoRedo/Entities");
class HideUC extends core_1.AppObjectUC {
    static get(id, appObjects) {
        const appObj = appObjects.get(id);
        if (appObj === undefined) {
            return undefined;
        }
        return appObj.getComponent(HideUC.type);
    }
}
exports.HideUC = HideUC;
HideUC.type = "HideUC";
function makeHideUC(appObject) {
    return new HideUCImp(appObject);
}
class HideUCImp extends HideUC {
    get hideable() {
        return this.getCachedLocalComponent(Entities_1.Hideable.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(Dispatcher_1.DispatchStateChangeUC.type)?.doDispatch;
    }
    get undoRedo() {
        return this.getCachedSingleton(Entities_2.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