"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    IterationNavigator: ()=>IterationNavigator
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const apollo_wind_namespaceObject = require("@uipath/apollo-wind");
const external_react_namespaceObject = require("react");
const index_cjs_namespaceObject = require("../../utils/index.cjs");
const icon_registry_cjs_namespaceObject = require("../../utils/icon-registry.cjs");
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: (0, index_cjs_namespaceObject.clamp)(rawActiveIndex, 0, total - 1)
    };
}
function stopCanvasControlEvent(event) {
    event.stopPropagation();
}
function IterationNavigator({ iterationState }) {
    const resolvedState = resolveState(iterationState);
    if (!resolvedState) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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 = (0, external_react_namespaceObject.useCallback)((event)=>{
        event.stopPropagation();
        if (!canGoPrevious) return;
        onActiveIndexChange?.(activeIndex - 1);
    }, [
        activeIndex,
        canGoPrevious,
        onActiveIndexChange
    ]);
    const handleNext = (0, external_react_namespaceObject.useCallback)((event)=>{
        event.stopPropagation();
        if (!canGoNext) return;
        onActiveIndexChange?.(activeIndex + 1);
    }, [
        activeIndex,
        canGoNext,
        onActiveIndexChange
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("fieldset", {
        className: (0, apollo_wind_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsxs)("legend", {
                className: "sr-only",
                children: [
                    label,
                    ": ",
                    visibleIndex,
                    " of ",
                    total
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                type: "button",
                className: (0, apollo_wind_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                    icon: "chevron-left",
                    size: 12
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
                type: "button",
                className: (0, apollo_wind_namespaceObject.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__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                    icon: "chevron-right",
                    size: 12
                })
            })
        ]
    });
}
exports.IterationNavigator = __webpack_exports__.IterationNavigator;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "IterationNavigator"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
