import * as tslib_1 from "tslib";
import { Ranking } from '../model';
import ADataProvider from './ADataProvider';
import { isComplexAccessor, rowGetter, rowComplexGetter, rowGuessGetter } from './accessor';
function injectAccessor(d) {
    d.accessor = d.accessor || (d.column ? (isComplexAccessor(d.column) ? rowComplexGetter : rowGetter) : rowGuessGetter);
    d.label = d.label || d.column;
    return d;
}
/**
 * common base implementation of a DataProvider with a fixed list of column descriptions
 */
var ACommonDataProvider = /** @class */ (function (_super) {
    tslib_1.__extends(ACommonDataProvider, _super);
    function ACommonDataProvider(columns, options) {
        if (columns === void 0) { columns = []; }
        if (options === void 0) { options = {}; }
        var _this = _super.call(this, options) || this;
        _this.columns = columns;
        _this.rankingIndex = 0;
        //generate the accessor
        columns.forEach(injectAccessor);
        return _this;
    }
    ACommonDataProvider.prototype.cloneRanking = function (existing) {
        var _this = this;
        var id = this.nextRankingId();
        var clone = new Ranking(id);
        if (existing) { //copy the ranking of the other one
            //TODO better cloning
            existing.children.forEach(function (child) {
                _this.push(clone, child.desc);
            });
        }
        return clone;
    };
    /**
     * adds another column description to this data provider
     * @param column
     */
    ACommonDataProvider.prototype.pushDesc = function (column) {
        injectAccessor(column);
        this.columns.push(column);
        this.fire(ADataProvider.EVENT_ADD_DESC, column);
    };
    ACommonDataProvider.prototype.clearColumns = function () {
        this.clearRankings();
        this.columns.splice(0, this.columns.length);
        this.fire(ADataProvider.EVENT_CLEAR_DESC);
    };
    ACommonDataProvider.prototype.getColumns = function () {
        return this.columns.slice();
    };
    ACommonDataProvider.prototype.findDesc = function (ref) {
        return this.columns.filter(function (c) { return c.column === ref; })[0];
    };
    /**
     * identify by the tuple type@columnname
     * @param desc
     * @returns {string}
     */
    ACommonDataProvider.prototype.toDescRef = function (desc) {
        return typeof desc.column !== 'undefined' ? desc.type + "@" + desc.column : this.cleanDesc(Object.assign(desc));
    };
    ACommonDataProvider.prototype.fromDescRef = function (descRef) {
        if (typeof (descRef) === 'string') {
            return this.columns.find(function (d) { return d.type + "@" + d.column === descRef || d.type === descRef; });
        }
        var existing = this.columns.find(function (d) { return descRef.column === d.column && descRef.label === d.label && descRef.type === d.type; });
        if (existing) {
            return existing;
        }
        return descRef;
    };
    ACommonDataProvider.prototype.restore = function (dump) {
        _super.prototype.restore.call(this, dump);
        this.rankingIndex = 1 + Math.max.apply(Math, this.getRankings().map(function (r) { return +r.id.substring(4); }));
    };
    ACommonDataProvider.prototype.nextRankingId = function () {
        return "rank" + this.rankingIndex++;
    };
    return ACommonDataProvider;
}(ADataProvider));
export default ACommonDataProvider;
//# sourceMappingURL=ACommonDataProvider.js.map