import { jsx, jsxs } from "react/jsx-runtime";
import { cn } from "@uipath/apollo-wind";
import { useCallback } from "react";
import { clamp } from "../../utils/index.js";
import { CanvasIcon } from "../../utils/icon-registry.js";
function resolveState(iterationState) {
    if (!Number.isFinite(iterationState.total)) return;
    const total = Math.trunc(iterationState.total);
    if (total <= 0) return;
    const rawActiveIndex = Number.isFinite(iterationState.activeIndex) ? Math.trunc(iterationState.activeIndex) : 0;
    return {
        ...iterationState,
        total,
        activeIndex: clamp(rawActiveIndex, 0, total - 1)
    };
}
function stopCanvasControlEvent(event) {
    event.stopPropagation();
}
function IterationNavigator({ iterationState }) {
    const resolvedState = resolveState(iterationState);
    if (!resolvedState) return null;
    return /*#__PURE__*/ jsx(NavigatorContent, {
        iterationState: resolvedState
    });
}
function NavigatorContent({ iterationState }) {
    const { activeIndex, total, onActiveIndexChange, disabled, ariaLabel } = iterationState;
    const canInteract = !disabled && 'function' == typeof onActiveIndexChange;
    const canGoPrevious = canInteract && activeIndex > 0;
    const canGoNext = canInteract && activeIndex < total - 1;
    const label = ariaLabel ?? 'Loop iteration';
    const visibleIndex = activeIndex + 1;
    const handlePrevious = useCallback((event)=>{
        event.stopPropagation();
        if (!canGoPrevious) return;
        onActiveIndexChange?.(activeIndex - 1);
    }, [
        activeIndex,
        canGoPrevious,
        onActiveIndexChange
    ]);
    const handleNext = useCallback((event)=>{
        event.stopPropagation();
        if (!canGoNext) return;
        onActiveIndexChange?.(activeIndex + 1);
    }, [
        activeIndex,
        canGoNext,
        onActiveIndexChange
    ]);
    return /*#__PURE__*/ jsxs("fieldset", {
        className: cn('nodrag nopan pointer-events-auto m-0 flex h-6 min-w-0 shrink-0 items-center gap-1 rounded-full px-1 py-0', 'border border-border bg-surface text-foreground shadow-sm'),
        "data-testid": "loop-iteration-navigator",
        onPointerDown: stopCanvasControlEvent,
        onMouseDown: stopCanvasControlEvent,
        onDoubleClick: stopCanvasControlEvent,
        children: [
            /*#__PURE__*/ jsxs("legend", {
                className: "sr-only",
                children: [
                    label,
                    ": ",
                    visibleIndex,
                    " of ",
                    total
                ]
            }),
            /*#__PURE__*/ jsx("button", {
                type: "button",
                className: cn('nodrag nopan inline-flex h-4 w-4 items-center justify-center rounded-full', 'text-foreground transition-opacity', canGoPrevious ? 'cursor-pointer opacity-100' : 'cursor-not-allowed opacity-40'),
                disabled: !canGoPrevious,
                "aria-label": "Previous loop iteration",
                onClick: handlePrevious,
                onPointerDown: stopCanvasControlEvent,
                onMouseDown: stopCanvasControlEvent,
                "data-testid": "loop-iteration-previous",
                children: /*#__PURE__*/ jsx(CanvasIcon, {
                    icon: "chevron-left",
                    size: 12
                })
            }),
            /*#__PURE__*/ jsxs("span", {
                className: "min-w-8 select-none px-1 text-center text-[11px] font-semibold leading-4",
                "data-testid": "loop-iteration-label",
                children: [
                    visibleIndex,
                    " / ",
                    total
                ]
            }),
            /*#__PURE__*/ jsx("button", {
                type: "button",
                className: cn('nodrag nopan inline-flex h-4 w-4 items-center justify-center rounded-full', 'text-foreground transition-opacity', canGoNext ? 'cursor-pointer opacity-100' : 'cursor-not-allowed opacity-40'),
                disabled: !canGoNext,
                "aria-label": "Next loop iteration",
                onClick: handleNext,
                onPointerDown: stopCanvasControlEvent,
                onMouseDown: stopCanvasControlEvent,
                "data-testid": "loop-iteration-next",
                children: /*#__PURE__*/ jsx(CanvasIcon, {
                    icon: "chevron-right",
                    size: 12
                })
            })
        ]
    });
}
export { IterationNavigator };
