/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { __toESM } from "../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../node_modules/classnames/index.js";
import pconsole_default from "../../global/js/utils/pconsole.js";
import { pkg } from "../../settings.js";
import { usePreviousValue } from "../../global/js/hooks/usePreviousValue.js";
import { useRetrieveStepData } from "../../global/js/hooks/useRetrieveStepData.js";
import { StepNumberContext, StepsContext } from "./CreateFullPage.js";
import React, { forwardRef, isValidElement, useContext, useEffect, useState } from "react";
import PropTypes from "prop-types";
import { Column, FormGroup, Grid, Heading, Section } from "@carbon/react";

//#region src/components/CreateFullPage/CreateFullPageStep.tsx
/**
* Copyright IBM Corp. 2021, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const componentName = "CreateFullPageStep";
const blockClass = `${pkg.prefix}--create-full-page__step`;
const defaults = { includeStep: true };
const CreateFullPageStep = forwardRef(({ children, className, subtitle, description, disableSubmit, includeStep = defaults.includeStep, introStep, invalid, title, hasFieldset, fieldsetLegendText, onNext, onPrevious, onMount, secondaryLabel, ...rest }, ref) => {
	const stepsContext = useContext(StepsContext);
	const stepNumber = useContext(StepNumberContext);
	const [shouldIncludeStep, setShouldIncludeStep] = useState();
	const previousState = usePreviousValue({ currentStep: stepsContext?.currentStep });
	useRetrieveStepData({
		invalid,
		stepsContext,
		stepNumber,
		introStep,
		shouldIncludeStep,
		secondaryLabel,
		title
	});
	useEffect(() => {
		if (stepNumber === stepsContext?.currentStep && previousState?.currentStep !== stepsContext?.currentStep) stepsContext?.setOnMount(onMount);
	}, [
		onMount,
		stepsContext,
		stepNumber,
		previousState?.currentStep
	]);
	useEffect(() => {
		setShouldIncludeStep(includeStep);
	}, [
		includeStep,
		stepsContext,
		title
	]);
	useEffect(() => {
		if (stepNumber === stepsContext?.currentStep) {
			stepsContext.setIsDisabled(disableSubmit);
			stepsContext?.setOnNext(onNext);
			stepsContext?.setOnPrevious(onPrevious);
		}
	}, [
		stepsContext,
		stepNumber,
		disableSubmit,
		onNext,
		onPrevious
	]);
	const span = { span: 50 };
	const renderDescription = () => {
		if (description) {
			const common = {
				children: description,
				className: `${blockClass}-description`,
				...span
			};
			if (typeof description === "string") return /* @__PURE__ */ React.createElement(Column, {
				...common,
				as: "p"
			});
			else if (isValidElement(description)) return /* @__PURE__ */ React.createElement(Column, {
				...common,
				as: "div"
			});
		}
		return null;
	};
	return stepsContext ? /* @__PURE__ */ React.createElement(Section, {
		...rest,
		className: (0, import_classnames.default)(blockClass, className, {
			[`${blockClass}__step--hidden-step`]: stepNumber !== stepsContext?.currentStep,
			[`${blockClass}__step--visible-step`]: stepNumber === stepsContext?.currentStep
		}),
		ref
	}, /* @__PURE__ */ React.createElement(Grid, null, /* @__PURE__ */ React.createElement(Column, span, /* @__PURE__ */ React.createElement(Grid, null, /* @__PURE__ */ React.createElement(Column, {
		className: `${blockClass}-title`,
		as: Heading,
		...span
	}, title), subtitle && /* @__PURE__ */ React.createElement(Column, {
		className: `${blockClass}-subtitle`,
		as: "p",
		...span
	}, subtitle), renderDescription()))), hasFieldset ? /* @__PURE__ */ React.createElement(FormGroup, {
		legendText: fieldsetLegendText,
		className: `${blockClass}-fieldset`
	}, children) : children) : pconsole_default.warn(`You have tried using a ${componentName} component outside of a CreateFullPage. This is not allowed. ${componentName}s should always be children of the CreateFullPage`);
});
CreateFullPageStep.propTypes = {
	children: PropTypes.node,
	className: PropTypes.string,
	description: PropTypes.node,
	disableSubmit: PropTypes.bool,
	fieldsetLegendText: PropTypes.string,
	hasFieldset: PropTypes.bool,
	includeStep: PropTypes.bool,
	introStep: PropTypes.bool,
	invalid: PropTypes.bool,
	onMount: PropTypes.func,
	onNext: PropTypes.func,
	onPrevious: PropTypes.func,
	secondaryLabel: PropTypes.string,
	subtitle: PropTypes.string,
	title: PropTypes.node.isRequired
};

//#endregion
export { CreateFullPageStep };