// 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 { formatElementTemplateUpdateCommands } from '../debug/alog.js';
import { markTiming, setPipeline } from '../lynx/performance.js';
import { ElementTemplateLifecycleConstant } from '../protocol/lifecycle-constant.js';
import { __page } from '../runtime/page/page.js';
import { applyElementTemplateUpdateCommands } from '../runtime/patch.js';
let listener;
export function installElementTemplatePatchListener() {
    resetElementTemplatePatchListener();
    listener = (event) => {
        const { data } = event;
        const payload = data;
        const hasOps = Array.isArray(payload?.ops);
        const flushOptions = payload?.flushOptions ?? {};
        const pipelineOptions = flushOptions.pipelineOptions;
        setPipeline(pipelineOptions);
        const flowIds = Array.isArray(payload?.flowIds) && payload.flowIds.length > 0
            ? payload.flowIds
            : undefined;
        const shouldProfilePatch = hasOps
            && !!flowIds
            && typeof lynx.performance?.profileStart === 'function'
            && typeof lynx.performance?.profileEnd === 'function';
        if (shouldProfilePatch) {
            lynx.performance.profileStart('ReactLynx::patch', {
                flowId: flowIds[0],
                flowIds,
            });
        }
        if (hasOps) {
            if (typeof __ALOG__ !== 'undefined' && __ALOG__) {
                console.alog?.('[ReactLynxDebug] ElementTemplate main-thread patch:\n'
                    + JSON.stringify({
                        ops: formatElementTemplateUpdateCommands(payload.ops),
                        flushOptions,
                        flowIds,
                    }, null, 2));
            }
            markTiming('mtsRenderStart');
            markTiming('parseChangesStart');
            markTiming('parseChangesEnd');
            markTiming('patchChangesStart');
            try {
                applyElementTemplateUpdateCommands(payload.ops);
            }
            finally {
                markTiming('patchChangesEnd');
                markTiming('mtsRenderEnd');
            }
        }
        __FlushElementTree(__page, flushOptions);
        if (shouldProfilePatch) {
            lynx.performance.profileEnd();
        }
    };
    lynx.getJSContext().addEventListener(ElementTemplateLifecycleConstant.update, listener);
}
export function resetElementTemplatePatchListener() {
    if (listener) {
        lynx.getJSContext().removeEventListener(ElementTemplateLifecycleConstant.update, listener);
    }
    listener = undefined;
}
//# sourceMappingURL=patch-listener.js.map