/* @oracle/oraclejet-preact: undefined */
import { jsxs, Fragment, jsx } from 'preact/jsx-runtime';
import { Fragment as Fragment$1 } from 'preact';
import { memo, forwardRef } from 'preact/compat';
import { useRef, useCallback, useEffect, useLayoutEffect, useMemo, useState, useImperativeHandle } from 'preact/hooks';
import { m as mergeProps } from './mergeProps-88ea8306.js';
import { e as getItemContext, h as dragToScroll, i as handleSelectionRange, k as keyExtractor, f as findElementByKey, j as getViewportConfig, l as getPrevNextKeyByCount } from './collectionUtils-5313629f.js';
import { R as ReorderableContext } from './ReorderableContext-0daeea89.js';
import { a as useNavigation } from './useNavigation-b257c33a.js';
import './LayerHost-7931b424.js';
import { u as useSelection } from './useSelection-65134722.js';
import { g as getClientHints } from './clientHints-a42b510c.js';
import { c as classNames } from './classNames-4e12b00d.js';
import './DragAndDropStyles.styles.css';
import './ReorderableItemStyles.styles.css';
import { H as HiddenAccessible } from './HiddenAccessible-61197e7c.js';
import { L as LOADMORE_STYLE_CLASS, a as LoadMoreCollection } from './LoadMoreCollection-d9f22790.js';
import { multiVariantStyles, styles } from './PRIVATE_BaseCardView/themes/BaseCardViewStyles.css.js';
import { m as mergeRefs } from './refUtils-76275c69.js';
import { w as warn } from './logger-c92f309c.js';
import { a as allTabbableElements } from './tabbableUtils-c8311ec6.js';
import { u as useId } from './useId-03dbfdf0.js';
import { u as useAnimation } from './useAnimation-fe39685e.js';
import { u as useResizeObserver } from './useResizeObserver-13641643.js';
import { T as TabbableModeContext } from './TabbableModeContext-7d8ad946.js';
import { F as FocusTrap } from './FocusTrap-b59d5752.js';
import { u as useUser } from './useUser-06d34d19.js';
import { i as isKeyDefined, e as isSameKey, c as containsKey } from './keys-49c92dbb.js';
import './StyledCheckbox-d6c8f557.js';
import './SelectorStyles.styles.css';
import './CollectionInteractionContext-7cf19abc.js';
import { a as useReorderableItem, u as useReorderable } from './useReorderableItem-857636b7.js';
import { u as useReorderableContext } from './useReorderableContext-79b1c941.js';
import { G as Grid } from './Grid-9e4b4f19.js';
import { F as Flex } from './Flex-03129963.js';
import { cardViewVars } from './PRIVATE_BaseCardView/themes/BaseCardViewContract.css.js';
import { S as Skeleton } from './Skeleton-d6f0969e.js';
import './PRIVATE_BaseCardView/themes/redwood/BaseCardViewBaseTheme.styles.css';
import { u as useAddBusyState } from './useAddBusyState-7a6af61f.js';

/**
 * Item selector for CardView components
 */
const CARDVIEW_ITEM_SELECTOR = '[data-oj-card-item]';
const emptyKeys = { all: false, keys: new Set() };
/**
 * A mapping of gutterSize to concrete size in pixels
 */
const gutterSizeToPX = {
    xs: 12,
    sm: 16,
    md: 24,
    lg: 48,
    xl: 64
};
const getColCount = (gapWidth, cardWidth, rootWidth) => {
    if (!cardWidth || !rootWidth)
        return 0;
    // if we have n cards, we have n-1 gaps
    const colCount = Math.floor((rootWidth + gapWidth) / (gapWidth + cardWidth));
    return Math.max(1, colCount);
};

/**
 * The internal component used to render a reorderable item in CardView.
 */
function BaseCardViewReorderableItem({ children, itemKey, itemIndex, isCurrentKey, dragKey, setDragKey, onReorder, rootRef, gutterSize, columns }) {
    const itemRef = useRef(null);
    const isOneColumn = columns === 1;
    const isFirstColumn = itemIndex % columns === 0;
    const isLastColumn = itemIndex % columns === columns - 1;
    const variantClasses = multiVariantStyles({
        itemOverlayFront: gutterSize
    });
    const variantClassesVertical = multiVariantStyles({
        itemOverlayFrontVertical: gutterSize
    });
    const itemStyles = {
        itemOverlayFront: isOneColumn ? variantClassesVertical : variantClasses,
        itemOverlayRear: isOneColumn ? styles.itemOverlayRearVertical : styles.itemOverlayRear,
        itemDragger: styles.itemDragger
    };
    const getItem = useCallback(() => itemRef?.current?.closest(CARDVIEW_ITEM_SELECTOR), []);
    const setTransferData = useCallback((event) => {
        event.dataTransfer?.setData('text/cardview-items-key', JSON.stringify(itemKey));
    }, [itemKey]);
    const { rootProps, isDragged, overlayZoneBeforeProps, overlayZoneAfterProps } = useReorderableItem({
        isDisabled: onReorder ? false : true,
        dragKey,
        setDragKey,
        onReorder,
        rootRef,
        ref: itemRef,
        getItem,
        setTransferData,
        itemStyles,
        itemSelector: CARDVIEW_ITEM_SELECTOR,
        isVertical: isOneColumn
    });
    // TODO: remove this as part of JET-70023
    const { currentItemKey } = useReorderableContext();
    useEffect(() => {
        if (isCurrentKey) {
            currentItemKey.current = itemKey;
        }
    }, [isCurrentKey, itemKey, currentItemKey]);
    const { direction } = useUser();
    const isRtl = direction === 'rtl';
    const overlayZoneBeforeClass = classNames([
        overlayZoneBeforeProps?.class,
        !isOneColumn && !isRtl && isFirstColumn && styles.itemOverlayFrontEdge,
        !isOneColumn && isRtl && isLastColumn && styles.itemOverlayFrontEdge
    ]);
    const overlayZoneAfterClass = classNames([
        overlayZoneAfterProps?.class,
        !isOneColumn && !isRtl && isLastColumn && styles.itemOverlayRearEdge,
        !isOneColumn && isRtl && isFirstColumn && styles.itemOverlayRearEdge
    ]);
    return (jsxs("div", { ...rootProps, children: [children, !isDragged && isKeyDefined(dragKey) && (jsxs(Fragment, { children: [jsx("div", { ...overlayZoneBeforeProps, class: overlayZoneBeforeClass }), jsx("div", { ...overlayZoneAfterProps, class: overlayZoneAfterClass })] }))] }));
}

/**
 * The internal component used to render a single item in CardView.
 */
function BaseCardViewItem({ children, itemKey, itemData, itemIndex, isTabbable, isCurrentKey, isFocused, isFocusRingVisible, selectedKeys, selectionMode, onSelectionChange, focusBehavior, setIsFocusBehaviorValid, initialAnimation, isGridLayout, updateCardSize, reorderItemProps, onAnimationBusyStart, onAnimationBusyEnd }) {
    const rootRef = useRef(null);
    const timeoutRef = useRef();
    const isItemFocused = isFocused && isFocusRingVisible;
    const variantClasses = multiVariantStyles({
        itemFocused: focusBehavior === 'card' && isItemFocused ? 'isItemFocused' : 'isNotItemFocused',
        itemInitialOpacity: initialAnimation ? 'isTransparent' : 'isNotTransparent',
        itemInGridLayout: isGridLayout ? 'isInGridLayout' : 'isNotInGridLayout'
    });
    const classes = classNames([styles.itemStyle, variantClasses]);
    /*********************************** Tabbable Mode ***********************************/
    // don't bubble Arrow Up/Down/Left/Right events to parent when in isTabbable mode
    // this bubbling causes the outer CollectionFocus ring to change index while in the isTabbable mode.
    const handleKeyDown = (event) => {
        if (focusBehavior === 'card' &&
            isTabbable &&
            (event.key === 'ArrowDown' ||
                event.key === 'ArrowUp' ||
                event.key === 'ArrowLeft' ||
                event.key === 'ArrowRight')) {
            event.stopPropagation();
        }
    };
    /************************************ Card Size ************************************/
    const handleResize = useCallback((entry) => {
        updateCardSize?.(entry.contentRect.width, entry.contentRect.height);
    }, [updateCardSize]);
    const nullRef = useRef(null);
    useResizeObserver(updateCardSize ? rootRef : nullRef, handleResize);
    /********************************* Focus Behavior *********************************/
    useLayoutEffect(() => {
        if (focusBehavior === 'content' && isItemFocused) {
            // rootRef is cardview item, its first child is the focus trap div, so its first grand child
            // is the actual content
            const content = rootRef.current?.firstElementChild?.firstElementChild;
            const focusable = allTabbableElements(content, true, true);
            if (focusable.length === 0 || focusable.length > 1) {
                warn(`${focusable.length} focusable elements are detected in the card. 'content' focusBehavior is intended only for use when the card has single focusable element. Please specify one focusable element in the card or switch the focusBehavior to 'card'.`);
                setIsFocusBehaviorValid?.(false);
            }
            else {
                focusable[0].focus();
            }
        }
    }, [focusBehavior, isItemFocused, setIsFocusBehaviorValid]);
    /******************************** Initial Animation ********************************/
    // animation for slide up/down
    const { nodeRef: nodeRefSlide, controller: controllerSlide } = useAnimation(initialAnimation || 'none', animationConfigSlide(itemIndex));
    // animation for opacity change
    const { nodeRef: nodeRefOpacity, controller: controllerOpacity } = useAnimation((initialAnimation && 'opacity') || 'none', animationConfigOpacity(itemIndex, timeoutRef, onAnimationBusyEnd));
    if (initialAnimation) {
        onAnimationBusyStart?.();
    }
    // max cap for animation is 1s
    useEffect(() => {
        if (initialAnimation !== undefined) {
            timeoutRef.current = setTimeout(() => {
                controllerSlide.cancel();
                controllerOpacity.cancel();
                if (rootRef.current) {
                    rootRef.current.style.opacity = '1';
                }
                onAnimationBusyEnd?.();
            }, 1000);
        }
    }, [controllerOpacity, controllerSlide, initialAnimation, onAnimationBusyEnd]);
    // TODO: issue track in JET-57681, we currently have two useAnimation() because the opacity and
    // slide up/down have different durations, and the current API couldn't handle this case, we will
    // update this once the API is updated
    /********************************** Child Component **********************************/
    const itemRefs = useMemo(() => {
        // NOTE: spot an issue when we click the cards during animation is processing, the cards would not finish
        // the current animations and stay as what it is, e.g. leave itself as half transparent.
        // need to remove nodeRefSlide and nodeRefOpacity to avoid this issue.
        if (initialAnimation === undefined) {
            return mergeRefs(rootRef);
        }
        return mergeRefs(rootRef, nodeRefSlide, nodeRefOpacity);
    }, [rootRef, nodeRefSlide, nodeRefOpacity, initialAnimation]);
    const rendererContext = getItemContext({ data: { data: itemData, metadata: { key: itemKey } }, index: itemIndex }, selectedKeys, selectionMode === 'multiple', onSelectionChange);
    const childrenComponent = reorderItemProps ? (jsx(BaseCardViewReorderableItem, { ...reorderItemProps, itemKey: itemKey, itemIndex: itemIndex, isCurrentKey: isCurrentKey, children: children(rendererContext) })) : (children(rendererContext));
    return (jsx("div", { id: useId(), ref: itemRefs, class: classes, role: "gridcell", "data-oj-card-item": "", "data-oj-key": itemKey, onKeyDown: handleKeyDown, "aria-selected": selectionMode === 'none' ? undefined : containsKey(selectedKeys, itemKey), ...(typeof itemKey === 'number' && { 'data-oj-key-type': 'number' }), children: jsx(TabbableModeContext.Provider, { value: { isTabbable }, children: jsx(FocusTrap, { isDisabled: !isTabbable, restoreFocusRef: false, children: childrenComponent }) }) }));
}
const animationConfigSlide = (index) => {
    return {
        animationStates: {
            slideUp: {
                from: {
                    translateY: '50px'
                },
                to: {
                    translateY: '0px'
                },
                options: {
                    duration: 300,
                    delay: index * 50,
                    easing: [0, 0, 0.2, 1]
                }
            },
            slideDown: {
                from: {
                    translateY: '-50px'
                },
                to: {
                    translateY: '0'
                },
                options: {
                    duration: 300,
                    delay: index * 50,
                    easing: [0, 0, 0.2, 1]
                }
            }
        },
        isAnimatedOnMount: true
    };
};
const animationConfigOpacity = (index, timeoutRef, onAnimationBusyEnd) => {
    return {
        animationStates: {
            opacity: {
                from: {
                    opacity: 0
                },
                to: {
                    opacity: 1
                },
                options: {
                    duration: 150,
                    delay: index * 50,
                    easing: 'linear'
                }
            }
        },
        isAnimatedOnMount: true,
        onAnimationEnd: () => {
            onAnimationBusyEnd?.();
            if (timeoutRef.current) {
                clearTimeout(timeoutRef.current);
            }
        }
    };
};
/**
 * Compare the equality between two reorderItemProps, especially useful for memoized items
 */
const _compareReorderItemProps = (props1, props2) => {
    return (props1?.['dragKey'] === props2?.['dragKey'] &&
        props1?.['setDragKey'] === props2?.['setDragKey'] &&
        props1?.['columns'] === props2?.['columns'] &&
        props1?.['gutterSize'] === props2?.['gutterSize'] &&
        props1?.['rootRef'] === props2?.['rootRef']);
};
const MemoizeBaseCardViewItem = memo(BaseCardViewItem, (prev, next) => {
    if (prev && next) {
        return (prev.children === next.children &&
            prev.itemKey === next.itemKey &&
            prev.itemData === next.itemData &&
            prev.itemIndex === next.itemIndex &&
            prev.isTabbable === next.isTabbable &&
            prev.isCurrentKey === next.isCurrentKey &&
            prev.isFocused === next.isFocused &&
            prev.isFocusRingVisible === next.isFocusRingVisible &&
            prev.initialAnimation === next.initialAnimation &&
            prev.focusBehavior === next.focusBehavior &&
            isSameKey(prev.selectedKeys, next.selectedKeys) &&
            prev.selectionMode === next.selectionMode &&
            prev.onSelectionChange === next.onSelectionChange &&
            prev.setIsFocusBehaviorValid === next.setIsFocusBehaviorValid &&
            prev.isGridLayout === next.isGridLayout &&
            prev.updateCardSize === next.updateCardSize &&
            prev.onAnimationBusyStart === next.onAnimationBusyStart &&
            prev.onAnimationBusyEnd === next.onAnimationBusyEnd &&
            _compareReorderItemProps(prev.reorderItemProps, next.reorderItemProps));
    }
    return false;
});

function BaseCardViewLayout({ children, layout, gutterSize, columns }) {
    const gap = cardViewVars.gutterSize[gutterSize];
    if (layout === 'grid') {
        return columns < 1 ? null : (jsx(Grid, { gridTemplateColumns: `repeat(${columns}, 1fr)`, gap: gap, children: children }));
    }
    else {
        return (jsx(Flex, { wrap: "wrap", gap: gap, children: children }));
    }
}

/**
 * Allows to specify the time delay for rendering the component
 **/
const timerValue = 50;
/**
 * BaseCardViewSkeletonContainer renders skeletons after 50ms threshold defined in the Redwood spec
 **/
function BaseCardViewSkeletonContainer({ children }) {
    const [isVisible, setIsVisible] = useState(false);
    useEffect(() => {
        setTimeout(() => {
            setIsVisible(true);
        }, timerValue);
    }, []);
    return isVisible ? jsx(Fragment$1, { children: children }) : jsx("div", { class: LOADMORE_STYLE_CLASS });
}

const DEFAULT_SKELETON_NUMBER = 25;
const DEFAULT_SKELETON_WIDTH = '80x';
const DEFAULT_SKELETON_HEIGHT = '85x';
/**
 * A function that returns an array of skeletons based on count
 * @param count the number of skeletons
 */
const getSkeletonsByCount = (layout, count = DEFAULT_SKELETON_NUMBER, width, height, isUnique = true) => {
    const sw = width || (layout === 'flex' ? DEFAULT_SKELETON_WIDTH : '100%');
    const sh = height || DEFAULT_SKELETON_HEIGHT;
    return [...Array(count)].map((_v, i) => (
    // create unique keys if needed, otherwise the animations won't start at the same time, because the old copy of skeletons
    // could be reused, even the key is specified by useId(), combine count with time to make the key unique.
    jsx(Skeleton, { width: sw, height: sh, borderRadius: "6px" }, isUnique && `${i}${new Date().getTime()}`)));
};
/**
 * When there is no data (i.e. data is an empty array or null, and hasMore is false), we will show 25 card skeletons with default size
 */
const InitialLoadSkeletons = (props) => (jsx(BaseCardViewSkeletonContainer, { children: getSkeletonsByCount(props.layout) }));
/**
 * When we are fetching the initial data (i.e. data is an empty array, and hasMore is true), we will show 25 card skeletons with default size,
 * because we don't know the card dimension at this time. Since there is more data to load, we will specify LOADMORE_STYLE_CLASS on skeletons
 */
const DefaultLoadMoreSkeletons = (props) => {
    const firstSkeleton = (jsx("div", { class: LOADMORE_STYLE_CLASS, children: getSkeletonsByCount(props.layout, 1) }));
    const otherSkeletons = getSkeletonsByCount(props.layout, DEFAULT_SKELETON_NUMBER - 1);
    const skeletons = [firstSkeleton, ...otherSkeletons];
    return jsx(BaseCardViewSkeletonContainer, { children: skeletons });
};
/**
 * During the render that we measure and update card dimension, we will hide skeletons temporarily
 * to avoid showing incorrect skeletons
 */
const InvisibleSkeletons = () => {
    return jsx("div", { class: LOADMORE_STYLE_CLASS });
};
const LoadMoreSkeletons = (props) => {
    if (props.colCount < 1 || props.cardWidth == null || props.cardHeight == null) {
        // if column count / card width / card height are invalid, we will show the invisible skeletons
        return jsx(InvisibleSkeletons, {});
    }
    else {
        // after initial renders, show skeletons with correct dimension and count
        const skeletons = [];
        const firstRowColCount = props.colCount - (props.totalCount % props.colCount);
        const isOnlyOneRow = firstRowColCount === props.colCount;
        const cardWidth = `calc(${props.cardWidth}px)`;
        const cardHeight = props.cardHeight
            ? `calc(${props.cardHeight}px)`
            : DEFAULT_SKELETON_HEIGHT;
        const width = props.layout === 'flex' ? cardWidth : '100%';
        const height = props.layout === 'flex' || isOnlyOneRow ? cardHeight : '100%';
        // first row
        const firstSkeleton = (jsx("div", { class: LOADMORE_STYLE_CLASS, children: getSkeletonsByCount(props.layout, 1, width, height) }));
        skeletons.push(firstSkeleton);
        skeletons.push(getSkeletonsByCount(props.layout, firstRowColCount - 1, width, height));
        // second row
        if (!isOnlyOneRow) {
            skeletons.push(getSkeletonsByCount(props.layout, props.colCount, width, cardHeight));
        }
        return jsx(BaseCardViewSkeletonContainer, { children: skeletons });
    }
};
const CustomInitialSkeletions = (props) => {
    const context = { loadingStatus: 'initial' };
    return (jsx(BaseCardViewSkeletonContainer, { children: renderMultipleSkeletons(context, 25, props.skeletonRenderer) }));
};
const CustomLoadMoreSkeletons = (props) => {
    const skeletons = [];
    if (props.isRenderAsInitial) {
        const context = { loadingStatus: 'initial' };
        skeletons.push(jsx("div", { class: LOADMORE_STYLE_CLASS, children: props.skeletonRenderer(context) }));
        skeletons.push(renderMultipleSkeletons(context, 24, props.skeletonRenderer));
    }
    else {
        if (props.colCount === 0) {
            return null;
        }
        const cardWidth = `calc(${props.cardWidth}px)`;
        const cardHeight = `calc(${props.cardHeight}px)`;
        const firstRowColCount = props.colCount - (props.totalCount % props.colCount);
        const isOnlyOneRow = firstRowColCount === props.colCount;
        const context = {
            loadingStatus: 'loadMore',
            height: props.layout === 'flex' || isOnlyOneRow ? cardHeight : '100%',
            width: props.layout === 'flex' ? cardWidth : '100%'
        };
        skeletons.push(jsx("div", { class: LOADMORE_STYLE_CLASS, children: props.skeletonRenderer(context) }));
        skeletons.push(Array(renderMultipleSkeletons(context, firstRowColCount - 1, props.skeletonRenderer)));
        if (!isOnlyOneRow) {
            skeletons.push(renderMultipleSkeletons(context, props.colCount, props.skeletonRenderer));
        }
    }
    return jsx(BaseCardViewSkeletonContainer, { children: skeletons });
};
const renderMultipleSkeletons = (context, count, skeletonRenderer) => {
    return Array(count).fill(skeletonRenderer(context));
};

const useCardViewReorder = ({ onReorder, gutterSize, columns, rootRef, scrollCurrItemIntoView }) => {
    const isReorderEnabled = onReorder != undefined;
    const [dragKey, setDragKey] = useState(undefined);
    const { reorderProps, reorderContext, reorderInstructionsId, reorderInstructions } = useReorderable({
        onReorder,
        rootRef,
        itemSelector: CARDVIEW_ITEM_SELECTOR,
        isDisabled: !isReorderEnabled,
        columns
    });
    if (!isReorderEnabled) {
        return {
            reorderProps,
            reorderContext,
            reorderInstructionsId,
            reorderInstructions
        };
    }
    const handleDragOver = (event) => {
        dragToScroll(event, dragKey !== undefined, rootRef.current);
        // during mouse reordering, the drag item might not be the same as current item,
        // so we should disallow current item scroll into view
        scrollCurrItemIntoView.current = false;
    };
    const reorderEventProps = { ...reorderProps, onDragOver: handleDragOver };
    const reorderItemProps = {
        dragKey,
        setDragKey,
        onReorder,
        rootRef,
        gutterSize,
        columns
    };
    return {
        reorderProps: reorderEventProps,
        reorderItemProps,
        reorderContext,
        reorderInstructionsId,
        reorderInstructions
    };
};

const BaseCardViewImpl = forwardRef(({ children, data, onLoadMore = () => { }, hasMore = false, getRowKey, currentItemOverride, onPersistCurrentItem, selectionMode = 'none', selectedKeys = emptyKeys, onSelectionChange, onReorder, viewportConfig, focusBehavior = 'card', 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, initialAnimation = 'slideUp', gutterSize = 'sm', columns, layout, cardSize, updateCardSize, skeletonRenderer }, ref) => {
    const rootRef = useRef(null);
    useImperativeHandle(ref, () => rootRef.current, [rootRef]);
    // tracking the anchor key which is used for shift+click selection
    const anchorKey = useRef();
    const gutterSizePX = gutterSizeToPX[gutterSize];
    const layoutProps = { layout, columns, gutterSize };
    //setup busy start and busy end callbacks for the initial animation
    const { onBusyStart: onAnimationBusyStart, onBusyEnd: onAnimationBusyEnd } = useAddBusyState('handling initial animation');
    const isInitialAnimation = useRef(true);
    useEffect(() => {
        if (cardSize.height != undefined && cardSize.width != undefined) {
            isInitialAnimation.current = false;
        }
    }, [cardSize]);
    const ariaMultiSelectable = selectionMode === 'none' ? undefined : selectionMode === 'multiple';
    const ariaRowcount = 1;
    const ariaColcount = data ? data.length : -1;
    const [isFocusBehaviorValid, setIsFocusBehaviorValid] = useState(true);
    const validFocusBehavior = isFocusBehaviorValid ? focusBehavior : 'card';
    // ensure current item is scrolled into view when current item is updated
    // during reordering, enable that during keyboard reordering, disable that during mouse reordering
    const scrollCurrItemIntoView = useRef(false);
    const isReorderEnabled = onReorder != undefined;
    const isFetching = useRef(false);
    /*****************************************************************************************/
    /*********************** Current Key, Focus Ring and Tabbable Mode ***********************/
    /*****************************************************************************************/
    const arrowKeyAndHandlers = {
        ArrowUp: (key) => getPrevNextKeyByCount(data, getRowKey, key, -columns),
        ArrowDown: (key) => getPrevNextKeyByCount(data, getRowKey, key, columns),
        ArrowLeft: (key) => getPrevNextKeyByCount(data, getRowKey, key, -1),
        ArrowRight: (key) => getPrevNextKeyByCount(data, getRowKey, key, 1)
    };
    const { navigationProps, currentKey, isCurrentItemOverridden, showFocusRing, isCurrentTabbableKey } = useNavigation(data, rootRef, idExtractor, CARDVIEW_ITEM_SELECTOR, arrowKeyAndHandlers, currentItemOverride, onPersistCurrentItem, viewportConfig, undefined, selectionMode !== 'multiple', validFocusBehavior === 'card', validFocusBehavior === 'card', false);
    /*****************************************************************************************/
    /*************************************** Selection ***************************************/
    /*****************************************************************************************/
    const handleSelectionChange = useCallback((detail) => {
        if (onSelectionChange) {
            if (detail.value.all === false && detail.value.keys.size > 0) {
                // during regular selection, the last key selected by a user gesture is the anchor key
                anchorKey.current = Array.from(detail.value.keys.values()).pop();
            }
            onSelectionChange(detail);
        }
    }, [anchorKey, onSelectionChange]);
    const handleSelectionRangeChange = useCallback((detail) => {
        if (data && onSelectionChange) {
            const value = handleSelectionRange(detail, data, getRowKey);
            // for non-virtualized CardView, value should always be an array
            if (Array.isArray(value)) {
                if (detail.eventType === 'shiftSpace') {
                    // during range selection by shift+space, the first key selected by a user gesture is the anchor key
                    anchorKey.current = detail.value.start;
                }
                else {
                    // during range selection by shift+click/arrow keys, the last key selected by a user gesture is the anchor key
                    anchorKey.current = detail.value.end;
                }
                onSelectionChange({ value: { all: false, keys: new Set(value) }, target: null });
            }
        }
    }, [data, onSelectionChange, getRowKey]);
    const { selectionProps } = useSelection((element) => {
        if (element === rootRef.current) {
            return currentKey === undefined ? null : currentKey;
        }
        return keyExtractor(element, CARDVIEW_ITEM_SELECTOR);
    }, selectedKeys, selectionMode, false, 'toggle', handleSelectionChange, anchorKey.current, currentKey, (currentKey, isPrev) => getPrevNextKeyByCount(data, getRowKey, currentKey, isPrev ? -columns : columns), (currentKey, isPrev) => getPrevNextKeyByCount(data, getRowKey, currentKey, isPrev ? -1 : 1), (key) => {
        if (rootRef.current) {
            const elem = findElementByKey(rootRef.current, key, CARDVIEW_ITEM_SELECTOR);
            if (elem) {
                elem.scrollIntoView({ block: 'nearest' });
            }
        }
    }, handleSelectionRangeChange);
    /*****************************************************************************************/
    /**************************************** Reorder ****************************************/
    /*****************************************************************************************/
    const { reorderProps, reorderItemProps, reorderContext, reorderInstructionsId, reorderInstructions } = useCardViewReorder({
        onReorder,
        gutterSize,
        columns,
        rootRef,
        scrollCurrItemIntoView
    });
    const reorderAcc = isReorderEnabled && (jsx(Fragment, { children: jsx("span", { id: reorderInstructionsId, children: jsx(HiddenAccessible, { children: reorderInstructions }) }) }));
    const handleKeyDown = (event) => {
        // for content focusBehavior, when users tab out, we need to focus back to card grid, to ensure
        // the tabbing order works properly, otherwise the focus might get stuck at the current item
        if (validFocusBehavior === 'content' && event.key === 'Tab') {
            rootRef.current?.focus({ preventScroll: true });
        }
        // during keyboard reordering, the current item should be scrolled into view
        const keyHandlers = ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'];
        if (isReorderEnabled &&
            (event.metaKey || event.ctrlKey) &&
            event.shiftKey &&
            keyHandlers.includes(event.key)) {
            scrollCurrItemIntoView.current = true;
        }
    };
    useEffect(() => {
        if (isReorderEnabled && currentKey != null && rootRef.current) {
            const elem = findElementByKey(rootRef.current, currentKey, CARDVIEW_ITEM_SELECTOR);
            // make sure current item is visible during reordering
            // scroll the current item into view only when the current item is updated internally
            if (elem && scrollCurrItemIntoView.current && !isCurrentItemOverridden) {
                elem.scrollIntoView({ block: 'nearest' });
                scrollCurrItemIntoView.current = false;
            }
        }
        // since the currentKey will not change during keyboard reordering,
        // listen to data to ensure cardview scroll into view
    }, [currentKey, data, isReorderEnabled, isCurrentItemOverridden]);
    /*****************************************************************************************/
    /************************************* Load More *****************************************/
    /*****************************************************************************************/
    const handleLoadMore = useCallback(() => {
        // check isFetching to avoid multiple load more at the same time (details in JET-61117)
        if (!isFetching.current) {
            isFetching.current = true;
            onLoadMore();
        }
    }, [isFetching, onLoadMore]);
    useEffect(() => {
        isFetching.current = false;
        // listen to data and hasMore because when one of them gets updated, the fetching is done
    }, [data, hasMore]);
    /*****************************************************************************************/
    /************************************ Child Component ************************************/
    /*****************************************************************************************/
    const childrenComponent = (context) => {
        const itemKey = getRowKey(context.data);
        const isTabbable = isCurrentTabbableKey(itemKey);
        const isFocused = currentKey === itemKey && !isTabbable;
        const isFocusRingVisible = isFocused && showFocusRing;
        const isLastCard = data ? context.index === data.length - 1 : false;
        return (jsx(MemoizeBaseCardViewItem, { itemKey: itemKey, isCurrentKey: currentKey === itemKey, itemData: context.data, itemIndex: context.index, isTabbable: isTabbable, isFocused: isFocused, isFocusRingVisible: isFocusRingVisible, isGridLayout: layout === 'grid', selectedKeys: selectedKeys, selectionMode: selectionMode, onSelectionChange: onSelectionChange, focusBehavior: validFocusBehavior, ...(context.index === 0 && isInitialAnimation.current && { onAnimationBusyStart }), ...(isLastCard && isInitialAnimation.current && { onAnimationBusyEnd }), ...(focusBehavior === 'content' && { setIsFocusBehaviorValid }), ...(isInitialAnimation.current && { initialAnimation }), ...(context.index === 0 && !cardSize.width && { updateCardSize }), ...(isReorderEnabled && { reorderItemProps }), children: children }, itemKey));
    };
    // todo: issue track in JET-57952, need a way to show the skeletons in keyboard navigation
    const skeletons = data ? (!data.length && hasMore ? (skeletonRenderer ? (jsx(CustomLoadMoreSkeletons, { layout: layout, colCount: columns, totalCount: data.length, cardWidth: cardSize.width, cardHeight: cardSize.height, isRenderAsInitial: true, skeletonRenderer: skeletonRenderer })) : (jsx(DefaultLoadMoreSkeletons, { layout: layout }))) : skeletonRenderer ? (jsx(CustomLoadMoreSkeletons, { layout: layout, colCount: columns, totalCount: data.length, cardWidth: cardSize.width, cardHeight: cardSize.height, isRenderAsInitial: false, skeletonRenderer: skeletonRenderer })) : (jsx(LoadMoreSkeletons, { layout: layout, colCount: columns, totalCount: data.length, cardWidth: cardSize.width, cardHeight: cardSize.height }))) : null;
    viewportConfig = getViewportConfig(rootRef, viewportConfig);
    const collectionComponent = (jsx("div", { role: "row", children: jsx(BaseCardViewLayout, { ...layoutProps, children: jsx(LoadMoreCollection, { data: data, hasMore: hasMore, onLoadMore: handleLoadMore, loadMoreIndicator: skeletons, loadMoreThreshold: gutterSizePX + 4, viewportConfig: viewportConfig, children: childrenComponent }) }) }));
    const initialLoadSkeletons = (jsx(BaseCardViewLayout, { ...layoutProps, children: skeletonRenderer ? (jsx(CustomInitialSkeletions, { skeletonRenderer: skeletonRenderer })) : (jsx(InitialLoadSkeletons, { layout: layout })) }));
    const allowBrowserContextMenu = !(getClientHints().platform === 'ios' || getClientHints().platform === 'android');
    const classArray = [styles.baseStyle];
    if (!allowBrowserContextMenu) {
        classArray.push(styles.userSelect);
    }
    const classes = classNames(classArray);
    return (jsxs("div", { ...mergeProps(navigationProps, selectionProps, reorderProps, {
            onKeyDown: handleKeyDown
        }), ref: rootRef, role: "grid", class: classes, tabIndex: 0, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": isReorderEnabled ? reorderInstructionsId + ' ' + ariaDescribedBy : ariaDescribedBy, "aria-multiselectable": ariaMultiSelectable, "aria-rowcount": ariaRowcount, "aria-colcount": ariaColcount, children: [jsx(ReorderableContext.Provider, { value: reorderContext, children: !data ? (initialLoadSkeletons) : data.length || hasMore ? (collectionComponent) : (jsx(Fragment$1, {})) }), reorderAcc] }));
});
const idExtractor = (elem) => elem.id;
/**
 * Cast BaseCardViewImpl to generic type, because BaseCardViewImpl would infer the generic type to specific type,
 * i.e infer <K extends string | number, D> to <string | number, unknown>, which will cause type issues in the parent component
 */
const BaseCardView = BaseCardViewImpl;

export { BaseCardView as B, getColCount as a, gutterSizeToPX as g };
//# sourceMappingURL=BaseCardView-b3d8bae0.js.map
