/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { __extends, __values } from "tslib";
import { Base, EnumUtil, String_$type, fromEnum, markType } from "./type";
import { IDataSeriesAdapterRule_$type } from "./IDataSeriesAdapterRule";
import { HashSet$1 } from "./HashSet$1";
import { DataSeriesIntent_$type } from "./DataSeriesIntent";
import { DataSeries } from "./DataSeries";
import { DataSeriesMemberPathHint } from "./DataSeriesMemberPathHint";
import { truncate } from "./number";
/**
 * @hidden
 */
var SimpleCategorySeriesRule = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(SimpleCategorySeriesRule, _super);
    function SimpleCategorySeriesRule() {
        var _this = _super.call(this) || this;
        _this._priority = 0;
        _this._useColumnThreshold = 0;
        _this._distinctCheckThreshold = 0;
        _this.b = 1;
        _this.priority = 10;
        _this.distinctCheckThreshold = 50000;
        _this.useColumnThreshold = 30;
        return _this;
    }
    Object.defineProperty(SimpleCategorySeriesRule.prototype, "priority", {
        get: function () {
            return this._priority;
        },
        set: function (a) {
            this._priority = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SimpleCategorySeriesRule.prototype, "useColumnThreshold", {
        get: function () {
            return this._useColumnThreshold;
        },
        set: function (a) {
            this._useColumnThreshold = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SimpleCategorySeriesRule.prototype, "distinctCheckThreshold", {
        get: function () {
            return this._distinctCheckThreshold;
        },
        set: function (a) {
            this._distinctCheckThreshold = a;
        },
        enumerable: false,
        configurable: true
    });
    SimpleCategorySeriesRule.prototype.getPrimaryAxisLabelsString = function (a) {
        var _this = this;
        var b = 1;
        var c = ((function () { var d = SimpleCategorySeriesRule.f(a.analyzer, _this.distinctCheckThreshold, b); b = d.p2; return d.ret; })());
        this.b = b;
        return c;
    };
    SimpleCategorySeriesRule.prototype.getAdditionalValuePropertyStrings = function (a) {
        return null;
    };
    SimpleCategorySeriesRule.prototype.getPrimaryAxisLabelsStrings = function (a) {
        var e_1, _a;
        var b = a.analyzer;
        var c = b.getAllPropertiesWithIntent(EnumUtil.getName(DataSeriesIntent_$type, (16)));
        if (c.length > 0) {
            for (var d = 0; d < c.length; d++) {
                a.am(a.getCurrentPath(), c[d], 0);
            }
            return c;
        }
        var e = b.getAllStringProperties();
        var f = b.getAllDateTimeProperties();
        var g = b.getAllDistinctMonotonicNumericProperties(this.distinctCheckThreshold);
        var h = new HashSet$1(String_$type, 0);
        var i = a.getCurrentPath();
        for (var j = 0; j < e.length; j++) {
            var k = e[j];
            h.add_1(k);
            a.am(a.getCurrentPath(), k, 0);
        }
        for (var l = 0; l < f.length; l++) {
            var m = f[l];
            h.add_1(m);
            a.am(a.getCurrentPath(), m, 1);
        }
        for (var n = 0; n < g.length; n++) {
            var o = g[n];
            h.add_1(o);
            a.am(a.getCurrentPath(), o, 2);
        }
        e = b.getAllExcludedStringProperties();
        f = b.getAllExcludedDateTimeProperties();
        g = b.getAllExcludedDistinctMonotonicNumericProperties(this.distinctCheckThreshold);
        for (var p = 0; p < e.length; p++) {
            var q = e[p];
            h.add_1(q);
            a.am(a.getCurrentPath(), q, 0);
        }
        for (var r = 0; r < f.length; r++) {
            var s = f[r];
            h.add_1(s);
            a.am(a.getCurrentPath(), s, 1);
        }
        for (var t = 0; t < g.length; t++) {
            var u = g[t];
            h.add_1(u);
            a.am(a.getCurrentPath(), u, 2);
        }
        var v = new Array(h.count);
        var w = 0;
        try {
            for (var _b = __values(fromEnum(h)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var x = _c.value;
                v[w] = x;
                w++;
            }
        }
        catch (e_1_1) {
            e_1 = { error: e_1_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_1)
                    throw e_1.error;
            }
        }
        return v;
    };
    SimpleCategorySeriesRule.f = function (a, b, c) {
        var d = a.getAllPropertiesWithIntent(EnumUtil.getName(DataSeriesIntent_$type, (16)));
        if (d.length > 0) {
            return {
                ret: d[0],
                p2: c
            };
        }
        var e = a.getFirstDistinctStringProperty(b);
        c = 1;
        if (e != null) {
            c = 1;
            return {
                ret: e,
                p2: c
            };
        }
        e = a.getFirstDistinctDateTimeProperty(b);
        if (e != null) {
            c = 1;
            return {
                ret: e,
                p2: c
            };
        }
        e = a.getFirstDistinctMonotonicNumericPropertyWithEvenSpacing(b);
        if (e != null) {
            c = 1;
            return {
                ret: e,
                p2: c
            };
        }
        e = a.getFirstDistinctMonotonicNumericProperty(b);
        if (e != null) {
            c = 0.9;
            return {
                ret: e,
                p2: c
            };
        }
        c = 0.7;
        e = a.getFirstStringProperty();
        return {
            ret: e,
            p2: c
        };
    };
    SimpleCategorySeriesRule.e = function (a, b) {
        var c = 1;
        return ((function () { var d = SimpleCategorySeriesRule.f(a.analyzer, b, c); c = d.p2; return d.ret; })());
    };
    SimpleCategorySeriesRule.prototype.evaluate = function (a) {
        var b = 1;
        var c = SimpleCategorySeriesRule.e(a, this.distinctCheckThreshold);
        if (a.adjustPrioritiesBasedOnFitness) {
            b *= this.b;
        }
        var d = null;
        var e = a.analyzer.getAllPropertiesWithIntent(EnumUtil.getName(DataSeriesIntent_$type, (0)));
        if (e.length > 0) {
            d = e;
        }
        if (d == null) {
            d = a.analyzer.getAllNumericProperties();
        }
        if (d == null || d.length == 0) {
            return;
        }
        var f = a.getCurrentDataSource().actualCount;
        if (a.adjustPrioritiesBasedOnFitness && d.length > 0 && d.length < 2 && f > 1 && f < 12) {
            if (this.a(d[0], a)) {
                b *= 0.9;
            }
        }
        if (a.adjustPrioritiesBasedOnFitness && f == 1) {
            b *= 0.8;
        }
        for (var g = 0; g < d.length; g++) {
            if (d[g] == c && d.length > 1) {
                continue;
            }
            var h = new Array(1);
            h[0] = d[g];
            var i = a.analyzer.getTitleString(null, h);
            var j = new DataSeries();
            var k = new DataSeriesMemberPathHint();
            k.intent = 0;
            k.path = d[g];
            var l = new DataSeriesMemberPathHint();
            l.intent = 16;
            l.path = c;
            j.addMemberPathHint(k);
            j.addMemberPathHint(l);
            if (f > this.useColumnThreshold) {
                j.suggestedSeries = 0;
            }
            else {
                j.suggestedSeries = 1;
            }
            this.g(j, a);
            j.suggestedPrimaryAxis = 0;
            j.suggestedSecondaryAxis = 1;
            j.name = d[g];
            if (i != null) {
                j.title = i;
            }
            else {
                j.title = d[g];
                j.title = a.analyzer.expandCamelCasedWords(j.title);
            }
            if (a.adjustPrioritiesBasedOnFitness) {
                j.priority = truncate(Math.round(this.priority * b));
            }
            a.addDataSeries(j, this);
        }
    };
    SimpleCategorySeriesRule.prototype.a = function (a, b) {
        return b.analyzer.numericColumnHasDiverseValues(a);
    };
    SimpleCategorySeriesRule.prototype.g = function (a, b) {
        if (!b.hasValidType(a)) {
            a.suggestedSeries = 0;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 1;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 2;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 6;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 7;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 4;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 5;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 10;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 8;
            if (b.hasValidType(a)) {
                return;
            }
            a.suggestedSeries = 0;
        }
    };
    SimpleCategorySeriesRule.$t = markType(SimpleCategorySeriesRule, 'SimpleCategorySeriesRule', Base.$, [IDataSeriesAdapterRule_$type]);
    return SimpleCategorySeriesRule;
}(Base));
export { SimpleCategorySeriesRule };
