/*
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 { List$1 } from "igniteui-angular-core";
import { StringBuilder } from "igniteui-angular-core";
import { CalcFunctionUtilities } from "./CalcFunctionUtilities";
import { markType } from "igniteui-angular-core";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
import { ExcelCalcFunction } from "./ExcelCalcFunction";
import { tryParseBool } from "igniteui-angular-core";
/**
 * @hidden
 */
export let UltraCalcFunctionTextJoin = /*@__PURE__*/ (() => {
    class UltraCalcFunctionTextJoin extends BuiltInFunctionBase {
        evaluate(a, b) {
            let c = CalcFunctionUtilities.p(b - 2);
            let d = b - 2;
            let e = new List$1(ExcelCalcValue.$, 2, d);
            for (let f = 0; f < d; f++) {
                e.add(a.pop());
            }
            e.w();
            let g = a.pop();
            let h = a.pop();
            if (h.isError) {
                return new ExcelCalcValue(h.toErrorValue());
            }
            if (g.isError) {
                return new ExcelCalcValue(g.toErrorValue());
            }
            let i;
            if (g.isString) {
                if (!((() => { let j = tryParseBool(g.toString(), i); i = j.p1; return j.ret; })())) {
                    return new ExcelCalcValue(new ExcelCalcErrorValue(1));
                }
            }
            else {
                i = g._toBoolean1();
            }
            for (let j = 0; j < e.count; j++) {
                ExcelCalcFunction._ac(c, e._inner[j], i, false);
            }
            let k;
            if ((h.isReference && h.toReference().isEnumerable) || h.isArray || h.isArrayGroup) {
                let l = CalcFunctionUtilities.p(10);
                ExcelCalcFunction._ac(l, h, false, false);
                k = new Array(l.count);
                for (let m = 0; m < k.length; m++) {
                    k[m] = l._inner[m].toString();
                }
            }
            else {
                k = new Array(1);
                k[0] = h.toString();
            }
            let n = new StringBuilder(0);
            for (let o = 0; o < c.count; o++) {
                if (o > 0) {
                    n.l(k[(o - 1) % k.length]);
                }
                let p = c._inner[o];
                if (p.isError) {
                    return new ExcelCalcValue(p.toErrorValue());
                }
                n.l(p.toString());
            }
            if (n.c > 32767) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(1));
            }
            return new ExcelCalcValue(n.toString());
        }
        get_name() {
            return "textjoin";
        }
        get name() {
            return this.get_name();
        }
        get_minArgs() {
            return 3;
        }
        get minArgs() {
            return this.get_minArgs();
        }
        get_maxArgs() {
            return 0x7FFFFFFF;
        }
        get maxArgs() {
            return this.get_maxArgs();
        }
    }
    UltraCalcFunctionTextJoin.$t = markType(UltraCalcFunctionTextJoin, 'UltraCalcFunctionTextJoin', BuiltInFunctionBase.$);
    return UltraCalcFunctionTextJoin;
})();
