// 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 { render } from 'preact';
import { createContext, createElement } from 'preact/compat';
import { useState } from 'preact/hooks';
import { factory, withInitDataInState } from './compat/initData.js';
import { profileEnd, profileStart } from './debug/utils.js';
import { useLynxGlobalEventListener } from './hooks/useLynxGlobalEventListener.js';
import { LifecycleConstant } from './lifecycleConstant.js';
import { flushDelayedLifecycleEvents } from './lynx/tt.js';
import { __root } from './root.js';
/**
 * The default and only root of ReactLynx for you to render JSX
 * @example
 * ```ts
 * import { root } from "@lynx-js/react"
 * ```
 *
 * @public
 */
export const root = {
    render: (jsx) => {
        /* v8 ignore next 2 */
        if (typeof __MAIN_THREAD__ !== 'undefined' && __MAIN_THREAD__) {
            __root.__jsx = jsx;
        }
        else {
            __root.__jsx = jsx;
            if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
                profileStart('ReactLynx::renderBackground');
            }
            // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
            render(jsx, __root);
            if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
                profileEnd();
            }
            if (__FIRST_SCREEN_SYNC_TIMING__ === 'immediately') {
                // This is for cases where `root.render()` is called asynchronously,
                // `firstScreen` message might have been reached.
                flushDelayedLifecycleEvents();
            }
            else {
                lynx.getNativeApp().callLepusMethod(LifecycleConstant.jsReady, {});
            }
        }
    },
    /* v8 ignore next 3 */
    registerDataProcessors: (dataProcessorDefinition) => {
        lynx.registerDataProcessors(dataProcessorDefinition);
    },
};
const _InitData = /* @__PURE__ */ factory({
    createContext,
    useState,
    createElement,
    useLynxGlobalEventListener,
}, '__initData', 'onDataChanged');
/**
 * The {@link https://react.dev/reference/react/createContext#provider | Provider} Component that provide `initData`,
 * you must wrap your JSX inside it
 * @group Components
 *
 * @example
 *
 * ```ts
 * import { root } from "@lynx-js/react"
 *
 * function App() {
 *   return (
 *     <InitDataConsumer children={(initData) => <view>...</view>}/>
 *   )
 * }
 *
 * root.render(
 *   <InitDataProvider>
 *      <App/>
 *   </InitDataProvider>
 * );
 *
 * ```
 *
 * @public
 */
// @ts-expect-error make preact and react types work
export const InitDataProvider = /* @__PURE__ */ _InitData.Provider();
/**
 * The {@link https://react.dev/reference/react/createContext#consumer | Consumer} Component that provide `initData`.
 * This should be used with {@link InitDataProvider}
 * @group Components
 * @public
 */
// @ts-expect-error make preact and react types work
export const InitDataConsumer = /* @__PURE__ */ _InitData.Consumer();
/**
 * A React Hooks for you to get `initData`.
 * If `initData` is changed, a re-render will be triggered automatically.
 *
 * @example
 *
 * ```ts
 * function App() {
 *   const initData = useInitData();
 *
 *   initData.someProperty // use it
 * }
 * ```
 *
 * @public
 */
export const useInitData = /* @__PURE__ */ _InitData.use();
/**
 * A React Hooks for you to get notified when `initData` changed.
 *
 * @example
 * ```ts
 * function App() {
 *   useInitDataChanged((data) => {
 *     data.someProperty // can use it
 *   })
 * }
 * ```
 * @public
 */
export const useInitDataChanged = /* @__PURE__ */ _InitData.useChanged();
export { withInitDataInState };
export { useLynxGlobalEventListener } from './hooks/useLynxGlobalEventListener.js';
export { runOnBackground } from './worklet/call/runOnBackground.js';
export { runOnMainThread } from './worklet/call/runOnMainThread.js';
export { MainThreadRef, useMainThreadRef } from './worklet/ref/workletRef.js';
//# sourceMappingURL=lynx-api.js.map