import React from 'react';
import { Box } from '@wix/design-system';
import { useCollectionPageContext } from '../../providers';
import { observer } from 'mobx-react-lite';
import { MoreActionsText } from '../../components/MoreActions/MoreActionsText';
import { useIsMobile } from '../../hooks/useIsMobile';
import { pushFactoryIfNotExistsOrAddToTopOfGroup } from './pushFactoryIfNotExistsOrAddToTopOfGroup';
import { mergeActionsForNarrowMode } from './transformSecondaryActionsToMoreActions';
import { MoreActions } from '../../components/MoreActions';
import { useWixPatternsContainer } from '@wix/bex-core/react';
function withFeaturedActions(items, featuredActions) {
    if (!featuredActions) {
        return items;
    }
    let result = items;
    for (const factory of featuredActions) {
        result = pushFactoryIfNotExistsOrAddToTopOfGroup(result, factory);
    }
    return result;
}
const WideModeMoreActions = observer(({ child }) => {
    const { primaryToolbar } = useCollectionPageContext();
    const featuredActions = primaryToolbar?.getPageFeaturedActions();
    const forceMoreActions = featuredActions?.length ? (React.createElement(MoreActions, null)) : undefined;
    const moreActions = child.props.moreActions ?? forceMoreActions;
    if (!moreActions) {
        return null;
    }
    const { type: MoreActionsType, props: moreActionsProps } = moreActions;
    const items = withFeaturedActions(moreActionsProps.items, featuredActions);
    if (child.props.secondaryActions) {
        return React.createElement(MoreActionsType, { ...moreActionsProps, items: items });
    }
    return React.createElement(MoreActionsText, { ...moreActionsProps, items: items });
});
const NarrowModeMoreActions = observer(({ child }) => {
    const { primaryToolbar } = useCollectionPageContext();
    const featuredActions = primaryToolbar?.getPageFeaturedActions();
    const moreActionsProps = child.props.moreActions?.props;
    const moreActionsItems = withFeaturedActions(moreActionsProps?.items, featuredActions);
    const items = mergeActionsForNarrowMode(child.props.secondaryActions, moreActionsItems);
    if (items.length === 0) {
        return null;
    }
    return (React.createElement(MoreActions, { items: items, containerId: moreActionsProps?.containerId, exploreAppsModalProps: moreActionsProps?.exploreAppsModalProps, biAdditionalInfo: moreActionsProps?.biAdditionalInfo }));
});
export const CollectionPageActionsBar = observer(({ child }) => {
    const isMobile = useIsMobile();
    const { isNarrow } = useCollectionPageContext();
    const { internalExperiments } = useWixPatternsContainer();
    const isNarrowMode = isNarrow &&
        internalExperiments?.enabled('specs.cairo.CollectionPageMinWidth');
    if (isMobile) {
        return React.createElement(Box, { gap: "SP2" }, child.props.primaryAction);
    }
    if (isNarrowMode) {
        return (React.createElement(Box, { gap: "SP2" },
            React.createElement(NarrowModeMoreActions, { child: child }),
            child.props.primaryAction));
    }
    return (React.createElement(Box, { gap: "SP2" },
        React.createElement(WideModeMoreActions, { child: child }),
        child.props.secondaryActions,
        child.props.primaryAction));
});
//# sourceMappingURL=CollectionPageActionsBar.js.map