// 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.
import { reloadMainThread } from './reload-main-thread.js';
import { applyUpdatePageData } from '../../core/lynx-page-data.js';
import { __page, createElementTemplatePage, setupPage } from '../runtime/page/page.js';
import { renderMainThread, resetMainThreadRootRefs } from '../runtime/render/render-main-thread.js';
function injectCalledByNative() {
    const calledByNative = {
        renderPage,
        updatePage,
        updateGlobalProps,
        getPageData: function () {
            return null;
        },
        removeComponents: function () { },
    };
    Object.assign(globalThis, calledByNative);
}
function renderPage(data) {
    lynx.__initData = data ?? {};
    setupPage(createElementTemplatePage());
    resetMainThreadRootRefs();
    renderMainThread();
}
function updatePage(data, options) {
    if (__FIRST_SCREEN_SYNC_TIMING__ !== 'immediately') {
        return;
    }
    if (options?.reloadTemplate) {
        reloadMainThread(data, options);
        return;
    }
    applyUpdatePageData(data, options);
    __FlushElementTree(__page, options ?? {});
}
function updateGlobalProps(_data, options) {
    if (options) {
        __FlushElementTree(__page, options);
    }
    else {
        __FlushElementTree();
    }
}
/**
 * @internal
 */
export { injectCalledByNative };
//# sourceMappingURL=main-thread-api.js.map