"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDomName = exports.parseReactDomNode = exports.getLatestFiber = exports.parseReactDomNodeDeep = exports.addCss = exports.setStyle = void 0;
const lodash_es_1 = require("lodash-es");
function setStyle(dom, style) {
    const styleKeys = Object.keys(style);
    const cachedStyle = (0, lodash_es_1.pick)(dom.style, styleKeys);
    Object.assign(dom.style, style);
    return () => {
        Object.assign(dom.style, cachedStyle);
    };
}
exports.setStyle = setStyle;
function addCss(dom, cls) {
    dom.classList.add(cls);
    return () => {
        dom.classList.remove(cls);
    };
}
exports.addCss = addCss;
function parseReactDomNodeDeep(dom) {
    var _a, _b;
    while (dom) {
        const data = parseReactDomNode(dom);
        if (!data) {
            dom = dom.parentElement;
            continue;
        }
        if (!!data.mometa) {
            return data;
        }
        if (data.fiber) {
            let fiber = data.fiber;
            while (fiber && !((_a = fiber === null || fiber === void 0 ? void 0 : fiber._debugSource) === null || _a === void 0 ? void 0 : _a.__mometa)) {
                fiber = fiber.return;
            }
            if (fiber) {
                return {
                    mometa: (_b = fiber._debugSource) === null || _b === void 0 ? void 0 : _b.__mometa,
                    fiber
                };
            }
        }
        dom = dom.parentElement;
    }
}
exports.parseReactDomNodeDeep = parseReactDomNodeDeep;
function getLatestFiber(f) {
    if (!f) {
        return f;
    }
    if (f.alternate) {
        return f.alternate.actualStartTime > f.actualStartTime ? f.alternate : f;
    }
    return f;
}
exports.getLatestFiber = getLatestFiber;
const domMap = new WeakMap();
function parseReactDomNode(dom) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    const cachedName = domMap.get(dom);
    if (cachedName) {
        const { fiberName, propName } = cachedName;
        if (propName && ((_a = dom[propName]) === null || _a === void 0 ? void 0 : _a.__mometa)) {
            return {
                fiber: !!fiberName && getLatestFiber(dom[fiberName]),
                mometa: (_b = dom[propName]) === null || _b === void 0 ? void 0 : _b.__mometa
            };
        }
        if (fiberName && ((_d = (_c = dom[fiberName]) === null || _c === void 0 ? void 0 : _c._debugSource) === null || _d === void 0 ? void 0 : _d.__mometa)) {
            return {
                fiber: getLatestFiber(dom[cachedName === null || cachedName === void 0 ? void 0 : cachedName.fiberName]),
                mometa: (_f = (_e = getLatestFiber(dom[cachedName === null || cachedName === void 0 ? void 0 : cachedName.fiberName])) === null || _e === void 0 ? void 0 : _e._debugSource) === null || _f === void 0 ? void 0 : _f.__mometa
            };
        }
    }
    const fiberName = Object.keys(dom).find((name) => /^__reactFiber\$.+$/.test(name));
    if (fiberName && ((_h = (_g = dom[fiberName]) === null || _g === void 0 ? void 0 : _g._debugSource) === null || _h === void 0 ? void 0 : _h.__mometa)) {
        domMap.set(dom, { fiberName });
        return {
            fiber: getLatestFiber(dom[fiberName]),
            mometa: (_k = (_j = getLatestFiber(dom[fiberName])) === null || _j === void 0 ? void 0 : _j._debugSource) === null || _k === void 0 ? void 0 : _k.__mometa
        };
    }
    const propName = Object.keys(dom).find((name) => /^__reactProps\$.+$/.test(name));
    if (propName && ((_l = dom[propName]) === null || _l === void 0 ? void 0 : _l.__mometa)) {
        domMap.set(dom, { propName, fiberName });
        return {
            fiber: !!fiberName && dom[fiberName],
            mometa: (_m = dom[propName]) === null || _m === void 0 ? void 0 : _m.__mometa
        };
    }
    if (fiberName) {
        return {
            fiber: dom[fiberName]
        };
    }
}
exports.parseReactDomNode = parseReactDomNode;
function getDomName(dom) {
    var _a;
    let name = dom.localName;
    if (dom.getAttribute('id')) {
        name += `#${dom.getAttribute('id')}`;
        return name;
    }
    if ((_a = dom.getAttribute('class')) === null || _a === void 0 ? void 0 : _a.trim()) {
        name += `.${Array.from(dom.classList.entries()).join('.')}`;
    }
    return name;
}
exports.getDomName = getDomName;
//# sourceMappingURL=dom-utils.js.map