/// class PD.Manage.Modules.Rules.RuleScriptDeleteCommand
// PD.Commands.Command

PD.Manage.Modules.Rules.RuleScriptDeleteCommand =
PD.Commands.Command.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    setDefaults: function () {
        this._super();
        this.props("id", "rule-script/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete the selected Rule Script.");
        this.state("rulescript", null);
    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this._isEnabled());
    },

    execute: function (e) {
        this._super(e);

        var rulescript =
            this.state("rulescript");

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Delete Rule Script?",
            message: `Delete the "${rulescript.name}" rule script?`,
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._executeDeletion(rulescript);
                }
            }
        });

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _isEnabled: function () {

        var rulescript =
            this.state("rulescript");

        return rulescript != null
            && rulescript.isBuiltIn !== true;

    },

    _executeDeletion: function (rulescript) {

        let service = new PD.Manage.Services.RulesService();

        service.deleteRuleScript(rulescript.id, function () {
            PD.context
                .get_default()
                .get_events()
                .publish("rule-script/delete");
        });

    }
});