/*
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 { UltraCalcFunctionFact } from "./UltraCalcFunctionFact";
import { markType } from "igniteui-angular-core";
import { truncate, log10 } from "igniteui-angular-core";
/**
 * @hidden
 */
export let UltraCalcFunctionPoisson = /*@__PURE__*/ (() => {
    class UltraCalcFunctionPoisson extends BuiltInFunctionBase {
        evaluate(a, b) {
            let c = a.pop();
            let d = a.pop();
            let e = a.pop();
            if (e.isError) {
                return new ExcelCalcValue(e.toErrorValue());
            }
            if (d.isError) {
                return new ExcelCalcValue(d.toErrorValue());
            }
            if (c.isError) {
                return new ExcelCalcValue(c.toErrorValue());
            }
            let f = e._toDecimal2();
            let g = d._toDouble3();
            let h = c._toBoolean1();
            if (f < 0 || (f >= 1 && g < 0)) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(4));
            }
            let i = g;
            let j = truncate(f);
            let k = 0;
            if (j > 170 || j * log10(i) >= 290) {
                let l = 0;
                let m = 0;
                let n = truncate(i);
                l = l + 1;
                if (n == j) {
                    m++;
                }
                if (h && n < j) {
                    m++;
                }
                let o = n * 2;
                let p = 1;
                let q = false;
                let r = n + 1;
                while (!q && r <= o) {
                    let s = p * i / r;
                    l = l + s;
                    if (r == j) {
                        m += s;
                    }
                    if (h && r < j) {
                        m += s;
                    }
                    p = s;
                    r = r + 1;
                }
                p = 1;
                q = false;
                r = n - 1;
                while (!q && r >= 0) {
                    let t = p * (r + 1) / i;
                    l = l + t;
                    if (r == j) {
                        m += t;
                    }
                    if (h && r < j) {
                        m += t;
                    }
                    p = t;
                    r = r - 1;
                }
                k = m / l;
            }
            else {
                let u = h ? 0 : j;
                let v = UltraCalcFunctionFact._ak(u);
                let w = Math.exp(-i);
                for (let x = u; x <= j; x++) {
                    k += w * Math.pow(i, x) / v;
                    v *= (x + 1);
                }
            }
            return new ExcelCalcValue(k);
        }
        get_name() {
            return "poisson";
        }
        get name() {
            return this.get_name();
        }
        get_minArgs() {
            return 3;
        }
        get minArgs() {
            return this.get_minArgs();
        }
        get_maxArgs() {
            return 3;
        }
        get maxArgs() {
            return this.get_maxArgs();
        }
    }
    UltraCalcFunctionPoisson.$t = markType(UltraCalcFunctionPoisson, 'UltraCalcFunctionPoisson', BuiltInFunctionBase.$);
    return UltraCalcFunctionPoisson;
})();
