/**
 * 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 { pkg } from "../../settings.js";
import { useCreateComponentFocus } from "../../global/js/hooks/useCreateComponentFocus.js";
import { useCreateComponentStepChange } from "../../global/js/hooks/useCreateComponentStepChange.js";
import { usePreviousValue } from "../../global/js/hooks/usePreviousValue.js";
import { getNumberOfHiddenSteps } from "../../global/js/utils/getNumberOfHiddenSteps.js";
import { useResetCreateComponent } from "../../global/js/hooks/useResetCreateComponent.js";
import { useValidCreateStepCount } from "../../global/js/hooks/useValidCreateStepCount.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { deprecateProp } from "../../global/js/utils/props-helper.js";
import { CreateInfluencer } from "../CreateInfluencer/CreateInfluencer.js";
import { lastIndexInArray } from "../../global/js/utils/lastIndexInArray.js";
import { TearsheetShell } from "../Tearsheet/TearsheetShell.js";
import React, { createContext, forwardRef, useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Form } from "@carbon/react";

//#region src/components/CreateTearsheet/CreateTearsheet.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 = "CreateTearsheet";
const blockClass = `${pkg.prefix}--tearsheet-create`;
const StepsContext = createContext(null);
const StepNumberContext = createContext(-1);
const CreateTearsheet = forwardRef(({ backButtonText, cancelButtonText, children, className, experimentalSecondarySubmitText, firstFocusElement, description, hasError, influencerWidth = "narrow", initialStep, label, nextButtonText, onClose, onRequestSubmit, open, selectorPrimaryFocus, slug: deprecated_slug, decorator, submitButtonText, title, verticalPosition = "normal", ...rest }, ref) => {
	const [createTearsheetActions, setCreateTearsheetActions] = useState([]);
	const [shouldViewAll, setShouldViewAll] = useState(false);
	const [currentStep, setCurrentStep] = useState(0);
	const [isSubmitting, setIsSubmitting] = useState(false);
	const [isDisabled, setIsDisabled] = useState(false);
	const [loadingPrevious, setLoadingPrevious] = useState(false);
	const [onPrevious, setOnPrevious] = useState();
	const [onNext, setOnNext] = useState();
	const [onMount, setOnMount] = useState();
	const [secondaryButtonDisabled, setSecondaryButtonDisabled] = useState(false);
	const [stepData, setStepData] = useState([]);
	const [firstIncludedStep, setFirstIncludedStep] = useState(1);
	const [lastIncludedStep, setLastIncludedStep] = useState();
	const [experimentalSecondarySubmit, setExperimentalSecondarySubmit] = useState();
	const previousState = usePreviousValue({
		currentStep,
		open
	});
	const contentRef = useRef(null);
	useEffect(() => {
		const firstItem = stepData.findIndex((item) => item?.shouldIncludeStep) + 1;
		const lastItem = lastIndexInArray(stepData, "shouldIncludeStep", true);
		if (firstItem !== firstIncludedStep) {
			setCurrentStep(firstItem);
			setFirstIncludedStep(firstItem);
		}
		if (lastItem !== lastIncludedStep) setLastIncludedStep(lastItem);
		if (open && initialStep) {
			const numberOfHiddenSteps = getNumberOfHiddenSteps(stepData, initialStep);
			setCurrentStep(Number(initialStep + numberOfHiddenSteps));
		}
	}, [
		firstIncludedStep,
		initialStep,
		lastIncludedStep,
		open,
		stepData
	]);
	useCreateComponentFocus({
		previousState,
		currentStep,
		blockClass: `.${blockClass} .${pkg.prefix}--tearsheet-create__step`,
		onMount,
		firstFocusElement: firstFocusElement || selectorPrimaryFocus
	});
	useValidCreateStepCount(stepData.length, componentName);
	useResetCreateComponent({
		firstIncludedStep,
		previousState,
		open,
		setCurrentStep,
		stepData,
		initialStep,
		totalSteps: stepData?.length,
		componentName
	});
	useCreateComponentStepChange({
		firstIncludedStep,
		lastIncludedStep,
		stepData,
		onPrevious,
		onNext,
		isSubmitDisabled: isDisabled,
		setCurrentStep,
		setIsSubmitting,
		setShouldViewAll,
		setLoadingPrevious,
		loadingPrevious,
		onClose,
		onRequestSubmit,
		componentName,
		currentStep,
		shouldViewAll,
		backButtonText,
		cancelButtonText,
		submitButtonText,
		nextButtonText,
		isSubmitting,
		secondaryButtonDisabled,
		componentBlockClass: blockClass,
		experimentalSecondarySubmit,
		experimentalSecondarySubmitText: experimentalSecondarySubmit?.labelText ? experimentalSecondarySubmit.labelText : experimentalSecondarySubmitText,
		setCreateComponentActions: setCreateTearsheetActions
	});
	return /* @__PURE__ */ React.createElement(TearsheetShell, {
		...getDevtoolsProps(componentName),
		...rest,
		ref,
		actions: createTearsheetActions,
		className: (0, import_classnames.default)(blockClass, className),
		description,
		influencer: /* @__PURE__ */ React.createElement(CreateInfluencer, {
			currentStep,
			stepData
		}),
		influencerWidth,
		label,
		onClose,
		open,
		size: "wide",
		decorator: decorator || deprecated_slug,
		title,
		verticalPosition,
		closeIconDescription: "",
		currentStep,
		hasError,
		selectorPrimaryFocus
	}, /* @__PURE__ */ React.createElement("div", {
		className: `${blockClass}__content`,
		ref: contentRef
	}, /* @__PURE__ */ React.createElement(Form, {
		"aria-label": title,
		onSubmit: (e) => e.preventDefault()
	}, /* @__PURE__ */ React.createElement(StepsContext.Provider, { value: {
		currentStep,
		setExperimentalSecondarySubmit,
		setIsDisabled,
		setOnPrevious: (fn) => setOnPrevious(() => fn),
		setOnNext: (fn) => setOnNext(() => fn),
		setOnMount: (fn) => setOnMount(() => fn),
		setSecondaryButtonDisabled,
		setStepData,
		stepData
	} }, React.Children.toArray(children).filter(Boolean).map((child, index) => /* @__PURE__ */ React.createElement(StepNumberContext.Provider, {
		value: index + 1,
		key: index
	}, child))))));
});
CreateTearsheet.displayName = componentName;
const deprecatedProps = { slug: deprecateProp(PropTypes.node, "Property replaced by `decorator`") };
CreateTearsheet.propTypes = {
	backButtonText: PropTypes.string.isRequired,
	cancelButtonText: PropTypes.string.isRequired,
	children: PropTypes.node,
	className: PropTypes.string,
	decorator: PropTypes.node,
	description: PropTypes.node,
	experimentalSecondarySubmitText: PropTypes.string,
	firstFocusElement: PropTypes.string,
	hasError: PropTypes.bool,
	influencerWidth: PropTypes.oneOf(["narrow", "wide"]),
	initialStep: PropTypes.number,
	label: PropTypes.node,
	nextButtonText: PropTypes.string.isRequired,
	onClose: PropTypes.func,
	onRequestSubmit: PropTypes.func.isRequired,
	open: PropTypes.bool,
	selectorPrimaryFocus: PropTypes.string,
	submitButtonText: PropTypes.string.isRequired,
	title: PropTypes.node,
	verticalPosition: PropTypes.oneOf(["normal", "lower"]),
	...deprecatedProps
};

//#endregion
export { CreateTearsheet, StepNumberContext, StepsContext };