import { WizardContext, useWizard } from "./WizardContext.js";
import React from "react";
import { Fragment as Fragment$1, jsx } from "@emotion/react/jsx-runtime";

//#region src/components/ConfigureSSO/elements/Wizard/Wizard.tsx
const Root = ({ children, initialStepId }) => {
	const parentWizard = React.useContext(WizardContext);
	return /* @__PURE__ */ jsx(RootInner, {
		parentWizard,
		isNested: parentWizard !== null,
		initialStepId,
		children
	});
};
const RootInner = ({ parentWizard, isNested, initialStepId, children }) => {
	const [activeSteps, setActiveSteps] = React.useState([]);
	const [currentStepId, setCurrentStepId] = React.useState(initialStepId);
	const registerStep = React.useCallback((step) => {
		setActiveSteps((prev) => {
			const existingIndex = prev.findIndex((s) => s.id === step.id);
			if (existingIndex >= 0) {
				const existing = prev[existingIndex];
				if (existing.label === step.label && existing.isCompleted === step.isCompleted) return prev;
				const next = prev.slice();
				next[existingIndex] = step;
				return next;
			}
			return [...prev, step];
		});
		setCurrentStepId((prev) => prev ?? step.id);
	}, []);
	const unregisterStep = React.useCallback((id) => {
		setActiveSteps((prev) => prev.some((s) => s.id === id) ? prev.filter((s) => s.id !== id) : prev);
		setCurrentStepId((prev) => prev === id ? void 0 : prev);
	}, []);
	const currentStep = React.useMemo(() => activeSteps.find((s) => s.id === currentStepId), [activeSteps, currentStepId]);
	const currentIndex = React.useMemo(() => currentStep ? activeSteps.findIndex((s) => s.id === currentStep.id) : -1, [activeSteps, currentStep]);
	const goNext = React.useCallback(() => {
		if (currentIndex < 0) return;
		const next = activeSteps[currentIndex + 1];
		if (next) {
			setCurrentStepId(next.id);
			return;
		}
		return parentWizard?.goNext();
	}, [
		activeSteps,
		currentIndex,
		parentWizard
	]);
	const goPrev = React.useCallback(() => {
		if (currentIndex < 0) return;
		const prev = activeSteps[currentIndex - 1];
		if (prev) {
			setCurrentStepId(prev.id);
			return;
		}
		return parentWizard?.goPrev();
	}, [
		activeSteps,
		currentIndex,
		parentWizard
	]);
	const goToStep = React.useCallback((id) => {
		if (activeSteps.some((s) => s.id === id)) setCurrentStepId(id);
	}, [activeSteps]);
	const value = React.useMemo(() => ({
		activeSteps,
		currentStep,
		currentIndex,
		totalSteps: activeSteps.length,
		isNested,
		isFirstStep: currentIndex <= 0 && (!parentWizard || parentWizard.isFirstStep),
		isLastStep: currentIndex === activeSteps.length - 1 && (!parentWizard || parentWizard.isLastStep),
		goNext,
		goPrev,
		goToStep,
		registerStep,
		unregisterStep
	}), [
		activeSteps,
		currentStep,
		currentIndex,
		isNested,
		parentWizard,
		goNext,
		goPrev,
		goToStep,
		registerStep,
		unregisterStep
	]);
	return /* @__PURE__ */ jsx(WizardContext.Provider, {
		value,
		children
	});
};
const Step = ({ id, label, isCompleted, children }) => {
	const { registerStep, unregisterStep, currentStep } = useWizard();
	React.useLayoutEffect(() => {
		registerStep({
			id,
			label,
			isCompleted
		});
	}, [
		id,
		label,
		isCompleted,
		registerStep
	]);
	React.useLayoutEffect(() => {
		return () => unregisterStep(id);
	}, [id, unregisterStep]);
	if (currentStep?.id !== id) return null;
	return /* @__PURE__ */ jsx(Fragment$1, { children });
};
Step.displayName = "Wizard.Step";
/**
* Declarative wizard primitive — UI-less, state-driven.
*
* Steps are written as JSX children: render a `<Wizard.Step>` for
* each step and toggle visibility with regular conditional
* expressions (`{cond && <Step>...</Step>}`).
*
* Steps register themselves with the parent wizard on mount via
* effect, so the wizard's `activeSteps` list always reflects exactly
* what's currently rendered. Inner sub-steps are declared by nesting
* another `<Wizard>` inside a step's body.
*
* The Wizard root renders `{children}` directly — no chrome, no
* routing, no layout wrapper. Header, Footer, and any step indicator
* are provided by the host layout via `useWizard()`. Each step owns
* its own footer via `<Step.Footer>`, which reads the nearest wizard
* via `useWizard()` so nested-wizard fall-through works automatically.
*
* When mounted without `initialStepId`, the wizard defaults to the
* first registered step. When `initialStepId` is provided, the wizard
* starts on that step instead — useful for resuming a flow from
* server-derived state.
*/
const Wizard = Object.assign(Root, { Step });

//#endregion
export { Wizard };
//# sourceMappingURL=Wizard.js.map