/*
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 * as tslib_1 from "tslib";
import { BuiltInFunctionBase } from "./BuiltInFunctionBase";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
import { MatrixUtilities } from "./MatrixUtilities";
import { markType } from "igniteui-angular-core";
import { arrayGetLength } from "igniteui-angular-core";
import { isNaN_ } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionTwoMatrixFunction = /** @class */ (function (_super) {
    tslib_1.__extends(UltraCalcFunctionTwoMatrixFunction, _super);
    function UltraCalcFunctionTwoMatrixFunction() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionTwoMatrixFunction.prototype._ak = function (a, b) {
        var c;
        var d;
        var e;
        var f = UltraCalcFunctionTwoMatrixFunction._ao(a, c, d, e);
        c = f.p1;
        d = f.p2;
        e = f.p3;
        if (c != null) {
            return new ExcelCalcValue(c);
        }
        return new ExcelCalcValue(UltraCalcFunctionTwoMatrixFunction._am(d, e, b));
    };
    UltraCalcFunctionTwoMatrixFunction._al = function (a, b) {
        b = null;
        var c;
        var d;
        var e = UltraCalcFunctionTwoMatrixFunction._ao(a, b, c, d);
        b = e.p1;
        c = e.p2;
        d = e.p3;
        if (b != null) {
            return {
                ret: 0,
                p1: b
            };
        }
        var f;
        var g;
        var h;
        var i = UltraCalcFunctionTwoMatrixFunction._ar(c, d, f, g, h);
        f = i.p2;
        g = i.p3;
        h = i.p4;
        var j = g / f;
        var k = h / f;
        var l = UltraCalcFunctionTwoMatrixFunction._am(c, d, function (m, n) { return (m - j) * (n - k); });
        var m = UltraCalcFunctionTwoMatrixFunction._an(c, function (n) { return Math.pow(n - j, 2); });
        var n = UltraCalcFunctionTwoMatrixFunction._an(d, function (o) { return Math.pow(o - k, 2); });
        var o = Math.sqrt(m * n);
        if (o == 0) {
            b = new ExcelCalcErrorValue(2);
            return {
                ret: 0,
                p1: b
            };
        }
        return {
            ret: l / o,
            p1: b
        };
    };
    UltraCalcFunctionTwoMatrixFunction._ao = function (a, b, c, d) {
        b = null;
        c = null;
        d = null;
        var e = a.pop();
        var f = a.pop();
        if (f.isError) {
            b = f.toErrorValue();
            return {
                p1: b,
                p2: c,
                p3: d
            };
        }
        if (e.isError) {
            b = e.toErrorValue();
            return {
                p1: b,
                p2: c,
                p3: d
            };
        }
        var g = f.toArrayProxy();
        var h = g.getLength(0);
        var i = g.getLength(1);
        var j = e.toArrayProxy();
        var k = j.getLength(0);
        var l = j.getLength(1);
        if (h != k || i != l) {
            b = new ExcelCalcErrorValue(3);
            return {
                p1: b,
                p2: c,
                p3: d
            };
        }
        c = ((function () { var m = MatrixUtilities.b(g, 2, b); b = m.p2; return m.ret; })());
        if (b != null) {
            return {
                p1: b,
                p2: c,
                p3: d
            };
        }
        d = ((function () { var m = MatrixUtilities.b(j, 2, b); b = m.p2; return m.ret; })());
        return {
            p1: b,
            p2: c,
            p3: d
        };
    };
    UltraCalcFunctionTwoMatrixFunction._aq = function (a, b, c, d) {
        d = null;
        b = 0;
        c = 0;
        var e;
        var f;
        var g = UltraCalcFunctionTwoMatrixFunction._ao(a, d, e, f);
        d = g.p1;
        e = g.p2;
        f = g.p3;
        if (d != null) {
            return {
                p1: b,
                p2: c,
                p3: d
            };
        }
        var h;
        var i;
        var j = UltraCalcFunctionTwoMatrixFunction._ap(e, f, h, i, b, c, d);
        h = j.p2;
        i = j.p3;
        b = j.p4;
        c = j.p5;
        d = j.p6;
        return {
            p1: b,
            p2: c,
            p3: d
        };
    };
    UltraCalcFunctionTwoMatrixFunction._ap = function (a, b, c, d, e, f, g) {
        g = null;
        e = 0;
        f = 0;
        var h;
        var i;
        var j;
        var k = UltraCalcFunctionTwoMatrixFunction._ar(a, b, h, i, j);
        h = k.p2;
        i = k.p3;
        j = k.p4;
        c = i / h;
        d = j / h;
        var l = c;
        var m = d;
        var n = UltraCalcFunctionTwoMatrixFunction._am(a, b, function (o, p) { return (o - l) * (p - m); });
        var o = UltraCalcFunctionTwoMatrixFunction._an(b, function (p) { return Math.pow(p - m, 2); });
        if (o == 0) {
            g = new ExcelCalcErrorValue(2);
            return {
                p2: c,
                p3: d,
                p4: e,
                p5: f,
                p6: g
            };
        }
        e = n / o;
        f = c - (e * d);
        return {
            p2: c,
            p3: d,
            p4: e,
            p5: f,
            p6: g
        };
    };
    UltraCalcFunctionTwoMatrixFunction._ar = function (a, b, c, d, e) {
        var f = arrayGetLength(a, 0);
        var g = arrayGetLength(a, 1);
        c = f * g;
        d = 0;
        e = 0;
        for (var h = 0; h < f; h++) {
            for (var i = 0; i < g; i++) {
                var j = a[h][i];
                var k = b[h][i];
                if (isNaN_(j) || isNaN_(k)) {
                    c--;
                    a[h][i] = NaN;
                    b[h][i] = NaN;
                    continue;
                }
                d += j;
                e += k;
            }
        }
        return {
            p2: c,
            p3: d,
            p4: e
        };
    };
    UltraCalcFunctionTwoMatrixFunction._an = function (a, b) {
        var c = arrayGetLength(a, 0);
        var d = arrayGetLength(a, 1);
        var e = 0;
        for (var f = 0; f < c; f++) {
            for (var g = 0; g < d; g++) {
                var h = a[f][g];
                if (isNaN_(h)) {
                    continue;
                }
                e += b(h);
            }
        }
        return e;
    };
    UltraCalcFunctionTwoMatrixFunction._am = function (a, b, c) {
        var d = arrayGetLength(a, 0);
        var e = arrayGetLength(a, 1);
        var f = 0;
        for (var g = 0; g < d; g++) {
            for (var h = 0; h < e; h++) {
                var i = a[g][h];
                var j = b[g][h];
                if (isNaN_(i) || isNaN_(j)) {
                    continue;
                }
                f += c(i, j);
            }
        }
        return f;
    };
    UltraCalcFunctionTwoMatrixFunction.prototype.get_minArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionTwoMatrixFunction.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionTwoMatrixFunction.prototype.get_maxArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionTwoMatrixFunction.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionTwoMatrixFunction.$t = markType(UltraCalcFunctionTwoMatrixFunction, 'UltraCalcFunctionTwoMatrixFunction', BuiltInFunctionBase.$);
    return UltraCalcFunctionTwoMatrixFunction;
}(BuiltInFunctionBase));
export { UltraCalcFunctionTwoMatrixFunction };
//# sourceMappingURL=UltraCalcFunctionTwoMatrixFunction.js.map