// 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.
import { __etAttrPlanMap, adaptMTEventAttrSlot } from './attr-slot-plan.js';
import { retainMTEventBackgroundFunctionCtx } from './main-thread-background-function.js';
import { isMTEventNativeWrapper } from './main-thread-event-ctx.js';
const dynamicAttrState = new Map();
const mtEventSlotsByHandle = new Map();
function deleteSlotState(handleId, attrSlotIndex) {
    const handleState = dynamicAttrState.get(handleId);
    if (!handleState) {
        return;
    }
    handleState.delete(attrSlotIndex);
    if (handleState.size === 0) {
        dynamicAttrState.delete(handleId);
    }
}
function setSlotState(handleId, attrSlotIndex, nativeHeldValue) {
    retainMTEventBackgroundFunctionCtx(nativeHeldValue);
    let handleState = dynamicAttrState.get(handleId);
    if (!handleState) {
        handleState = new Map();
        dynamicAttrState.set(handleId, handleState);
    }
    handleState.set(attrSlotIndex, {
        kind: 'mt-event',
        nativeHeldValue,
    });
}
function getMTEventAttrSlotIndexes(templateType) {
    const attrPlan = __etAttrPlanMap[templateType];
    if (!attrPlan) {
        return undefined;
    }
    let slots;
    for (let planIndex = 0; planIndex < attrPlan.length; planIndex += 2) {
        if (attrPlan[planIndex + 1] !== adaptMTEventAttrSlot) {
            continue;
        }
        slots ??= new Set();
        slots.add(attrPlan[planIndex]);
    }
    return slots;
}
export function clearMainThreadDynamicAttrState() {
    dynamicAttrState.clear();
    mtEventSlotsByHandle.clear();
}
export function getMainThreadDynamicAttrState(handleId, attrSlotIndex) {
    return dynamicAttrState.get(handleId)?.get(attrSlotIndex);
}
export function initializeMainThreadDynamicAttrSlots(handleId, templateType, attributeSlots) {
    const mtEventSlots = getMTEventAttrSlotIndexes(templateType);
    if (!mtEventSlots) {
        return;
    }
    mtEventSlotsByHandle.set(handleId, mtEventSlots);
    for (const attrSlotIndex of mtEventSlots) {
        const value = attributeSlots?.[attrSlotIndex];
        if (isMTEventNativeWrapper(value)) {
            setSlotState(handleId, attrSlotIndex, value.value);
        }
    }
}
export function updateMainThreadDynamicAttrSlot(handleId, attrSlotIndex, value, isHydration = false) {
    if (mtEventSlotsByHandle.get(handleId)?.has(attrSlotIndex) !== true) {
        return undefined;
    }
    const previousState = dynamicAttrState.get(handleId)?.get(attrSlotIndex);
    if (!isMTEventNativeWrapper(value)) {
        deleteSlotState(handleId, attrSlotIndex);
        return undefined;
    }
    setSlotState(handleId, attrSlotIndex, value.value);
    if (isHydration && previousState) {
        return {
            kind: 'mt-event',
            nextValue: value.value,
            previousNativeHeldValue: previousState.nativeHeldValue,
        };
    }
    return undefined;
}
export function deleteMainThreadDynamicAttrStateForSubtree(handleIds) {
    for (const handleId of handleIds) {
        mtEventSlotsByHandle.delete(handleId);
        dynamicAttrState.delete(handleId);
    }
}
//# sourceMappingURL=main-thread-dynamic-attr-state.js.map