"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeleteAllLabelDropUC = void 0;
exports.makeDeleteAllLabelDropUC = makeDeleteAllLabelDropUC;
const core_1 = require("@vived/core");
const DispatchStateChangeUC_1 = require("../../Dispatcher/UC/DispatchStateChangeUC");
const UndoRedoEntity_1 = require("../../UndoRedo/Entities/UndoRedoEntity");
const LabelDropRepo_1 = require("../Entities/LabelDropRepo");
class DeleteAllLabelDropUC extends core_1.AppObjectUC {
    static get(appObjects) {
        return (0, core_1.getSingletonComponent)(DeleteAllLabelDropUC.type, appObjects);
    }
}
exports.DeleteAllLabelDropUC = DeleteAllLabelDropUC;
DeleteAllLabelDropUC.type = "DeleteAllLabelDropUC";
function makeDeleteAllLabelDropUC(appObject) {
    return new DeleteAllLabelDropUCImp(appObject);
}
class DeleteAllLabelDropUCImp extends DeleteAllLabelDropUC {
    get repo() {
        return this.getCachedSingleton(LabelDropRepo_1.LabelDropRepo.type);
    }
    get undo() {
        return this.getCachedSingleton(UndoRedoEntity_1.UndoRedoEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC_1.DispatchStateChangeUC.type)?.doDispatch;
    }
    delete() {
        const repo = this.repo;
        const dispatchStateChange = this.dispatchStateChange;
        if (!repo || !dispatchStateChange) {
            this.error("Missing Component");
            return;
        }
        const allData = repo.getAll().map((item) => item.data);
        if (allData.length === 0)
            return;
        repo.deleteAllItems();
        this.undo?.submitAction({
            undoAction: () => {
                allData.forEach((data) => {
                    repo.makeLabelDropItem(data.id).data = data;
                });
                dispatchStateChange();
            },
            redoAction: () => {
                allData.forEach((data) => {
                    repo.deleteItem(data.id);
                });
                dispatchStateChange();
            },
        });
        dispatchStateChange();
    }
    constructor(appObject) {
        super(appObject, DeleteAllLabelDropUC.type);
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=DeleteAllLabelDropUC.js.map