import { __read } from "tslib";
import { getRegexp } from '@slardar/sdk-template';
import { getDefaultPerformance, applyPerformance, getDefaultBrowser, UNKNOWN_PATH } from '../../utils';
import { getDataFromEvent, buildPayload } from './util';
var RESOURCE_ERROR_EV_TYPE = 'resource_error';
export var resourceErrorGetterWithContext = function (report, tearDownGroup, _a, config) {
    var _b = __read(_a, 1), errorObserver = _b[0];
    var window = getDefaultBrowser();
    if (!window)
        return;
    var ignoreUrls = config.ignoreUrls, includeUrls = config.includeUrls, dedupe = config.dedupe;
    var includeRegExp = getRegexp(includeUrls);
    var ignoreRegExp = getRegexp(ignoreUrls);
    var _c = __read(applyPerformance(getDefaultPerformance()), 5), getEntriesByName = _c[4];
    var lastErrPath = undefined;
    var reportResourceError = function (data) {
        var locationHref = location && location.href;
        if (locationHref && data.url === locationHref)
            return;
        if ((includeRegExp && !includeRegExp.test(data.url)) || (ignoreRegExp && ignoreRegExp.test(data.url)))
            return;
        // url为空时 && dom path为空 对排查错误没有意义
        if (!data.url && (!data.xpath || data.xpath === UNKNOWN_PATH))
            return;
        if (dedupe && data.url === lastErrPath)
            return;
        lastErrPath = data.url;
        // when the error occurs, the timing has been pushed into the buffer pool
        // so use getEntriesByName instead of resourceObserver
        var payload = buildPayload(data, getEntriesByName);
        if (payload) {
            report({
                ev_type: RESOURCE_ERROR_EV_TYPE,
                payload: payload,
            });
        }
    };
    tearDownGroup.push(errorObserver[0](function (event) {
        var e = event || window.event;
        if (!e) {
            return;
        }
        var data = getDataFromEvent(e);
        data && reportResourceError(data);
    }));
    return reportResourceError;
};
//# sourceMappingURL=index.js.map