/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
import { isFunction, isString } from '@slardar/sdk-template';
import { getFullUrl, htmlTreeAsString } from '../../utils';
export function isHTMLLinkElement(target) {
    return target.tagName.toLowerCase() === 'link';
}
export function getElementAttr(target, attr) {
    if (isFunction(target.getAttribute)) {
        // returns what was in the HTML. It may be a relative URL.
        return target.getAttribute(attr) || '';
    }
    // target.src returns absoulute path
    return target[attr] || '';
}
export var getSrc = function (target) {
    return getElementAttr(target, isHTMLLinkElement(target) ? 'href' : 'src');
};
export var getDataFromEvent = function (e) {
    var target = (e.target || e.srcElement);
    if (!target) {
        return;
    }
    var tagName = target.tagName;
    if (!tagName || !isString(tagName)) {
        return;
    }
    var src = getSrc(target);
    return {
        url: src,
        tagName: tagName,
        xpath: src ? undefined : htmlTreeAsString(target),
    };
};
export var buildPayload = function (data, getEntriesByName) {
    var url = data.url, tagName = data.tagName, xpath = data.xpath;
    var fullUrl = getFullUrl(url);
    var timing = getEntriesByName(fullUrl)[0];
    return {
        type: tagName.toLowerCase(),
        url: fullUrl,
        xpath: xpath,
        timing: timing,
    };
};
//# sourceMappingURL=util.js.map