// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
/**
 * Implements the IFR (Instant First-Frame Rendering) on main thread.
 */
import { isValidElement } from 'preact';
import { profileEnd, profileStart } from '../debug/utils.js';
import { renderOpcodesInto } from '../opcodes.js';
import { render as renderToString } from '../renderToOpcodes/index.js';
import { __root } from '../root.js';
import { SnapshotInstance } from '../snapshot.js';
function renderMainThread() {
    let opcodes;
    try {
        if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
            profileStart('ReactLynx::renderMainThread');
        }
        opcodes = renderToString(__root.__jsx, undefined);
    }
    catch (e) {
        lynx.reportError(e);
        opcodes = [];
    }
    finally {
        if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
            profileEnd();
        }
    }
    if (process.env['NODE_ENV'] === 'test') {
        opcodes = opcodes.map((opcode) => {
            if (isValidElement(opcode) && typeof opcode.type === 'string') {
                return Object.assign(new SnapshotInstance(opcode.type), opcode, { $$typeof: undefined });
            }
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return
            return opcode;
        });
    }
    if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
        profileStart('ReactLynx::renderOpcodes');
    }
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    renderOpcodesInto(opcodes, __root);
    if (__ENABLE_SSR__) {
        __root.__opcodes = opcodes;
    }
    if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
        profileEnd();
    }
}
export { renderMainThread };
//# sourceMappingURL=render.js.map