var PDJS;
(function (PDJS) {
    var Actions;
    (function (Actions) {
        class CreateEntityActions extends PDJS.Shared.Actions.ActionDefinition {
            constructor() {
                super({
                    id: "create-entity",
                    name: "Create Entity",
                    description: "Opens the Create Entity dialog.",
                    targetType: "ProspectDirector.Web.Manage.Modules.Actions.CreateEntityAction",
                    parameters: [
                        { id: "entityTypeId", type: PDJS.Core.Actions.ActionParameterType.integer },
                        { id: "parentId", type: PDJS.Core.Actions.ActionParameterType.integer }
                    ]
                });
            }
            execute(args) {
                PDJS.Entities.EntityDialogs.showCreate({
                    entityTypeId: args.entityTypeId,
                    parentId: args.parentId
                });
            }
        }
        Actions.CreateEntityActions = CreateEntityActions;
    })(Actions = PDJS.Actions || (PDJS.Actions = {}));
})(PDJS || (PDJS = {}));
var PDJS;
(function (PDJS) {
    var Actions;
    (function (Actions) {
        class DownloadReportAction extends PDJS.Shared.Actions.ActionDefinition {
            constructor() {
                super({
                    id: "download-report",
                    name: "Download Report",
                    description: "Downloads the specified report.",
                    targetType: "ProspectDirector.Web.Manage.Modules.Actions.DownloadReportAction",
                    parameters: [
                        { id: "entityId", type: PDJS.Core.Actions.ActionParameterType.integer },
                        { id: "reportId", type: PDJS.Core.Actions.ActionParameterType.integer }
                    ]
                });
            }
            execute(args) {
                if (args.reportId == null) {
                    this.showErrorDialog("Report ID");
                    return;
                }
                if (args.entityId == null) {
                    PDJS.Shared.Downloader.download({
                        url: `~/Shared/Report/Export?id=${args.reportId}`,
                    });
                }
                else {
                    PDJS.Shared.Downloader.download({
                        url: `~/Shared/Report/Export?id=${args.reportId}&entityId=${args.entityId}`
                    });
                }
            }
            showErrorDialog(parameterName) {
                Jade.UI.MessageDialog.open({
                    title: "Action Error",
                    message: `Cannot complete the "Download Report Action" because no ${parameterName} has been specified.`,
                    allowClose: true,
                    dialogStyle: Jade.UI.MessageDialogStyle.Error,
                    dialogButtons: Jade.UI.MessageDialogButtonStyle.OK
                });
            }
        }
        Actions.DownloadReportAction = DownloadReportAction;
    })(Actions = PDJS.Actions || (PDJS.Actions = {}));
})(PDJS || (PDJS = {}));
var PDJS;
(function (PDJS) {
    var Actions;
    (function (Actions) {
        class ExecuteRuleAction extends PDJS.Shared.Actions.ActionDefinition {
            constructor() {
                super({
                    id: "execute-rule",
                    name: "Execute Rule",
                    description: "Executes rules contained within a named rule script.",
                    targetType: "ProspectDirector.Web.Manage.Modules.Actions.ExecuteRuleAction",
                    parameters: [
                        { id: "entityId", type: PDJS.Core.Actions.ActionParameterType.integer },
                        { id: "scriptName", type: PDJS.Core.Actions.ActionParameterType.string },
                        { id: "refresh", type: PDJS.Core.Actions.ActionParameterType.boolean }
                    ]
                });
            }
            execute(args) {
                PDJS.Ajax.call({
                    type: "POST",
                    url: Jade.Path.resolve("~/Shared/Rule/ExecuteByScriptName"),
                    data: {
                        entityId: args.entityId,
                        scriptName: args.scriptName
                    },
                    success: (e) => {
                        this.onScriptExecuteSuccess(e, args);
                    }
                });
            }
            onScriptExecuteSuccess(e, args) {
                if (e.hasScriptErrors) {
                    Jade.UI.ErrorDialog.open({
                        dialogStyle: Jade.UI.MessageDialogStyle.Error,
                        dialogButtons: Jade.UI.MessageDialogButtonStyle.OK,
                        title: "Errors",
                        errors: e.scriptErrors
                    });
                    return;
                }
                if (e.hasScriptAlerts) {
                    Jade.UI.ErrorDialog.open({
                        dialogStyle: Jade.UI.MessageDialogStyle.Information,
                        dialogButtons: Jade.UI.MessageDialogButtonStyle.OK,
                        title: "Errors",
                        errors: e.scriptAlerts
                    });
                }
                if (args) {
                    PDJS.JS.ProspectDirector.RUNTIME.MODULES.Explorer.loadContent(PDJS.JS.ProspectDirector.RUNTIME.CURRENT_FOLDER_ID, args.entityId);
                }
            }
        }
        Actions.ExecuteRuleAction = ExecuteRuleAction;
    })(Actions = PDJS.Actions || (PDJS.Actions = {}));
})(PDJS || (PDJS = {}));
var PDJS;
(function (PDJS) {
    var Actions;
    (function (Actions) {
        class OpenEmailMessageAction extends PDJS.Shared.Actions.ActionDefinition {
            constructor() {
                super({
                    id: "open-email-client-message",
                    name: "Open Email Client Message",
                    description: "Opens an email for sending using the default mail client of the user.",
                    targetType: "ProspectDirector.Web.Manage.Modules.Actions.OpenEmailMessageAction",
                    parameters: [
                        { id: "to", type: PDJS.Core.Actions.ActionParameterType.string },
                        { id: "cc", type: PDJS.Core.Actions.ActionParameterType.string },
                        { id: "subject", type: PDJS.Core.Actions.ActionParameterType.string },
                        { id: "body", type: PDJS.Core.Actions.ActionParameterType.string }
                    ]
                });
            }
            execute(args) {
                let mailto = `mailto:${args.to ? encodeURIComponent(args.to) : ""}`;
                let query = [];
                query.push(this.buildParamsString("cc", args.cc));
                query.push(this.buildParamsString("subject", args.subject));
                query.push(this.buildParamsString("body", args.body));
                window.location.href = `${mailto}?${query.join("&")}`;
            }
            buildParamsString(name, value) {
                return `${name}=${encodeURIComponent(value ? value : "")}`;
            }
        }
        Actions.OpenEmailMessageAction = OpenEmailMessageAction;
    })(Actions = PDJS.Actions || (PDJS.Actions = {}));
})(PDJS || (PDJS = {}));
PDJS.Application.onStart(() => {
    let plugin = PDJS.Shared.Plugins.PluginManager.instance.getPlugin("builtin");
    plugin.actions.addActionDefinition(new PDJS.Actions.CreateEntityActions());
    plugin.actions.addActionDefinition(new PDJS.Actions.DownloadReportAction());
    plugin.actions.addActionDefinition(new PDJS.Actions.ExecuteRuleAction());
    plugin.actions.addActionDefinition(new PDJS.Actions.OpenEmailMessageAction());
});
