import { jsx } from 'react/jsx-runtime';
import React__default, { useContext, createContext, useMemo } from 'react';
import { cn } from '../../../utils/cn.js';
import { timelineTheme } from './Timeline.theme.js';
import { TimelineItem } from './TimelineItem.js';
import { TimelineMarker } from './TimelineMarker.js';
import { TimelineContent } from './TimelineContent.js';

// Create context for the main Timeline component
const TimelineContext = createContext(undefined);
const useTimelineContext = () => {
    const ctx = useContext(TimelineContext);
    if (!ctx)
        throw new Error("Timeline compound components must be used within <Timeline>");
    return ctx;
};
const TimelineRoot = React__default.forwardRef(({ children, className, layout = "vertical", align = "left", theme: themeOverrides, ...props }, ref) => {
    // Merge theme with any overrides
    const theme = useMemo(() => ({
        ...timelineTheme,
        ...(themeOverrides || {}),
        markerStatuses: {
            ...timelineTheme.markerStatuses,
            ...((themeOverrides === null || themeOverrides === void 0 ? void 0 : themeOverrides.markerStatuses) || {}),
        },
    }), [themeOverrides]);
    // Count total children for index tracking
    const totalItems = React__default.Children.count(children);
    // Context value to be provided to children
    const contextValue = useMemo(() => ({
        layout,
        align,
        theme,
        totalItems,
    }), [layout, align, theme, totalItems]);
    const isHorizontal = layout === "horizontal";
    return (jsx(TimelineContext.Provider, { value: contextValue, children: jsx("div", { ref: ref, className: cn(isHorizontal ? theme.baseStyleHorizontal : theme.baseStyle, className), "data-layout": layout, "data-align": align, role: "list", "aria-label": "Timeline", ...props, children: children }) }));
});
TimelineRoot.displayName = "Timeline";
// Attach compound subcomponents
const Timeline = Object.assign(TimelineRoot, {
    Item: TimelineItem,
    Marker: TimelineMarker,
    Content: TimelineContent,
});

export { Timeline, useTimelineContext };
