// Copyright 2026 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.
/* eslint-disable */
import { Component } from 'preact';
import { globalCommitContext } from './commit-context.js';
import { PerfSpecificKey, markTimingLegacy } from './performance.js';
import { NEXT_STATE } from '../shared/render-constants.js';
const OriginalSetStateKey = '__reactLynxOriginalSetState';
function reportRefDeprecationError(fnName, newFnName) {
    if (__DEV__ && !__DISABLE_CREATE_SELECTOR_QUERY_INCOMPATIBLE_WARNING__) {
        lynx.reportError(new Error(`${fnName} is deprecated and has different behavior in ReactLynx 3.0, please use ref or ${newFnName} instead.`));
    }
}
function getReactAppInstance() {
    return lynxCoreInject.tt;
}
function getLegacyNativeApp() {
    return lynxCoreInject.tt._nativeApp;
}
function getLegacyReactComponent() {
    return lynxCoreInject.tt._reactLynx.ReactComponent.prototype;
}
function createLegacyComponentReceiver(reactAppInstance, nativeApp) {
    return {
        _type: '',
        _nativeApp: nativeApp,
        _uiModule: nativeApp.nativeModuleProxy.LynxUIMethodModule,
        _reactAppInstance: reactAppInstance,
    };
}
function installComponentCompat() {
    if (!__JS__) {
        return;
    }
    const __Component = Component;
    const reactAppInstance = getReactAppInstance();
    __Component.prototype._reactAppInstance = reactAppInstance;
    __Component.prototype.getNodeRef = function (a, b) {
        reportRefDeprecationError('getNodeRef', 'lynx.createSelectorQuery');
        return getLegacyReactComponent().getNodeRef.call(createLegacyComponentReceiver(getReactAppInstance(), getLegacyNativeApp()), a, b);
    };
    __Component.prototype.getNodeRefFromRoot = function (a) {
        reportRefDeprecationError('getNodeRefFromRoot', 'lynx.createSelectorQuery');
        return getLegacyReactComponent().getNodeRefFromRoot.call(createLegacyComponentReceiver(getReactAppInstance(), getLegacyNativeApp()), a);
    };
    __Component.prototype.registerModule = function (name, module) {
        this._reactAppInstance.registerModule(name, module);
    };
    __Component.prototype.getJSModule = function (name) {
        return this._reactAppInstance.getJSModule(name);
    };
    __Component.prototype.addGlobalEventListener = function (eventName, callback, context) {
        return this._reactAppInstance.getJSModule('GlobalEventEmitter').addListener(eventName, callback, context);
    };
    __Component.prototype.getElementById = function (id) {
        reportRefDeprecationError('getElementById', 'lynx.getElementById');
        return lynx.getElementById(id);
    };
    __Component.prototype.GlobalEventEmitter = reactAppInstance.GlobalEventEmitter;
    __Component.prototype.createSelectorQuery = function () {
        reportRefDeprecationError('createSelectorQuery on component instance', 'lynx.createSelectorQuery');
        return lynx.createSelectorQuery();
    };
    const oldSetState = __Component.prototype[OriginalSetStateKey] ?? __Component.prototype.setState;
    __Component.prototype[OriginalSetStateKey] = oldSetState;
    __Component.prototype.setState = function (state, callback) {
        oldSetState.call(this, state, callback);
        // @ts-ignore
        const timingFlag = this[NEXT_STATE][PerfSpecificKey];
        if (timingFlag) {
            globalCommitContext.flushOptions.__lynx_timing_flag = timingFlag;
            markTimingLegacy('updateSetStateTrigger', timingFlag);
            this[NEXT_STATE][PerfSpecificKey] = '';
        }
    };
}
export { installComponentCompat };
//# sourceMappingURL=component.js.map