import { FEAGlobals } from "../../../common/Globals";
const PROMPT_USER_ON_DIRTY_WORKSPACE = ["finsemble", "servicesConfig", "workspace", "promptUserOnDirtyWorkspace"];
const DISCARD_UNSAVED_CHANGES = ["finsemble", "servicesConfig", "workspace", "discardUnsavedChanges"];
function restartOrShutdown(isRestart, loseUnsavedChanges) {
    if (loseUnsavedChanges) {
        isRestart ? FEAGlobals.FSBL.restartApplication() : FEAGlobals.FSBL.shutdownApplication();
    }
    else {
        isRestart
            ? FEAGlobals.FSBL.Clients.WorkspaceClient.save(FEAGlobals.FSBL.restartApplication)
            : FEAGlobals.FSBL.Clients.WorkspaceClient.save(FEAGlobals.FSBL.shutdownApplication);
    }
}
export const openQuitConfirmationDialog = async (type = "Shutdown") => {
    var _a, _b;
    let { data: appname } = await FEAGlobals.FSBL.Clients.ConfigClient.get(["startup_app", "name"]);
    if (!appname)
        appname = "Finsemble";
    const { data } = await FEAGlobals.FSBL.Clients.ConfigClient.getMultiple([
        PROMPT_USER_ON_DIRTY_WORKSPACE,
        DISCARD_UNSAVED_CHANGES,
    ]);
    // we need to use join(".") because the path comes as string.property.subproperty
    const promptUserOnDirtyWorkspace = (_a = data[PROMPT_USER_ON_DIRTY_WORKSPACE.join(".")]) !== null && _a !== void 0 ? _a : true;
    const discardUnsavedChanges = (_b = data[DISCARD_UNSAVED_CHANGES.join(".")]) !== null && _b !== void 0 ? _b : false;
    // if we're not prompting the user on dirty workspace and
    // we do not want to discard unsaved changes - it means we're autosaving the workspace
    if (!promptUserOnDirtyWorkspace && !discardUnsavedChanges) {
        await FEAGlobals.FSBL.Clients.WorkspaceClient.save();
    }
    const isRestart = type === "Restart";
    // if we're not prompting the user on dirty workspace and want to discard unsaved changes
    // then we're going to lose all unsaved workspace changes
    const loseUnsavedChanges = !promptUserOnDirtyWorkspace && discardUnsavedChanges;
    const isWorkspaceDirty = loseUnsavedChanges
        ? false
        : await FEAGlobals.FSBL.Clients.WorkspaceClient.isWorkspaceDirty();
    let question = "";
    if (isRestart) {
        question = isWorkspaceDirty
            ? `Save changes to your workspace before restarting ${appname}? If you don't save, your changes will be lost`
            : `Are you sure you want to restart ${appname}? ${loseUnsavedChanges ? "All unsaved workspace changes will be lost" : ""}`;
    }
    else {
        question = isWorkspaceDirty
            ? `Save changes to your workspace before shutting down ${appname}? If you don't save, your changes will be lost`
            : `Are you sure you want to close ${appname}? ${loseUnsavedChanges ? "All unsaved workspace changes will be lost" : ""}`;
    }
    FEAGlobals.FSBL.Clients.DialogManager.open("YesNoDialog", {
        title: `${type} ${appname}?`,
        monitor: "primary",
        question: question,
        // show NegativeButton in case when workspace is dirty
        showNegativeButton: isWorkspaceDirty,
        negativeResponseLabel: "Don't Save",
        affirmativeResponseLabel: isWorkspaceDirty ? "Save" : null,
    }, (err, response) => {
        switch (response === null || response === void 0 ? void 0 : response.choice) {
            case "affirmative":
                restartOrShutdown(isRestart, loseUnsavedChanges);
                break;
            case "negative":
                FEAGlobals.FSBL.Clients.RouterClient.transmit("WorkspaceService.ignoreDirtyWorkspace", true);
                isRestart
                    ? FEAGlobals.FSBL.restartApplication({ bypassSaving: true })
                    : FEAGlobals.FSBL.shutdownApplication({ bypassSaving: true });
                break;
            default:
                // do nothing on Cancel
                break;
        }
    });
};
//# sourceMappingURL=openQuitConfirmationDialog.js.map