/// class PD.Manage.Modules.RecycleBinDeleteCommand

PD.Manage.Modules.RecycleBinDeleteCommand =
PD.Commands.Command.extend({

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

    setDefaults: function () {
        this.props("id", "recycle-bin/delete");
        this.props("enabled", false);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Permanently delete the selected Entities.");
        this.state("recycleBinItems", null);
    },

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

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

        let ids = _.map(this._selectedRecycleBinItems(), "id");

        PDJS.Manage.RecycleBin.RecycleBinConfirmationDialog.open({
            select: (e) => {
                this._deleteFromRecycleBin(ids, e === "delete");
            }
        });

    },

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

    _isEnabled: function () {
        return this._selectedRecycleBinItems().length > 0;
    },

    _selectedRecycleBinItems: function () {

        var result = [];

        if (_.isArray(this.state("recycleBinItems"))) {
            result = this.state("recycleBinItems");
        }

        return result;

    },

    _deleteFromRecycleBin: function (ids, deleteReferences) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Delete from Recycle Bin",
            taskStart: (args) => {

                PD.ajax({
                    type: "DELETE",
                    url: PD.Path.resolve("~/Manage/RecycleBin/Delete/"),
                    data: {
                        ids: ids,
                        removeReferences: deleteReferences
                    },
                    success: function (e) {
                        args.startPolling(e);
                    },
                    error: function (e) {
                        args.abort(e.responseJSON);
                        e.errorHandled = true;
                    }
                });

            },
            taskSuccess: (args) => {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("recycle-bin/delete");

                args.close();

            }
        });

    }

});