import * as tslib_1 from "tslib";
import ADialog from './ADialog';
import { uniqueId, forEach } from './utils';
import { getToolbarDialogAddons } from '../toolbar';
import { cssClass } from '../../styles';
/** @internal */
var GroupDialog = /** @class */ (function (_super) {
    tslib_1.__extends(GroupDialog, _super);
    function GroupDialog(column, dialog, ctx) {
        var _this = _super.call(this, dialog) || this;
        _this.column = column;
        _this.ctx = ctx;
        _this.addons = getToolbarDialogAddons(_this.column, 'group', ctx);
        return _this;
    }
    GroupDialog.prototype.build = function (node) {
        for (var _i = 0, _a = this.addons; _i < _a.length; _i++) {
            var addon = _a[_i];
            this.node.insertAdjacentHTML('beforeend', "<strong>" + addon.title + "</strong>");
            addon.append(this.column, this.node, this.dialog, this.ctx);
        }
        sortOrder(node, this.column, this.dialog.idPrefix);
    };
    return GroupDialog;
}(ADialog));
export default GroupDialog;
function sortOrder(node, column, idPrefix) {
    var ranking = column.findMyRanker();
    var current = ranking.getGroupCriteria();
    var order = current.indexOf(column);
    var enabled = order >= 0;
    if (order < 0) {
        order = current.length;
    }
    var id = uniqueId(idPrefix);
    node.insertAdjacentHTML('afterbegin', "\n        <strong>Group By</strong>\n        <label class=\"" + cssClass('checkbox') + "\"><input type=\"radio\" name=\"grouped\" value=\"true\" " + (enabled ? 'checked' : '') + " ><span>Enabled</span></label>\n        <label class=\"" + cssClass('checkbox') + "\"><input type=\"radio\" name=\"grouped\" value=\"false\" " + (!enabled ? 'checked' : '') + " ><span>Disabled</span></label>\n        <strong>Group Priority</strong>\n        <input type=\"number\" id=\"" + id + "P\" step=\"1\" min=\"1\" max=\"" + (current.length + 1) + "\" value=\"" + (order + 1) + "\">\n    ");
    var updateDisabled = function (disable) {
        forEach(node, 'input:not([name=grouped]), select, textarea', function (d) {
            d.disabled = disable;
        });
    };
    updateDisabled(!enabled);
    var trigger = function () {
        ranking.groupBy(column, !enabled ? -1 : order);
        updateDisabled(!enabled);
    };
    forEach(node, 'input[name=grouped]', function (n) {
        n.addEventListener('change', function () {
            enabled = n.value === 'true';
            trigger();
        }, {
            passive: true
        });
    });
    {
        var priority_1 = node.querySelector("#" + id + "P");
        priority_1.addEventListener('change', function () {
            order = parseInt(priority_1.value, 10) - 1;
            trigger();
        }, {
            passive: true
        });
    }
}
//# sourceMappingURL=GroupDialog.js.map