/*
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 { 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
 */
export class UltraCalcFunctionTwoMatrixFunction extends BuiltInFunctionBase {
    _ak(a, b) {
        let c;
        let d;
        let e;
        let 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));
    }
    static _al(a, b) {
        b = null;
        let c;
        let d;
        let e = UltraCalcFunctionTwoMatrixFunction._ao(a, b, c, d);
        b = e.p1;
        c = e.p2;
        d = e.p3;
        if (b != null) {
            return {
                ret: 0,
                p1: b
            };
        }
        let f;
        let g;
        let h;
        let i = UltraCalcFunctionTwoMatrixFunction._ar(c, d, f, g, h);
        f = i.p2;
        g = i.p3;
        h = i.p4;
        let j = g / f;
        let k = h / f;
        let l = UltraCalcFunctionTwoMatrixFunction._am(c, d, (m, n) => (m - j) * (n - k));
        let m = UltraCalcFunctionTwoMatrixFunction._an(c, (n) => Math.pow(n - j, 2));
        let n = UltraCalcFunctionTwoMatrixFunction._an(d, (o) => Math.pow(o - k, 2));
        let o = Math.sqrt(m * n);
        if (o == 0) {
            b = new ExcelCalcErrorValue(2);
            return {
                ret: 0,
                p1: b
            };
        }
        return {
            ret: l / o,
            p1: b
        };
    }
    static _ao(a, b, c, d) {
        b = null;
        c = null;
        d = null;
        let e = a.pop();
        let 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
            };
        }
        let g = f.toArrayProxy();
        let h = g.getLength(0);
        let i = g.getLength(1);
        let j = e.toArrayProxy();
        let k = j.getLength(0);
        let l = j.getLength(1);
        if (h != k || i != l) {
            b = new ExcelCalcErrorValue(3);
            return {
                p1: b,
                p2: c,
                p3: d
            };
        }
        c = ((() => { let m = MatrixUtilities.b(g, 2, b); b = m.p2; return m.ret; })());
        if (b != null) {
            return {
                p1: b,
                p2: c,
                p3: d
            };
        }
        d = ((() => { let m = MatrixUtilities.b(j, 2, b); b = m.p2; return m.ret; })());
        return {
            p1: b,
            p2: c,
            p3: d
        };
    }
    static _aq(a, b, c, d) {
        d = null;
        b = 0;
        c = 0;
        let e;
        let f;
        let 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
            };
        }
        let h;
        let i;
        let 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
        };
    }
    static _ap(a, b, c, d, e, f, g) {
        g = null;
        e = 0;
        f = 0;
        let h;
        let i;
        let j;
        let k = UltraCalcFunctionTwoMatrixFunction._ar(a, b, h, i, j);
        h = k.p2;
        i = k.p3;
        j = k.p4;
        c = i / h;
        d = j / h;
        let l = c;
        let m = d;
        let n = UltraCalcFunctionTwoMatrixFunction._am(a, b, (o, p) => (o - l) * (p - m));
        let o = UltraCalcFunctionTwoMatrixFunction._an(b, (p) => 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
        };
    }
    static _ar(a, b, c, d, e) {
        let f = arrayGetLength(a, 0);
        let g = arrayGetLength(a, 1);
        c = f * g;
        d = 0;
        e = 0;
        for (let h = 0; h < f; h++) {
            for (let i = 0; i < g; i++) {
                let j = a[h][i];
                let 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
        };
    }
    static _an(a, b) {
        let c = arrayGetLength(a, 0);
        let d = arrayGetLength(a, 1);
        let e = 0;
        for (let f = 0; f < c; f++) {
            for (let g = 0; g < d; g++) {
                let h = a[f][g];
                if (isNaN_(h)) {
                    continue;
                }
                e += b(h);
            }
        }
        return e;
    }
    static _am(a, b, c) {
        let d = arrayGetLength(a, 0);
        let e = arrayGetLength(a, 1);
        let f = 0;
        for (let g = 0; g < d; g++) {
            for (let h = 0; h < e; h++) {
                let i = a[g][h];
                let j = b[g][h];
                if (isNaN_(i) || isNaN_(j)) {
                    continue;
                }
                f += c(i, j);
            }
        }
        return f;
    }
    get_minArgs() {
        return 2;
    }
    get minArgs() {
        return this.get_minArgs();
    }
    get_maxArgs() {
        return 2;
    }
    get maxArgs() {
        return this.get_maxArgs();
    }
}
UltraCalcFunctionTwoMatrixFunction.$t = markType(UltraCalcFunctionTwoMatrixFunction, 'UltraCalcFunctionTwoMatrixFunction', BuiltInFunctionBase.$);
//# sourceMappingURL=UltraCalcFunctionTwoMatrixFunction.js.map