import { n as AnalyticsId } from "../../constants2.js";
import { n as getAriaLabelAttributes } from "../../accessibility.js";
import { t as useSize } from "../../useSize.js";
import classNames from "classnames";
import { useRef } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import styles from "./styles.module.scss";
var MARKER_DOT_MIN_DISTANCE_PX = 4;
var MARKER_WIDTH_PX = 24;
const STEPPER_PADDING_X_PX = 8;
const getValidatedMin = (min, max) => min < max ? min : max - 1;
const getValidatedMax = (min, max) => max < min ? min : max;
const getValidatedValue = (value, min, max) => {
	if (typeof value === "undefined" || value < min) return min;
	return value > max ? max : value;
};
const getMaximumDots = (stepperWidth) => Math.floor(stepperWidth / (MARKER_WIDTH_PX + MARKER_DOT_MIN_DISTANCE_PX));
const getDistanceBetweenDots = (stepperWidth, dots) => (stepperWidth - MARKER_WIDTH_PX) / (dots - 1);
const getMarkerPosition = (distanceBetweenDots, index) => distanceBetweenDots * index;
const getAllowedValues = (min, max) => Array.from(Array(max - min + 1), (_, index) => index + min);
const getValidatedProps = (value, min, max) => {
	const validatedMin = getValidatedMin(min, max);
	const validatedMax = getValidatedMax(validatedMin, max);
	return {
		validatedValue: getValidatedValue(value, validatedMin, validatedMax),
		validatedMin,
		validatedMax
	};
};
var Dot = ({ index, completed, distanceBetweenDots, position }) => {
	return /* @__PURE__ */ jsx("span", {
		className: classNames(styles.stepper__dot, completed && styles["stepper__dot--completed"], position && styles[`stepper__dot--${position}`]),
		style: !position && typeof index !== "undefined" && typeof distanceBetweenDots !== "undefined" ? { left: `${index * distanceBetweenDots + 8}px` } : void 0,
		"data-testid": "dot"
	});
};
var Dot_default = Dot;
var Dots = ({ allowedValues, currentValue, distanceBetweenDots }) => /* @__PURE__ */ jsx(Fragment, { children: allowedValues.map((value, index) => /* @__PURE__ */ jsx(Dot_default, {
	index,
	completed: value < currentValue,
	distanceBetweenDots
}, value)) });
var DotList_default = Dots;
var Stepper = ({ ariaLabelledById, ariaLabel, value, min = 0, max = 2, testId }) => {
	const wrapperRef = useRef(null);
	const ref = useRef(null);
	const { width: wrapperWidth = 0 } = useSize(wrapperRef) || {};
	const { width: stepperWidth = 0 } = useSize(ref) || {};
	const { validatedValue, validatedMin, validatedMax } = getValidatedProps(value, min, max);
	const allowedValues = getAllowedValues(validatedMin, validatedMax);
	const index = allowedValues.indexOf(validatedValue);
	const maximumDots = getMaximumDots(wrapperWidth);
	const distanceBetweenDots = getDistanceBetweenDots(stepperWidth, allowedValues.length);
	const markerPosition = getMarkerPosition(distanceBetweenDots, index);
	const showAllDots = allowedValues.length <= maximumDots;
	const isCompleted = validatedValue === validatedMax;
	const ariaLabelAttributes = getAriaLabelAttributes({
		label: ariaLabel,
		id: ariaLabelledById
	});
	return /* @__PURE__ */ jsxs("div", {
		className: styles["stepper-wrapper"],
		ref: wrapperRef,
		children: [/* @__PURE__ */ jsxs("span", {
			role: "progressbar",
			...ariaLabelAttributes,
			"aria-valuenow": validatedValue,
			"aria-valuemin": validatedMin,
			"aria-valuemax": validatedMax,
			className: styles.stepper,
			style: { backgroundPositionX: `${isCompleted ? stepperWidth : markerPosition}px` },
			ref,
			"data-testid": testId,
			"data-analyticsid": AnalyticsId.Stepper,
			children: [
				showAllDots && /* @__PURE__ */ jsx(DotList_default, {
					allowedValues,
					currentValue: validatedValue,
					distanceBetweenDots
				}),
				!showAllDots && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Dot_default, {
					completed: validatedValue > validatedMin,
					position: "left"
				}), /* @__PURE__ */ jsx(Dot_default, {
					completed: validatedValue === validatedMax,
					position: "right"
				})] }),
				/* @__PURE__ */ jsx("span", {
					className: styles.stepper__marker,
					style: { left: `${markerPosition}px` }
				})
			]
		}), !showAllDots && /* @__PURE__ */ jsx("span", {
			className: styles.stepper__number,
			children: `${validatedValue}/${validatedMax}`
		})]
	});
};
var Stepper_default = Stepper;
export { STEPPER_PADDING_X_PX, Stepper_default as default, getAllowedValues, getDistanceBetweenDots, getMarkerPosition, getMaximumDots, getValidatedMax, getValidatedMin, getValidatedProps, getValidatedValue };

//# sourceMappingURL=index.js.map