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


export {$b75a14d79419a916$export$89be5a243e59c4b2 as Virtualizer};
//# sourceMappingURL=Virtualizer.js.map
