/**
 * 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 "./CreateTearsheet.js";
import React, { forwardRef, isValidElement, useContext, useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Column, FormGroup, Grid } from "@carbon/react";

//#region src/components/CreateTearsheet/CreateTearsheetStep.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 = "CreateTearsheetStep";
const blockClass = `${pkg.prefix}--tearsheet-create__step`;
const defaults = {
	hasFieldset: true,
	includeStep: true
};
const CreateTearsheetStep = forwardRef(({ children, className, description, disableSubmit, experimentalSecondarySubmit, fieldsetLegendText, fieldsetLegendId, hasFieldset = defaults.hasFieldset, includeStep = defaults.includeStep, introStep, invalid, onMount, onNext, onPrevious, secondaryButtonDisabled, secondaryLabel, subtitle, title, ...rest }, ref) => {
	const localRef = useRef(null);
	const stepRef = ref || localRef;
	const stepRefValue = stepRef.current;
	const stepsContext = useContext(StepsContext);
	const stepNumber = useContext(StepNumberContext);
	const [shouldIncludeStep, setShouldIncludeStep] = useState(includeStep);
	const previousState = usePreviousValue({ currentStep: stepsContext?.currentStep });
	useRetrieveStepData({
		stepsContext,
		stepNumber,
		introStep: !!introStep,
		shouldIncludeStep,
		secondaryLabel: secondaryLabel || "",
		title,
		invalid: !!invalid
	});
	useEffect(() => {
		if (stepNumber === stepsContext?.currentStep && previousState?.currentStep !== stepsContext?.currentStep) {
			stepsContext?.setOnMount(onMount);
			stepsContext?.setExperimentalSecondarySubmit(experimentalSecondarySubmit);
		}
	}, [
		onMount,
		experimentalSecondarySubmit,
		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?.setSecondaryButtonDisabled?.(!!secondaryButtonDisabled);
			stepsContext?.setExperimentalSecondarySubmit(experimentalSecondarySubmit);
		}
	}, [
		stepsContext,
		stepNumber,
		disableSubmit,
		onNext,
		onPrevious,
		secondaryButtonDisabled,
		stepRef,
		stepRefValue,
		experimentalSecondarySubmit
	]);
	const renderDescription = () => {
		if (description) {
			if (typeof description === "string") return /* @__PURE__ */ React.createElement("p", { className: `${blockClass}--description` }, description);
			if (isValidElement(description)) return /* @__PURE__ */ React.createElement("div", { className: `${blockClass}--description` }, description);
		}
		return null;
	};
	return stepsContext ? /* @__PURE__ */ React.createElement("div", {
		ref: stepRef,
		inert: stepNumber !== stepsContext?.currentStep
	}, /* @__PURE__ */ React.createElement(Grid, {
		...rest,
		className: (0, import_classnames.default)(blockClass, className, {
			[`${blockClass}__step--hidden-step`]: stepNumber !== stepsContext?.currentStep,
			[`${blockClass}__step--visible-step`]: stepNumber === stepsContext?.currentStep
		})
	}, /* @__PURE__ */ React.createElement(Column, {
		xlg: 12,
		lg: 12,
		md: 8,
		sm: 4
	}, /* @__PURE__ */ React.createElement("h4", { className: `${blockClass}--title` }, title), subtitle && /* @__PURE__ */ React.createElement("h5", { className: `${blockClass}--subtitle` }, subtitle), renderDescription()), /* @__PURE__ */ React.createElement(Column, { span: 100 }, hasFieldset ? /* @__PURE__ */ React.createElement(FormGroup, {
		legendText: fieldsetLegendText,
		className: `${blockClass}--fieldset`,
		legendId: fieldsetLegendId
	}, children) : children))) : pconsole_default.warn(`You have tried using a ${componentName} component outside of a CreateTearsheet. This is not allowed. ${componentName}s should always be children of the CreateTearsheet`);
});
CreateTearsheetStep.propTypes = {
	children: PropTypes.node,
	className: PropTypes.string,
	description: PropTypes.node,
	disableSubmit: PropTypes.bool,
	experimentalSecondarySubmit: PropTypes.shape({
		labelText: PropTypes.string,
		disabled: PropTypes.bool,
		hideSecondarySubmit: PropTypes.bool,
		onClick: PropTypes.func
	}),
	fieldsetLegendId: PropTypes.node,
	fieldsetLegendText: PropTypes.string,
	hasFieldset: PropTypes.bool,
	includeStep: PropTypes.bool,
	introStep: PropTypes.bool,
	invalid: PropTypes.bool,
	onMount: PropTypes.func,
	onNext: PropTypes.func,
	onPrevious: PropTypes.func,
	secondaryButtonDisabled: PropTypes.bool,
	secondaryLabel: PropTypes.string,
	subtitle: PropTypes.string,
	title: PropTypes.node.isRequired
};

//#endregion
export { CreateTearsheetStep };