import {CollectionRendererContext as $263ab7fc0f95ccdb$export$4feb769f8ddf26c5, renderAfterDropIndicators as $263ab7fc0f95ccdb$export$2dbbd341daed716d} from "./Collection.mjs";
import {useVirtualizerState as $2DTaS$useVirtualizerState} from "react-stately/useVirtualizerState";
import $2DTaS$react, {createContext as $2DTaS$createContext, useMemo as $2DTaS$useMemo, useContext as $2DTaS$useContext} from "react";
import {useScrollView as $2DTaS$useScrollView} from "react-aria/private/virtualizer/ScrollView";
import {VirtualizerItem as $2DTaS$VirtualizerItem} from "react-aria/private/virtualizer/VirtualizerItem";

/*
 * Copyright 2024 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */ 




const $143fb904d86051ce$var$VirtualizerContext = /*#__PURE__*/ (0, $2DTaS$createContext)(null);
const $143fb904d86051ce$var$LayoutContext = /*#__PURE__*/ (0, $2DTaS$createContext)(null);
function $143fb904d86051ce$export$89be5a243e59c4b2(props) {
    let { children: children, layout: layoutProp, layoutOptions: layoutOptions } = props;
    let layout = (0, $2DTaS$useMemo)(()=>typeof layoutProp === 'function' ? new layoutProp() : layoutProp, [
        layoutProp
    ]);
    let renderer = (0, $2DTaS$useMemo)(()=>({
            isVirtualized: true,
            layoutDelegate: layout,
            dropTargetDelegate: layout.getDropTargetFromPoint ? layout : undefined,
            CollectionRoot: $143fb904d86051ce$var$CollectionRoot,
            CollectionBranch: $143fb904d86051ce$var$CollectionBranch
        }), [
        layout
    ]);
    return /*#__PURE__*/ (0, $2DTaS$react).createElement((0, $263ab7fc0f95ccdb$export$4feb769f8ddf26c5).Provider, {
        value: renderer
    }, /*#__PURE__*/ (0, $2DTaS$react).createElement($143fb904d86051ce$var$LayoutContext.Provider, {
        value: {
            layout: layout,
            layoutOptions: layoutOptions
        }
    }, children));
}
function $143fb904d86051ce$var$CollectionRoot({ collection: collection, persistedKeys: persistedKeys, scrollRef: scrollRef, renderDropIndicator: renderDropIndicator }) {
    let { layout: layout, layoutOptions: layoutOptions } = (0, $2DTaS$useContext)($143fb904d86051ce$var$LayoutContext);
    let layoutOptions2 = layout.useLayoutOptions?.();
    let state = (0, $2DTaS$useVirtualizerState)({
        layout: layout,
        collection: collection,
        renderView: (type, item)=>{
            return item?.render?.(item);
        },
        onVisibleRectChange (rect) {
            let element = scrollRef?.current;
            if (element) {
                element.scrollLeft = rect.x;
                element.scrollTop = rect.y;
            }
        },
        persistedKeys: persistedKeys,
        layoutOptions: (0, $2DTaS$useMemo)(()=>{
            if (layoutOptions && layoutOptions2) return {
                ...layoutOptions,
                ...layoutOptions2
            };
            return layoutOptions || layoutOptions2;
        }, [
            layoutOptions,
            layoutOptions2
        ])
    });
    let { contentProps: contentProps } = (0, $2DTaS$useScrollView)({
        onVisibleRectChange: state.setVisibleRect,
        contentSize: state.contentSize,
        onScrollStart: state.startScrolling,
        onScrollEnd: state.endScrolling
    }, scrollRef);
    return /*#__PURE__*/ (0, $2DTaS$react).createElement("div", contentProps, /*#__PURE__*/ (0, $2DTaS$react).createElement($143fb904d86051ce$var$VirtualizerContext.Provider, {
        value: state
    }, $143fb904d86051ce$var$renderChildren(null, state.visibleViews, renderDropIndicator)));
}
function $143fb904d86051ce$var$CollectionBranch({ parent: parent, renderDropIndicator: renderDropIndicator }) {
    let virtualizer = (0, $2DTaS$useContext)($143fb904d86051ce$var$VirtualizerContext);
    let parentView = virtualizer.virtualizer.getVisibleView(parent.key);
    return $143fb904d86051ce$var$renderChildren(parentView, Array.from(parentView.children), renderDropIndicator);
}
function $143fb904d86051ce$var$renderChildren(parent, children, renderDropIndicator) {
    return children.map((view)=>$143fb904d86051ce$var$renderWrapper(parent, view, renderDropIndicator));
}
function $143fb904d86051ce$var$renderWrapper(parent, reusableView, renderDropIndicator) {
    let rendered = /*#__PURE__*/ (0, $2DTaS$react).createElement((0, $2DTaS$VirtualizerItem), {
        key: reusableView.key,
        layoutInfo: reusableView.layoutInfo,
        virtualizer: reusableView.virtualizer,
        parent: parent?.layoutInfo
    }, reusableView.rendered);
    let { collection: collection, layout: layout } = reusableView.virtualizer;
    let node = reusableView.content;
    if (node?.type === 'item' && renderDropIndicator && layout.getDropTargetLayoutInfo) rendered = /*#__PURE__*/ (0, $2DTaS$react).createElement((0, $2DTaS$react).Fragment, {
        key: reusableView.key
    }, $143fb904d86051ce$var$renderDropIndicatorWrapper(parent, reusableView, {
        type: 'item',
        key: reusableView.content.key,
        dropPosition: 'before'
    }, renderDropIndicator), rendered, (0, $263ab7fc0f95ccdb$export$2dbbd341daed716d)(collection, node, (target)=>$143fb904d86051ce$var$renderDropIndicatorWrapper(parent, reusableView, target, renderDropIndicator)));
    return rendered;
}
function $143fb904d86051ce$var$renderDropIndicatorWrapper(parent, reusableView, target, renderDropIndicator) {
    let indicator = renderDropIndicator(target);
    if (indicator) {
        let layoutInfo = reusableView.virtualizer.layout.getDropTargetLayoutInfo(target);
        indicator = /*#__PURE__*/ (0, $2DTaS$react).createElement((0, $2DTaS$VirtualizerItem), {
            layoutInfo: layoutInfo,
            virtualizer: reusableView.virtualizer,
            parent: parent?.layoutInfo
        }, indicator);
    }
    return indicator;
}


export {$143fb904d86051ce$export$89be5a243e59c4b2 as Virtualizer};
//# sourceMappingURL=Virtualizer.mjs.map
