import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { LabelDropRepo } from "../Entities/LabelDropRepo";
import { DispatchStateChangeUC } from "../../Dispatcher/UC/DispatchStateChangeUC";
import { UndoRedoEntity } from "../../UndoRedo/Entities/UndoRedoEntity";
import { ShowConfirmUC, } from "../../Dispatcher/UC/ShowConfirmUC";
export class EditLabelDropUC extends AppObjectUC {
}
EditLabelDropUC.type = "EditLabelDropUC";
EditLabelDropUC.get = (appObjects) => getSingletonComponent(EditLabelDropUC.type, appObjects);
export function makeEditLabelDropUC(appObject) {
    return new EditLabelDropUCImp(appObject);
}
class EditLabelDropUCImp extends EditLabelDropUC {
    get labelDropRepo() {
        return this.getCachedSingleton(LabelDropRepo.type);
    }
    get undo() {
        return this.getCachedSingleton(UndoRedoEntity.type);
    }
    get dispatchStateChange() {
        return this.getCachedSingleton(DispatchStateChangeUC.type)?.doDispatch;
    }
    constructor(appObject) {
        super(appObject, EditLabelDropUC.type);
        this.message = "Are you sure you want to discard changes made to this label drop answer?";
        this.title = "Discard Changes";
        this.confirmButton = "Discard";
        this.cancelButton = "Cancel";
        this.start = (labelDropID) => {
            if (!this.labelDropRepo)
                return;
            this.editingItem = labelDropID
                ? this.labelDropRepo.getForAppObject(labelDropID)
                : this.labelDropRepo.makeLabelDropItem();
            if (!this.editingItem) {
                this.warn("No label drop found for ID: " + labelDropID);
                this.clearEditLabelDrop();
                return;
            }
            this.initialData = this.editingItem.data;
            this.labelDropRepo.editingID = this.editingItem.id;
            this.labelDropRepo.isEditingNewAnswer = labelDropID ? false : true;
        };
        this.discard = () => {
            if (!this.isEditChanged()) {
                this.onConfirmDiscardEdits();
                return;
            }
            const showConfirm = ShowConfirmUC.get(this.appObjects);
            if (!showConfirm) {
                this.warn("Could not find ShowConfirmUC");
                return;
            }
            const dto = {
                cancelButtonLabel: this.cancelButton,
                confirmButtonLabel: this.confirmButton,
                message: this.message,
                title: this.title,
                confirmCallback: this.onConfirmDiscardEdits,
                cancelCallback: () => { },
            };
            showConfirm.showConfirm(dto);
        };
        this.isEditChanged = () => {
            if (!this.editingItem || !this.labelDropRepo || !this.initialData)
                return false;
            if (this.labelDropRepo?.isEditingNewAnswer)
                return true;
            const currentData = this.editingItem.data;
            if (currentData.label !== this.initialData.label)
                return true;
            if (currentData.correctAppObjectIDs.length !==
                this.initialData.correctAppObjectIDs.length)
                return true;
            for (const correctObjectId of currentData.correctAppObjectIDs) {
                if (!this.initialData.correctAppObjectIDs.includes(correctObjectId))
                    return true;
            }
            return false;
        };
        this.onConfirmDiscardEdits = () => {
            if (!this.editingItem || !this.labelDropRepo || !this.initialData)
                return;
            if (this.labelDropRepo.isEditingNewAnswer) {
                this.labelDropRepo.deleteItem(this.editingItem.id);
            }
            else {
                this.editingItem.data = this.initialData;
            }
            this.clearEditLabelDrop();
        };
        this.finish = () => {
            if (!this.isEditChanged()) {
                this.clearEditLabelDrop();
                return;
            }
            const repo = this.labelDropRepo;
            const initialData = this.initialData;
            const item = this.editingItem;
            const dispatchStateChange = this.dispatchStateChange;
            if (!repo || !initialData || !item || !dispatchStateChange)
                return;
            const finalData = item.data;
            if (repo.isEditingNewAnswer) {
                this.undo?.submitAction({
                    undoAction: () => {
                        repo.deleteItem(item.id);
                        dispatchStateChange();
                    },
                    redoAction: () => {
                        repo.makeLabelDropItem(item.id).data = finalData;
                        dispatchStateChange();
                    },
                });
            }
            else {
                this.undo?.submitAction({
                    undoAction: () => {
                        item.data = initialData;
                        dispatchStateChange();
                    },
                    redoAction: () => {
                        item.data = finalData;
                        dispatchStateChange();
                    },
                });
            }
            this.clearEditLabelDrop();
            dispatchStateChange();
        };
        this.clearEditLabelDrop = () => {
            if (!this.labelDropRepo)
                return;
            this.initialData = undefined;
            this.editingItem = undefined;
            this.labelDropRepo.editingID = undefined;
            this.labelDropRepo.isEditingNewAnswer = false;
        };
        this.appObjects.registerSingleton(this);
    }
}
//# sourceMappingURL=EditLabelDropUC.js.map