import { deleteVersion } from "../services/editService";
import { getItemDescriptor } from "../utils";
export function getDeleteVersionCommand({}) {
    return {
        id: "deleteVersion",
        execute: (context) => execute(context),
        label: "Delete Version",
        icon: "X",
        disabled: (context) => !context.editContext.item?.canLock ||
            context.editContext.item.version === 0 ||
            context.editContext.itemVersions.length === 0,
    };
}
async function execute(context) {
    if (!context.editContext.item)
        return;
    const descriptor = getItemDescriptor(context.editContext.item);
    let message = "Are you sure you want to delete version " +
        context.editContext.item.version +
        "?";
    const sessions = context.editContext.activeSessions.filter((x) => ((x.item?.id === descriptor.id &&
        x.item?.language === descriptor.language &&
        x.item?.version === descriptor.version) ||
        (x.page?.id === descriptor.id &&
            x.page?.language === descriptor.language &&
            x.page?.version === descriptor.version)) &&
        x.sessionId !== context.editContext.sessionId);
    if (sessions.length > 0) {
        context.editContext.showMessageDialog({
            header: "Delete Version",
            message: "This version is currently locked by " +
                sessions.map((x) => x.user.name).join(", ") +
                ". You cannot delete it.",
        });
        return;
    }
    if (context.editContext.itemVersions[context.editContext.itemVersions.length - 1]?.version === context.editContext.item.version) {
        message += " This is the latest version!";
    }
    context.editContext.confirm?.({
        header: "Delete Version",
        message,
        showCancel: true,
        accept: async () => {
            await deleteVersion(descriptor, context.editContext.sessionId);
            context.editContext.loadItem({
                ...descriptor,
                version: 0,
            });
        },
    });
}
//# sourceMappingURL=deleteVersionCommand.js.map