import { profileEnd, profileStart } from '../debug/profile.js';
export function setupLynxEnv() {
    if (!__LEPUS__) {
        let initData = {};
        let updateData = {};
        try {
            const params = lynxCoreInject
                ?.tt?._params;
            if (params) {
                initData = params.initData ?? {};
                updateData = params.updateData ?? {};
            }
        }
        catch { }
        lynx.__initData = { ...initData, ...updateData };
        lynx.registerDataProcessors = function () { };
    }
    if (__LEPUS__) {
        lynx.__initData = {
        /* available only in renderPage */
        };
        lynx.triggerGlobalEventFromLepus = function (eventName, params) {
            __OnLifecycleEvent(['globalEventFromLepus', [eventName, params]]);
        };
        {
            // eslint-disable-next-line unicorn/consistent-function-scoping
            function __name(empty) {
                return `Native${empty}Modules`;
            }
            // TODO(hongzhiyuan.hzy): make sure this is run before any other code (especially code access `NativeModules`)
            // @ts-expect-error hack
            if (typeof globalThis[__name('')] === 'undefined') {
                // @ts-expect-error hack
                globalThis[__name('')] = undefined;
            }
        }
        lynx.registerDataProcessors = function (dataProcessorDefinition) {
            let hasDefaultDataProcessorExecuted = false;
            globalThis.processData = (data, processorName) => {
                if (__PROFILE__) {
                    profileStart('processData');
                }
                let r;
                try {
                    if (processorName) {
                        r = dataProcessorDefinition?.dataProcessors?.[processorName]?.(data) ?? data;
                    }
                    else {
                        r = dataProcessorDefinition?.defaultDataProcessor?.(data) ?? data;
                    }
                }
                catch (e) {
                    lynx.reportError(e);
                    r = {};
                }
                if (__PROFILE__) {
                    profileEnd();
                }
                if (hasDefaultDataProcessorExecuted === false) {
                    // @ts-expect-error todo: add types to i18n logic
                    if (globalThis.__I18N_RESOURCE_TRANSLATION__) {
                        r = {
                            ...r,
                            // @ts-expect-error todo: add types to i18n logic
                            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
                            __I18N_RESOURCE_TRANSLATION__: globalThis.__I18N_RESOURCE_TRANSLATION__,
                        };
                    }
                    // @ts-expect-error todo: add types to __EXTRACT_STR__
                    if (__EXTRACT_STR__) {
                        r = {
                            ...r,
                            // @ts-expect-error todo: add types to __EXTRACT_STR__
                            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
                            _EXTRACT_STR: __EXTRACT_STR_IDENT_FLAG__,
                        };
                    }
                }
                if (processorName) { }
                else {
                    hasDefaultDataProcessorExecuted = true;
                }
                return r;
            };
        };
        lynx.registerDataProcessors();
    }
}
//# sourceMappingURL=env.js.map