import * as tslib_1 from "tslib";
import { Category, toolbar, dialogAddons } from './annotations';
import Column from './Column';
import { patternFunction } from './internal';
import ValueColumn from './ValueColumn';
import { EAlignment } from './StringColumn';
import StringColumn from './StringColumn';
/**
 * a string column with optional alignment
 */
var LinkColumn = /** @class */ (function (_super) {
    tslib_1.__extends(LinkColumn, _super);
    function LinkColumn(id, desc) {
        var _this = _super.call(this, id, desc) || this;
        _this.patternFunction = null;
        _this.currentFilter = null;
        _this.currentGroupCriteria = [];
        _this.setDefaultWidth(200); //by default 200
        _this.alignment = desc.alignment || EAlignment.left;
        _this.escape = desc.escape !== false;
        _this.pattern = desc.pattern || '';
        _this.patternTemplates = desc.patternTemplates || [];
        if (_this.pattern) {
            _this.setDefaultRenderer('link');
            _this.setDefaultGroupRenderer('link');
        }
        return _this;
    }
    LinkColumn_1 = LinkColumn;
    LinkColumn.prototype.setPattern = function (pattern) {
        if (pattern === this.pattern) {
            return;
        }
        this.patternFunction = null; // reset cache
        this.fire([LinkColumn_1.EVENT_PATTERN_CHANGED, Column.EVENT_DIRTY_HEADER, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY_CACHES, Column.EVENT_DIRTY], this.pattern, this.pattern = pattern);
    };
    LinkColumn.prototype.getPattern = function () {
        return this.pattern;
    };
    LinkColumn.prototype.createEventList = function () {
        return _super.prototype.createEventList.call(this).concat([LinkColumn_1.EVENT_PATTERN_CHANGED, LinkColumn_1.EVENT_GROUPING_CHANGED, LinkColumn_1.EVENT_FILTER_CHANGED]);
    };
    LinkColumn.prototype.on = function (type, listener) {
        return _super.prototype.on.call(this, type, listener);
    };
    LinkColumn.prototype.getValue = function (row) {
        var l = this.getLink(row);
        return l == null ? null : l.href;
    };
    LinkColumn.prototype.getLink = function (row) {
        var v = _super.prototype.getValue.call(this, row);
        return this.transformValue(v, row);
    };
    LinkColumn.prototype.transformValue = function (v, row) {
        if (v == null || v === '') {
            return null;
        }
        if (typeof v === 'string') {
            if (!this.pattern) {
                return {
                    alt: v,
                    href: v
                };
            }
            if (!this.patternFunction) {
                this.patternFunction = patternFunction(this.pattern, 'item');
            }
            return {
                alt: v,
                href: this.patternFunction.call(this, v, row.v)
            };
        }
        return v;
    };
    LinkColumn.prototype.getLabel = function (row) {
        var l = this.getLink(row);
        return l == null ? '' : l.alt;
    };
    LinkColumn.prototype.dump = function (toDescRef) {
        var r = StringColumn.prototype.dump.call(this, toDescRef);
        if (this.pattern !== this.desc.pattern) {
            r.pattern = this.pattern;
        }
        return r;
    };
    LinkColumn.prototype.restore = function (dump, factory) {
        StringColumn.prototype.restore.call(this, dump, factory);
        if (dump.pattern) {
            this.pattern = dump.pattern;
        }
    };
    LinkColumn.prototype.isFiltered = function () {
        return this.currentFilter != null;
    };
    LinkColumn.prototype.filter = function (row) {
        return StringColumn.prototype.filter.call(this, row);
    };
    LinkColumn.prototype.getFilter = function () {
        return this.currentFilter;
    };
    LinkColumn.prototype.setFilter = function (filter) {
        return StringColumn.prototype.setFilter.call(this, filter);
    };
    LinkColumn.prototype.clearFilter = function () {
        return StringColumn.prototype.clearFilter.call(this);
    };
    LinkColumn.prototype.getGroupCriteria = function () {
        return this.currentGroupCriteria.slice();
    };
    LinkColumn.prototype.setGroupCriteria = function (value) {
        return StringColumn.prototype.setGroupCriteria.call(this, value);
    };
    LinkColumn.prototype.toCompareValue = function (a) {
        return StringColumn.prototype.toCompareValue.call(this, a);
    };
    LinkColumn.prototype.toCompareValueType = function () {
        return StringColumn.prototype.toCompareValueType.call(this);
    };
    LinkColumn.prototype.toCompareGroupValue = function (rows, group) {
        return StringColumn.prototype.toCompareGroupValue.call(this, rows, group);
    };
    LinkColumn.prototype.toCompareGroupValueType = function () {
        return StringColumn.prototype.toCompareGroupValueType.call(this);
    };
    LinkColumn.prototype.group = function (row) {
        return StringColumn.prototype.group.call(this, row);
    };
    var LinkColumn_1;
    LinkColumn.EVENT_FILTER_CHANGED = StringColumn.EVENT_FILTER_CHANGED;
    LinkColumn.EVENT_GROUPING_CHANGED = StringColumn.EVENT_GROUPING_CHANGED;
    LinkColumn.EVENT_PATTERN_CHANGED = 'patternChanged';
    LinkColumn = LinkColumn_1 = tslib_1.__decorate([
        toolbar('search', 'groupBy', 'filterString', 'editPattern'),
        dialogAddons('group', 'groupString'),
        Category('string')
    ], LinkColumn);
    return LinkColumn;
}(ValueColumn));
export default LinkColumn;
//# sourceMappingURL=LinkColumn.js.map