
import { basename } from '../../../base/common/resources.js';
import Severity$1 from '../../../base/common/severity.js';
import { localize } from '../../../nls.js';
import '../../../base/common/labels.js';
import '../../../base/common/platform.js';
import '../../../base/common/errors.js';

var DialogKind;
(function(DialogKind) {
    DialogKind[DialogKind["Confirmation"] = 1] = "Confirmation";
    DialogKind[DialogKind["Prompt"] = 2] = "Prompt";
    DialogKind[DialogKind["Input"] = 3] = "Input";
})(DialogKind || (DialogKind = {}));
class AbstractDialogHandler {
    getConfirmationButtons(dialog) {
        return this.getButtons(dialog, DialogKind.Confirmation);
    }
    getPromptButtons(dialog) {
        return this.getButtons(dialog, DialogKind.Prompt);
    }
    getInputButtons(dialog) {
        return this.getButtons(dialog, DialogKind.Input);
    }
    getButtons(dialog, kind) {
        const buttons = [];
        switch (kind) {
        case DialogKind.Confirmation:
            {
                const confirmationDialog = dialog;
                if (confirmationDialog.primaryButton) {
                    buttons.push(confirmationDialog.primaryButton);
                } else {
                    buttons.push(( localize(1905, "&&Yes")));
                }
                if (confirmationDialog.cancelButton) {
                    buttons.push(confirmationDialog.cancelButton);
                } else {
                    buttons.push(( localize(1906, "Cancel")));
                }
                break;
            }
        case DialogKind.Prompt:
            {
                const promptDialog = dialog;
                if (Array.isArray(promptDialog.buttons) && promptDialog.buttons.length > 0) {
                    buttons.push(...( promptDialog.buttons.map(button => button.label)));
                }
                if (promptDialog.cancelButton) {
                    if (promptDialog.cancelButton === true) {
                        buttons.push(( localize(1906, "Cancel")));
                    } else if (typeof promptDialog.cancelButton === "string") {
                        buttons.push(promptDialog.cancelButton);
                    } else {
                        if (promptDialog.cancelButton.label) {
                            buttons.push(promptDialog.cancelButton.label);
                        } else {
                            buttons.push(( localize(1906, "Cancel")));
                        }
                    }
                }
                if (buttons.length === 0) {
                    buttons.push(( localize(1907, "&&OK")));
                }
                break;
            }
        case DialogKind.Input:
            {
                const inputDialog = dialog;
                if (inputDialog.primaryButton) {
                    buttons.push(inputDialog.primaryButton);
                } else {
                    buttons.push(( localize(1907, "&&OK")));
                }
                if (inputDialog.cancelButton) {
                    buttons.push(inputDialog.cancelButton);
                } else {
                    buttons.push(( localize(1906, "Cancel")));
                }
                break;
            }
        }
        return buttons;
    }
    getDialogType(type) {
        if (typeof type === "string") {
            return type;
        }
        if (typeof type === "number") {
            return (type === Severity$1.Info) ? "info" : (type === Severity$1.Error) ? "error" : (type === Severity$1.Warning) ? "warning" : "none";
        }
        return undefined;
    }
    getPromptResult(prompt, buttonIndex, checkboxChecked) {
        const promptButtons = [...(prompt.buttons ?? [])];
        if (prompt.cancelButton && typeof prompt.cancelButton !== "string" && typeof prompt.cancelButton !== "boolean") {
            promptButtons.push(prompt.cancelButton);
        }
        let result = promptButtons[buttonIndex]?.run({
            checkboxChecked
        });
        if (!(result instanceof Promise)) {
            result = Promise.resolve(result);
        }
        return {
            result,
            checkboxChecked
        };
    }
}
var ConfirmResult;
(function(ConfirmResult) {
    ConfirmResult[ConfirmResult["SAVE"] = 0] = "SAVE";
    ConfirmResult[ConfirmResult["DONT_SAVE"] = 1] = "DONT_SAVE";
    ConfirmResult[ConfirmResult["CANCEL"] = 2] = "CANCEL";
})(ConfirmResult || (ConfirmResult = {}));
const MAX_CONFIRM_FILES = 10;
function getFileNamesMessage(fileNamesOrResources) {
    const message = [];
    message.push(...( fileNamesOrResources.slice(0, MAX_CONFIRM_FILES).map(
        fileNameOrResource => typeof fileNameOrResource === "string" ? fileNameOrResource : basename(fileNameOrResource)
    )));
    if (fileNamesOrResources.length > MAX_CONFIRM_FILES) {
        if (fileNamesOrResources.length - MAX_CONFIRM_FILES === 1) {
            message.push(( localize(1908, "...1 additional file not shown")));
        } else {
            message.push(( localize(
                1909,
                "...{0} additional files not shown",
                fileNamesOrResources.length - MAX_CONFIRM_FILES
            )));
        }
    }
    message.push("");
    return message.join("\n");
}

export { AbstractDialogHandler, ConfirmResult, getFileNamesMessage };
