"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComponentTreeFromElement = getComponentTreeFromElement;
function serializeComplexObject(obj, seen = new WeakSet()) {
    if (obj === null || obj === undefined) {
        return obj;
    }
    if (typeof obj === 'function') {
        return `[Function: ${obj.name || 'anonymous'}]`;
    }
    if (typeof obj !== 'object') {
        return obj;
    }
    if (obj.$$typeof && typeof obj.$$typeof === 'symbol') {
        const componentName = obj.type?.displayName || obj.type?.name || (typeof obj.type === 'string' ? obj.type : 'ReactElement');
        return `[ReactElement: <${componentName}>]`;
    }
    if (seen.has(obj)) {
        return '[Circular Reference]';
    }
    seen.add(obj);
    if (Array.isArray(obj)) {
        return obj.map(item => serializeComplexObject(item, seen));
    }
    const newObj = {};
    for (const key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            if (key === 'className') {
                continue;
            }
            newObj[key] = serializeComplexObject(obj[key], seen);
        }
    }
    return newObj;
}
function cleanupNullValues(obj) {
    if (obj === null || typeof obj !== 'object') {
        return obj;
    }
    if (Array.isArray(obj)) {
        return obj.map(cleanupNullValues);
    }
    const newObj = {};
    for (const key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            const value = obj[key];
            if (value !== null) {
                newObj[key] = cleanupNullValues(value);
            }
        }
    }
    return newObj;
}
function getFiberFromElement(element) {
    const fiberKey = Object.keys(element).find(key => key.startsWith('__reactFiber$'));
    return fiberKey ? element[fiberKey] : null;
}
function getComponentName(fiber) {
    const componentType = fiber.elementType;
    if (!componentType) {
        return 'FiberRoot';
    }
    if (typeof componentType === 'string') {
        return componentType;
    }
    return componentType.displayName || componentType.name || 'Anonymous';
}
function getComponentTreeFromElement(element) {
    let fiber = getFiberFromElement(element);
    if (!fiber) {
        return { error: 'Could not find React Fiber for the selected element.' };
    }
    const hierarchy = [];
    while (fiber) {
        const elementType = fiber.elementType;
        if (typeof elementType === 'function' || typeof elementType === 'string') {
            const name = getComponentName(fiber);
            hierarchy.push({
                name,
                props: serializeComplexObject(fiber.memoizedProps),
                state: serializeComplexObject(fiber.memoizedState),
            });
        }
        fiber = fiber.return;
    }
    if (hierarchy.length === 0) {
        return { error: 'Could not identify any components or elements in this element\'s hierarchy.' };
    }
    const cleanedHierarchy = hierarchy.map(item => ({
        ...item,
        props: cleanupNullValues(item.props),
        state: cleanupNullValues(item.state),
    }));
    return { hierarchy: cleanedHierarchy };
}
//# sourceMappingURL=inspector.js.map