// Copyright 2025 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 { profileEnd, profileStart } from '../../shared/profile.js';
const elementTemplatePAPINameList = [
    '__CreateElementTemplate',
    '__SetAttributeOfElementTemplate',
    '__InsertNodeToElementTemplate',
    '__RemoveNodeFromElementTemplate',
    '__SerializeElementTemplate',
];
export function initElementTemplatePAPICallAlog(globalWithIndex = globalThis) {
    let count = 0;
    const elementTemplateMap = new Map();
    for (const elementTemplatePAPIName of elementTemplatePAPINameList) {
        const oldElementTemplatePAPI = globalWithIndex[elementTemplatePAPIName];
        if (typeof oldElementTemplatePAPI !== 'function') {
            continue;
        }
        const callElementTemplatePAPI = oldElementTemplatePAPI;
        globalWithIndex[elementTemplatePAPIName] = (...args) => {
            const shouldProfile = typeof __PROFILE__ !== 'undefined' && __PROFILE__;
            if (shouldProfile) {
                profileStart(`ElementTemplatePAPI: ${elementTemplatePAPIName}`, {
                    args: {
                        args: formatValue(args, elementTemplateMap),
                    },
                });
            }
            let result;
            try {
                result = callElementTemplatePAPI(...args);
            }
            finally {
                if (shouldProfile) {
                    profileEnd();
                }
            }
            if (elementTemplatePAPIName === '__CreateElementTemplate' && result != null) {
                elementTemplateMap.set(result, `${String(args[0])}#${String(args[4])}`);
            }
            const formattedResult = result == null ? undefined : formatValue(result, elementTemplateMap);
            console.alog?.(`[ReactLynxDebug] ElementTemplate API call #${++count}: ${elementTemplatePAPIName}(${args.map(arg => formatValue(arg, elementTemplateMap)).join(', ')})${formattedResult == null ? '' : ` => ${formattedResult}`}`);
            return result;
        };
    }
}
function formatValue(value, elementTemplateMap) {
    if (elementTemplateMap.has(value)) {
        return elementTemplateMap.get(value);
    }
    if (value === undefined) {
        return 'undefined';
    }
    if (value === null) {
        return 'null';
    }
    if (Array.isArray(value)) {
        return '[' + value.map(item => formatValue(item, elementTemplateMap)).join(', ') + ']';
    }
    if (typeof value === 'function') {
        return `[Function${value.name ? ` ${value.name}` : ''}]`;
    }
    if (typeof value === 'symbol') {
        return value.toString();
    }
    try {
        const formatted = JSON.stringify(value);
        return formatted ?? Object.prototype.toString.call(value);
    }
    catch {
        return Object.prototype.toString.call(value);
    }
}
//# sourceMappingURL=elementPAPICall.js.map