/*
 * Application Insights JavaScript SDK - Debug Plugin, 3.3.7-nightly3.2505-06
 * Copyright (c) Microsoft and contributors. All rights reserved.
 */
import { isArray, isBoolean, isError, isNumber, isObject, isString, objToString } from "@microsoft/applicationinsights-core-js";
import { _DYN_LENGTH, _DYN_RENDER, _DYN_REPLACE } from "../__DynamicConstants";
import { MAX_DEPTH, formatLogElements, getTargetKeys, getTargetName, makeRegex, toggleClassName, traverseAndReplace } from "./helpers";
var LogEntry = /** @class */ (function () {
    function LogEntry(target, tm, key, level, kind, keep) {
        var _self = this;
        var searchContent = null;
        var lastTextFilter;
        var theEl = null;
        _self.isKeep = function () {
            return !!keep;
        };
        function _testValue(rg, value) {
            if (rg && value) {
                return rg.exec(value) !== null;
            }
            return false;
        }
        function _testObj(rg, value, excludedKeys, includeFunctions) {
            if (value !== null && value !== undefined && value !== "") {
                if (isArray(value)) {
                    for (var lp = 0; lp < value[_DYN_LENGTH /* @min:%2elength */]; lp++) {
                        if (_testObj(rg, value[lp], excludedKeys, includeFunctions)) {
                            return true;
                        }
                    }
                }
                else if (isObject(value) || isError(value)) {
                    var keys = getTargetKeys(value, excludedKeys, includeFunctions);
                    for (var lp = 0; lp < keys[_DYN_LENGTH /* @min:%2elength */]; lp++) {
                        var key_1 = keys[lp];
                        if (_testValue(rg, key_1) || _testObj(rg, value[key_1], excludedKeys, includeFunctions)) {
                            return true;
                        }
                    }
                }
                else if (isString(value) || isNumber(value) || isBoolean(value)) {
                    return _testValue(rg, objToString(value));
                }
            }
            return false;
        }
        _self.isMatch = function (textFilter, excludedKeys, includeFunctions) {
            var rg = makeRegex(textFilter);
            if (rg) {
                if (_testValue(rg, key)) {
                    return true;
                }
                if (_testValue(rg, getTargetName(_self))) {
                    return true;
                }
                if (!searchContent) {
                    // Delaying creating the search optimization until needed -- for performance
                    searchContent = traverseAndReplace(target, MAX_DEPTH, level, [], excludedKeys, includeFunctions);
                }
                return _testObj(rg, searchContent, excludedKeys, includeFunctions);
            }
            return true;
        };
        _self[_DYN_RENDER /* @min:%2erender */] = function (textFilter, excludeKeys, includeFunctions) {
            if (!theEl || lastTextFilter !== textFilter) {
                lastTextFilter = textFilter;
                var ms = "" + tm;
                while (ms[_DYN_LENGTH /* @min:%2elength */] < 4) {
                    ms = "0" + ms;
                }
                ms = ms[_DYN_REPLACE /* @min:%2ereplace */](/(.)(\d{3}){1}$/g, "$1.$2s");
                while (ms[_DYN_LENGTH /* @min:%2elength */] <= 9) {
                    ms = " " + ms;
                }
                theEl = formatLogElements(target, "[".concat(ms, "]"), key, level, textFilter, excludeKeys, [], includeFunctions).root;
                toggleClassName(theEl, " tree-root");
            }
            return theEl;
        };
        _self.getEl = function () {
            return theEl;
        };
        _self.getKind = function () {
            return kind;
        };
    }
    return LogEntry;
}());
export { LogEntry };
//# sourceMappingURL=LogEntry.js.map