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: [
                        { name: "entityTypeId", type: PDJS.Core.Actions.ActionParameterType.integer },
                        { name: "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: [
                        { name: "entityId", type: PDJS.Core.Actions.ActionParameterType.integer },
                        { name: "reportId", type: PDJS.Core.Actions.ActionParameterType.report }
                    ]
                });
            }
            execute(args) {
                if (args.reportId == null) {
                    this.showErrorDialog("Report ID");
                    return;
                }
                if (args.entityId == null) {
                    PDJS.Shared.Downloader.download({
                        url: "~/Hub/Report/Export",
                        params: {
                            id: args.reportId
                        }
                    });
                }
                else {
                    PDJS.Shared.Downloader.download({
                        url: "~/Hub/Report/Export",
                        params: {
                            id: args.reportId,
                            entityId: args.entityId
                        }
                    });
                }
            }
            showErrorDialog(parameterName) {
                Jade.UI.Dialogs.MessageDialog.open({
                    title: "Action Error",
                    message: `Cannot complete the "Download Report Action" because no ${parameterName} has been specified.`,
                    allowClose: true,
                    dialogStyle: Jade.UI.Dialogs.MessageDialogStyle.Error,
                    dialogButtons: Jade.UI.Dialogs.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: [
                        { name: "entityId", type: PDJS.Core.Actions.ActionParameterType.integer },
                        { name: "scriptId", type: PDJS.Core.Actions.ActionParameterType.ruleScript },
                        { name: "eventName", type: PDJS.Core.Actions.ActionParameterType.string },
                        { name: "refresh", type: PDJS.Core.Actions.ActionParameterType.boolean }
                    ]
                });
            }
            execute(args) {
                let service = new PDJS.Services.Rules.RulesService();
                service.executeScriptById({
                    scriptId: args.scriptId,
                    context: {
                        entityId: args.entityId,
                        eventName: args.eventName
                    },
                    success: (e) => {
                        let response = new PDJS.Shared.Rules.ScriptResponse(e.data);
                        response.handle();
                        if (args.refresh) {
                            if (!document.startViewTransition) {
                                window.location.reload();
                                return;
                            }
                            document.startViewTransition(() => window.location.reload());
                        }
                    }
                });
            }
        }
        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: [
                        { name: "to", type: PDJS.Core.Actions.ActionParameterType.string },
                        { name: "cc", type: PDJS.Core.Actions.ActionParameterType.string },
                        { name: "subject", type: PDJS.Core.Actions.ActionParameterType.string },
                        { name: "body", type: PDJS.Core.Actions.ActionParameterType.string }
                    ]
                });
            }
            execute(args) {
                let mailto = `mailto:${args.to ? encodeURIComponent(args.to) : ""}`;
                let query = [];
                let cc = this.buildParamsString("cc", args.cc);
                let subject = this.buildParamsString("subject", args.subject);
                let body = this.buildParamsString("body", args.body);
                !Jade.String.isNullOrWhiteSpace(cc) ? query.push(cc) : null;
                !Jade.String.isNullOrWhiteSpace(subject) ? query.push(subject) : null;
                !Jade.String.isNullOrWhiteSpace(body) ? query.push(body) : null;
                window.location.href = `${mailto}?${query.join("&")}`;
            }
            buildParamsString(name, value) {
                return !Jade.String.isNullOrWhiteSpace(value) ?
                    `${name}=${encodeURIComponent(value)}` : null;
            }
        }
        Actions.OpenEmailMessageAction = OpenEmailMessageAction;
    })(Actions = PDJS.Actions || (PDJS.Actions = {}));
})(PDJS || (PDJS = {}));
var PDJS;
(function (PDJS) {
    var Actions;
    (function (Actions) {
        class OpenEntityAction extends PDJS.Shared.Actions.ActionDefinition {
            constructor() {
                super({
                    id: "open-entity",
                    name: "Open Entity",
                    description: "Opens the specified entity in the explorer.",
                    targetType: "ProspectDirector.Web.Manage.Modules.Actions.OpenEntityAction",
                    parameters: [
                        { name: "entityId", type: PDJS.Core.Actions.ActionParameterType.integer },
                        { name: "folderId", type: PDJS.Core.Actions.ActionParameterType.string },
                        { name: "newWindow", type: PDJS.Core.Actions.ActionParameterType.boolean }
                    ]
                });
            }
            execute(args) {
                let url = null;
                if (args.entityId == null) {
                    this.showErrorDialog("Entity ID");
                    return;
                }
                args.folderId != null ?
                    url = Jade.Path.resolve(`~/explore/${args.entityId}/folder/${args.folderId}`) :
                    url = Jade.Path.resolve(`~/explore/${args.entityId}`);
                args.newWindow ?
                    window.open(url) :
                    window.location = url;
            }
            showErrorDialog(label) {
                Jade.UI.Dialogs.MessageDialog.open({
                    title: "Action Error",
                    message: "Cannot complete the 'Open Entity Action' because no " + label + " has been specified.",
                    allowClose: true,
                    dialogStyle: Jade.UI.Dialogs.MessageDialogStyle.Error,
                    dialogButtons: Jade.UI.Dialogs.MessageDialogButtonStyle.OK
                });
            }
        }
        Actions.OpenEntityAction = OpenEntityAction;
    })(Actions = PDJS.Actions || (PDJS.Actions = {}));
})(PDJS || (PDJS = {}));
var PDJS;
(function (PDJS) {
    var Actions;
    (function (Actions) {
        class OpenGlobalViewAction extends PDJS.Shared.Actions.ActionDefinition {
            constructor() {
                super({
                    id: "open-global-view",
                    name: "Open Global View",
                    description: "Opens the specified global view.",
                    targetType: "ProspectDirector.Web.Manage.Modules.Actions.OpenGlobalViewAction",
                    parameters: [
                        { name: "viewId", type: PDJS.Core.Actions.ActionParameterType.string },
                        { name: "newWindow", type: PDJS.Core.Actions.ActionParameterType.boolean }
                    ]
                });
            }
            execute(args) {
                let url = null;
                if (args.viewId == null) {
                    this.showErrorDialog();
                    return;
                }
                url = Jade.Path.resolve(`~/viewbrowser?viewId=${args.viewId}`);
                args.newWindow ?
                    window.open(url) :
                    window.location = url;
            }
            showErrorDialog() {
                Jade.UI.Dialogs.MessageDialog.open({
                    title: "Action Error",
                    message: "Cannot open the global view because no view custom ID has been specified.",
                    allowClose: true,
                    dialogStyle: Jade.UI.Dialogs.MessageDialogStyle.Error,
                    dialogButtons: Jade.UI.Dialogs.MessageDialogButtonStyle.OK
                });
            }
        }
        Actions.OpenGlobalViewAction = OpenGlobalViewAction;
    })(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());
    plugin.actions.addActionDefinition(new PDJS.Actions.OpenEntityAction());
    plugin.actions.addActionDefinition(new PDJS.Actions.OpenGlobalViewAction());
});
