import * as tslib_1 from "tslib";
import { Category, toolbar } from './annotations';
import CategoricalColumn from './CategoricalColumn';
import Column from './Column';
import ValueColumn from './ValueColumn';
import { ECompareValueType } from './interfaces';
import { DEFAULT_CATEGORICAL_COLOR_FUNCTION } from './CategoricalColorMappingFunction';
/**
 * a string column with optional alignment
 */
var BooleanColumn = /** @class */ (function (_super) {
    tslib_1.__extends(BooleanColumn, _super);
    function BooleanColumn(id, desc) {
        var _this = _super.call(this, id, desc) || this;
        _this.currentFilter = null;
        _this.setWidthImpl(30);
        _this.categories = [
            {
                name: desc.trueMarker || '✓',
                color: BooleanColumn_1.GROUP_TRUE.color,
                label: BooleanColumn_1.GROUP_TRUE.name,
                value: 0
            },
            {
                name: desc.trueMarker || '',
                color: BooleanColumn_1.GROUP_FALSE.color,
                label: BooleanColumn_1.GROUP_FALSE.name,
                value: 1
            }
        ];
        _this.colorMapping = DEFAULT_CATEGORICAL_COLOR_FUNCTION;
        return _this;
    }
    BooleanColumn_1 = BooleanColumn;
    BooleanColumn.prototype.createEventList = function () {
        return _super.prototype.createEventList.call(this).concat([BooleanColumn_1.EVENT_COLOR_MAPPING_CHANGED, BooleanColumn_1.EVENT_FILTER_CHANGED]);
    };
    BooleanColumn.prototype.on = function (type, listener) {
        return _super.prototype.on.call(this, type, listener);
    };
    Object.defineProperty(BooleanColumn.prototype, "dataLength", {
        get: function () {
            return this.categories.length;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BooleanColumn.prototype, "labels", {
        get: function () {
            return this.categories.map(function (d) { return d.label; });
        },
        enumerable: true,
        configurable: true
    });
    BooleanColumn.prototype.getValue = function (row) {
        var v = _super.prototype.getValue.call(this, row);
        if (typeof (v) === 'undefined' || v == null) {
            return false;
        }
        return v === true || v === 'true' || v === 'yes' || v === 'x';
    };
    BooleanColumn.prototype.getCategory = function (row) {
        var v = this.getValue(row);
        return this.categories[v ? 0 : 1];
    };
    BooleanColumn.prototype.getCategories = function (row) {
        return [this.getCategory(row)];
    };
    BooleanColumn.prototype.iterCategory = function (row) {
        return [this.getCategory(row)];
    };
    BooleanColumn.prototype.getColor = function (row) {
        return CategoricalColumn.prototype.getColor.call(this, row);
    };
    BooleanColumn.prototype.getLabel = function (row) {
        return CategoricalColumn.prototype.getLabel.call(this, row);
    };
    BooleanColumn.prototype.getLabels = function (row) {
        return CategoricalColumn.prototype.getLabels.call(this, row);
    };
    BooleanColumn.prototype.getValues = function (row) {
        return CategoricalColumn.prototype.getValues.call(this, row);
    };
    BooleanColumn.prototype.getMap = function (row) {
        return CategoricalColumn.prototype.getMap.call(this, row);
    };
    BooleanColumn.prototype.getMapLabel = function (row) {
        return CategoricalColumn.prototype.getMapLabel.call(this, row);
    };
    BooleanColumn.prototype.getSet = function (row) {
        var v = this.getValue(row);
        var r = new Set();
        r.add(this.categories[v ? 0 : 1]);
        return r;
    };
    BooleanColumn.prototype.dump = function (toDescRef) {
        var r = _super.prototype.dump.call(this, toDescRef);
        r.colorMapping = this.colorMapping.toJSON();
        if (this.currentFilter != null) {
            r.filter = this.currentFilter;
        }
        return r;
    };
    BooleanColumn.prototype.restore = function (dump, factory) {
        _super.prototype.restore.call(this, dump, factory);
        this.colorMapping = factory.categoricalColorMappingFunction(dump.colorMapping, this.categories);
        if (typeof dump.filter !== 'undefined') {
            this.currentFilter = dump.filter;
        }
    };
    BooleanColumn.prototype.getColorMapping = function () {
        return this.colorMapping.clone();
    };
    BooleanColumn.prototype.setColorMapping = function (mapping) {
        if (this.colorMapping.eq(mapping)) {
            return;
        }
        this.fire([CategoricalColumn.EVENT_COLOR_MAPPING_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY_CACHES, Column.EVENT_DIRTY_HEADER, Column.EVENT_DIRTY], this.colorMapping.clone(), this.colorMapping = mapping);
    };
    BooleanColumn.prototype.isFiltered = function () {
        return this.currentFilter != null;
    };
    BooleanColumn.prototype.filter = function (row) {
        if (!this.isFiltered()) {
            return true;
        }
        var r = this.getValue(row);
        return r === this.currentFilter;
    };
    BooleanColumn.prototype.getFilter = function () {
        return this.currentFilter;
    };
    BooleanColumn.prototype.setFilter = function (filter) {
        if (this.currentFilter === filter) {
            return;
        }
        this.fire([BooleanColumn_1.EVENT_FILTER_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY], this.currentFilter, this.currentFilter = filter);
    };
    BooleanColumn.prototype.clearFilter = function () {
        var was = this.isFiltered();
        this.setFilter(null);
        return was;
    };
    BooleanColumn.prototype.toCompareValue = function (row) {
        var v = this.getValue(row);
        if (v == null) {
            return NaN;
        }
        return v ? 1 : 0;
    };
    BooleanColumn.prototype.toCompareValueType = function () {
        return ECompareValueType.BINARY;
    };
    BooleanColumn.prototype.group = function (row) {
        var enabled = this.getValue(row);
        return Object.assign({}, enabled ? BooleanColumn_1.GROUP_TRUE : BooleanColumn_1.GROUP_FALSE);
    };
    var BooleanColumn_1;
    BooleanColumn.EVENT_FILTER_CHANGED = 'filterChanged';
    BooleanColumn.EVENT_COLOR_MAPPING_CHANGED = 'colorMappingChanged';
    BooleanColumn.GROUP_TRUE = { name: 'True', color: 'black' };
    BooleanColumn.GROUP_FALSE = { name: 'False', color: 'white' };
    BooleanColumn = BooleanColumn_1 = tslib_1.__decorate([
        toolbar('group', 'groupBy', 'filterBoolean', 'colorMappedCategorical'),
        Category('categorical')
    ], BooleanColumn);
    return BooleanColumn;
}(ValueColumn));
export default BooleanColumn;
//# sourceMappingURL=BooleanColumn.js.map