// 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.
/**
 * Snapshot manager that manages all snapshot definitions.
 */
import { DEFAULT_ENTRY_NAME } from './constants.js';
import { DynamicPartType, __DynamicPartChildren_0 } from './dynamicPartType.js';
import { entryUniqID } from './utils.js';
import { SnapshotOperation, __globalSnapshotPatch } from '../lifecycle/patch/snapshotPatch.js';
export let __page;
export let __pageId = 0;
export function setupPage(page) {
    __page = page;
    __pageId = __GetElementUniqueID(page);
}
export function clearPage() {
    __page = undefined;
    __pageId = 0;
}
/**
 * Manager for snapshot definitions
 */
export const snapshotManager = {
    values: /* @__PURE__ */ new Map([
        [
            'root',
            {
                create() {
                    /* v8 ignore start */
                    if (__JS__ && !__DEV__) {
                        return [];
                    }
                    /* v8 ignore stop */
                    return [__page];
                },
                update: [],
                slot: __DynamicPartChildren_0,
                isListHolder: false,
                cssId: 0,
            },
        ],
        [
            'wrapper',
            {
                create() {
                    /* v8 ignore start */
                    if (__JS__ && !__DEV__) {
                        return [];
                    }
                    /* v8 ignore stop */
                    return [__CreateWrapperElement(__pageId)];
                },
                update: [],
                slot: __DynamicPartChildren_0,
                isListHolder: false,
            },
        ],
        [
            null,
            {
                create() {
                    /* v8 ignore start */
                    if (__JS__ && !__DEV__) {
                        return [];
                    }
                    /* v8 ignore stop */
                    return [__CreateRawText('')];
                },
                update: [
                    ctx => {
                        /* v8 ignore start */
                        if (__JS__ && !__DEV__) {
                            return;
                        }
                        /* v8 ignore stop */
                        if (ctx.__elements) {
                            __SetAttribute(ctx.__elements[0], 'text', ctx.__values[0]);
                        }
                    },
                ],
                slot: [],
                isListHolder: false,
            },
        ],
    ]),
};
/**
 * Creates a new snapshot definition and adds it to the manager
 */
export function createSnapshot(uniqID, create, update, slot, cssId, entryName, refAndSpreadIndexes, isLazySnapshotSupported = false) {
    if (!isLazySnapshotSupported) {
        uniqID = entryUniqID(uniqID, entryName);
    }
    // For Lazy Bundle, their entryName is not DEFAULT_ENTRY_NAME.
    // We need to set the entryName correctly for HMR
    if (__DEV__ && __JS__ && __globalSnapshotPatch && entryName && entryName !== DEFAULT_ENTRY_NAME
        // `uniqID` will be `https://example.com/main.lynx.bundle:__snapshot_835da_eff1e_1` when loading a standalone lazy bundle after hydration.
        && !uniqID.includes(':')) {
        __globalSnapshotPatch.push(SnapshotOperation.DEV_ONLY_SetSnapshotEntryName, uniqID, entryName);
    }
    const s = { create, update, slot, cssId, entryName, refAndSpreadIndexes };
    snapshotManager.values.set(uniqID, s);
    if (slot && slot[0]) {
        const v = slot[0][0];
        if (v === DynamicPartType.ListChildren || v === DynamicPartType.ListSlotV2) {
            s.isListHolder = true;
        }
        s.isSlotV2 = slot.every(([type]) => type === DynamicPartType.SlotV2 || type === DynamicPartType.ListSlotV2);
    }
    return uniqID;
}
//# sourceMappingURL=definition.js.map