import * as tslib_1 from "tslib";
import { Category, toolbar, dialogAddons } from './annotations';
import Column from './Column';
import { defaultGroup, ECompareValueType, othersGroup } from './interfaces';
import { missingGroup, isMissingValue } from './missing';
import ValueColumn from './ValueColumn';
import { equal } from '../internal';
export var EAlignment;
(function (EAlignment) {
    EAlignment["left"] = "left";
    EAlignment["center"] = "center";
    EAlignment["right"] = "right";
})(EAlignment || (EAlignment = {}));
/**
 * a string column with optional alignment
 */
var StringColumn = /** @class */ (function (_super) {
    tslib_1.__extends(StringColumn, _super);
    function StringColumn(id, desc) {
        var _this = _super.call(this, id, desc) || this;
        _this.currentFilter = null;
        _this.currentGroupCriteria = [];
        _this.setDefaultWidth(200); //by default 200
        _this.alignment = desc.alignment || EAlignment.left;
        _this.escape = desc.escape !== false;
        return _this;
    }
    StringColumn_1 = StringColumn;
    StringColumn.prototype.createEventList = function () {
        return _super.prototype.createEventList.call(this).concat([StringColumn_1.EVENT_GROUPING_CHANGED, StringColumn_1.EVENT_FILTER_CHANGED]);
    };
    StringColumn.prototype.on = function (type, listener) {
        return _super.prototype.on.call(this, type, listener);
    };
    StringColumn.prototype.getValue = function (row) {
        var v = _super.prototype.getValue.call(this, row);
        return isMissingValue(v) ? null : String(v);
    };
    StringColumn.prototype.getLabel = function (row) {
        return this.getValue(row) || '';
    };
    StringColumn.prototype.dump = function (toDescRef) {
        var r = _super.prototype.dump.call(this, toDescRef);
        if (this.currentFilter instanceof RegExp) {
            r.filter = "REGEX:" + this.currentFilter.source;
        }
        else {
            r.filter = this.currentFilter;
        }
        if (this.currentGroupCriteria) {
            r.groupCriteria = this.currentGroupCriteria.map(function (d) { return typeof d === 'string' ? d : "REGEX:" + d.source; });
        }
        return r;
    };
    StringColumn.prototype.restore = function (dump, factory) {
        _super.prototype.restore.call(this, dump, factory);
        if (dump.filter && dump.filter.startsWith('REGEX:')) {
            this.currentFilter = new RegExp(dump.filter.slice(6), 'gm');
        }
        else {
            this.currentFilter = dump.filter || null;
        }
        if (dump.groupCriteria) {
            this.currentGroupCriteria = dump.groupCriteria.map(function (d) { return d.startsWith('REGEX:') ? new RegExp(d.slice(6), 'gm') : d; });
        }
    };
    StringColumn.prototype.isFiltered = function () {
        return this.currentFilter != null;
    };
    StringColumn.prototype.filter = function (row) {
        if (!this.isFiltered()) {
            return true;
        }
        var r = this.getLabel(row);
        var filter = this.currentFilter;
        if (filter === StringColumn_1.FILTER_MISSING) { //filter empty
            return r != null && r.trim() !== '';
        }
        if (typeof filter === 'string' && filter.length > 0) {
            return r !== '' && r.toLowerCase().indexOf(filter.toLowerCase()) >= 0;
        }
        if (filter instanceof RegExp) {
            return r !== '' && filter.test(r);
        }
        return true;
    };
    StringColumn.prototype.getFilter = function () {
        return this.currentFilter;
    };
    StringColumn.prototype.setFilter = function (filter) {
        if (filter === '') {
            filter = null;
        }
        if (this.currentFilter === filter) {
            return;
        }
        this.fire([StringColumn_1.EVENT_FILTER_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY], this.currentFilter, this.currentFilter = filter);
    };
    StringColumn.prototype.clearFilter = function () {
        var was = this.isFiltered();
        this.setFilter(null);
        return was;
    };
    StringColumn.prototype.getGroupCriteria = function () {
        return this.currentGroupCriteria.slice();
    };
    StringColumn.prototype.setGroupCriteria = function (value) {
        if (equal(this.currentGroupCriteria, value)) {
            return;
        }
        var bak = this.getGroupCriteria();
        this.currentGroupCriteria = value.slice();
        this.fire([StringColumn_1.EVENT_GROUPING_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY], bak, value);
    };
    StringColumn.prototype.group = function (row) {
        if (this.getValue(row) == null) {
            return Object.assign({}, missingGroup);
        }
        if (this.currentGroupCriteria.length === 0) {
            return Object.assign({}, othersGroup);
        }
        var value = this.getLabel(row);
        if (!value) {
            return Object.assign({}, missingGroup);
        }
        for (var _i = 0, _a = this.currentGroupCriteria; _i < _a.length; _i++) {
            var criteria = _a[_i];
            if (!((criteria instanceof RegExp && criteria.test(value)) || (typeof criteria === 'string' && value.startsWith(criteria)))) {
                continue;
            }
            return {
                name: typeof criteria === 'string' ? criteria : criteria.source,
                color: defaultGroup.color
            };
        }
        return Object.assign({}, othersGroup);
    };
    StringColumn.prototype.toCompareValue = function (row) {
        var v = this.getValue(row);
        return v === '' || v == null ? null : v.toLowerCase();
    };
    StringColumn.prototype.toCompareValueType = function () {
        return ECompareValueType.STRING;
    };
    var StringColumn_1;
    StringColumn.EVENT_FILTER_CHANGED = 'filterChanged';
    StringColumn.EVENT_GROUPING_CHANGED = 'groupingChanged';
    //magic key for filtering missing ones
    StringColumn.FILTER_MISSING = '__FILTER_MISSING';
    StringColumn = StringColumn_1 = tslib_1.__decorate([
        toolbar('search', 'groupBy', 'filterString'),
        dialogAddons('group', 'groupString'),
        Category('string')
    ], StringColumn);
    return StringColumn;
}(ValueColumn));
export default StringColumn;
//# sourceMappingURL=StringColumn.js.map