/**
 * 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 { useValidCreateStepCount } from "../../global/js/hooks/useValidCreateStepCount.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { SimpleHeader } from "../SimpleHeader/SimpleHeader.js";
import { ActionSet } from "../ActionSet/ActionSet.js";
import { CreateInfluencer } from "../CreateInfluencer/CreateInfluencer.js";
import { lastIndexInArray } from "../../global/js/utils/lastIndexInArray.js";
import React, { createContext, isValidElement, useEffect, useState } from "react";
import PropTypes from "prop-types";
import { Button, ComposedModal, Form, ModalBody, ModalFooter, ModalHeader, Tooltip } from "@carbon/react";

//#region src/components/CreateFullPage/CreateFullPage.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 blockClass = `${pkg.prefix}--create-full-page`;
const componentName = "CreateFullPage";
const StepsContext = createContext(null);
const StepNumberContext = createContext(-1);
/**
* Use with creations that must be completed in order for a service to be usable.
*
* ### Grid
*
* The `CreateFullPage` component utilizes Carbons' grid system in the inner
content of the main section inside of the component. You can read more guidance
on the Carbon's grid system
[here](https://www.carbondesignsystem.com/guidelines/2x-grid/overview). You can
include `<Row>` and `<Column>` components inside of each `CreateFullPageStep`
component to get the desired affect.
*/
const CreateFullPage = React.forwardRef(({ breadcrumbsOverflowAriaLabel, breadcrumbs, backButtonText, cancelButtonText, children, className, initialStep, maxVisibleBreadcrumbs, modalDangerButtonText, modalDescription, modalSecondaryButtonText, modalTitle, nextButtonText, onClickInfluencerStep, onClose, onRequestSubmit, firstFocusElement, submitButtonText, noTrailingSlash, title, secondaryTitle, breadcrumbOverflowTooltipAlign = "right", ...rest }, ref) => {
	const [createFullPageActions, setCreateFullPageActions] = useState([]);
	const [shouldViewAll, setShouldViewAll] = useState(false);
	const [currentStep, setCurrentStep] = useState(1);
	const [isSubmitting, setIsSubmitting] = useState(false);
	const [modalIsOpen, setModalIsOpen] = useState(false);
	const previousState = usePreviousValue({ currentStep });
	const [isDisabled, setIsDisabled] = useState(false);
	const [onPrevious, setOnPrevious] = useState();
	const [onNext, setOnNext] = useState();
	const [onMount, setOnMount] = useState();
	const [stepData, setStepData] = useState([]);
	const [firstIncludedStep, setFirstIncludedStep] = useState(1);
	const [lastIncludedStep, setLastIncludedStep] = useState();
	const stepLength = React.Children.toArray(children).filter((item) => isValidElement(item) && item.props.includeStep !== false).length;
	useEffect(() => {
		const firstItem = stepData.findIndex((item) => item?.shouldIncludeStep) + 1;
		const lastItem = lastIndexInArray(stepData, "shouldIncludeStep", true);
		if (firstItem !== firstIncludedStep) setFirstIncludedStep(firstItem);
		if (lastItem !== lastIncludedStep) setLastIncludedStep(lastItem);
		if (Number(initialStep) > stepLength || Number(initialStep) <= 0) setCurrentStep(1);
		else if (initialStep) {
			const numberOfHiddenSteps = getNumberOfHiddenSteps(stepData, initialStep);
			setCurrentStep(Number(initialStep + numberOfHiddenSteps));
		}
	}, [
		stepData,
		firstIncludedStep,
		lastIncludedStep,
		initialStep,
		modalIsOpen,
		stepLength
	]);
	useEffect(() => {
		checkForValidInitialStep();
	}, [initialStep]);
	useCreateComponentFocus({
		previousState,
		currentStep,
		blockClass: `.${blockClass} .${pkg.prefix}--create-full-page__step`,
		onMount,
		firstFocusElement
	});
	useValidCreateStepCount(stepData.length, componentName);
	useCreateComponentStepChange({
		firstIncludedStep,
		lastIncludedStep,
		stepData,
		onPrevious,
		onNext,
		isSubmitDisabled: isDisabled,
		setCurrentStep,
		setIsSubmitting,
		setShouldViewAll,
		onClose,
		onRequestSubmit,
		componentName,
		currentStep,
		shouldViewAll,
		backButtonText,
		cancelButtonText,
		submitButtonText,
		nextButtonText,
		isSubmitting,
		componentBlockClass: blockClass,
		setCreateComponentActions: setCreateFullPageActions,
		setModalIsOpen
	});
	const checkForValidInitialStep = () => {
		if (initialStep && stepLength && Number(initialStep) > stepLength || Number(initialStep) <= 0) console.warn(`${componentName}: An invalid \`initialStep\` prop was supplied. The \`initialStep\` prop should be a number that is greater than 0 or less than or equal to the number of steps your ${componentName} has.`);
	};
	/* istanbul ignore next */
	return /* @__PURE__ */ React.createElement("div", {
		...rest,
		ref,
		className: (0, import_classnames.default)(blockClass, className),
		...getDevtoolsProps(componentName)
	}, (title || breadcrumbs) && /* @__PURE__ */ React.createElement(SimpleHeader, {
		title,
		breadcrumbs,
		noTrailingSlash,
		overflowAriaLabel: breadcrumbsOverflowAriaLabel,
		maxVisible: maxVisibleBreadcrumbs,
		className: `${blockClass}__header`,
		overflowTooltipAlign: breadcrumbOverflowTooltipAlign
	}), /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__influencer-and-body-container` }, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__influencer` }, /* @__PURE__ */ React.createElement(CreateInfluencer, {
		stepData,
		currentStep,
		title: secondaryTitle,
		onClickStep: onClickInfluencerStep
	})), /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__body` }, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__main` }, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__content` }, /* @__PURE__ */ React.createElement(Form, {
		className: `${blockClass}__form`,
		"aria-label": title,
		onSubmit: (e) => e.preventDefault()
	}, /* @__PURE__ */ React.createElement(StepsContext.Provider, { value: {
		currentStep,
		setIsDisabled,
		setOnPrevious: (fn) => setOnPrevious(() => fn),
		setOnNext: (fn) => setOnNext(() => fn),
		setOnMount: (fn) => setOnMount(() => fn),
		setStepData,
		stepData
	} }, React.Children.toArray(children).filter(Boolean).map((child, index) => /* @__PURE__ */ React.createElement(StepNumberContext.Provider, {
		value: index + 1,
		key: index
	}, child))))), /* @__PURE__ */ React.createElement(ActionSet, {
		className: `${blockClass}__buttons`,
		actions: createFullPageActions,
		buttonSize: "2xl",
		size: "2xl"
	}))), /* @__PURE__ */ React.createElement(ComposedModal, {
		className: `${blockClass}__modal`,
		size: "sm",
		open: modalIsOpen,
		"aria-label": modalTitle,
		onClose: () => {
			setModalIsOpen(false);
		}
	}, /* @__PURE__ */ React.createElement(ModalHeader, { title: modalTitle }), /* @__PURE__ */ React.createElement(ModalBody, null, /* @__PURE__ */ React.createElement("p", null, modalDescription)), /* @__PURE__ */ React.createElement(ModalFooter, null, /* @__PURE__ */ React.createElement(Button, {
		type: "button",
		kind: "secondary",
		onClick: () => {
			setModalIsOpen(!modalIsOpen);
		},
		"data-modal-primary-focus": true
	}, modalSecondaryButtonText), /* @__PURE__ */ React.createElement(Button, {
		type: "button",
		kind: "danger",
		onClick: onClose
	}, modalDangerButtonText)))));
});
CreateFullPage.displayName = componentName;
CreateFullPage.propTypes = {
	backButtonText: PropTypes.string.isRequired,
	breadcrumbOverflowTooltipAlign: Tooltip.propTypes.align,
	breadcrumbs: PropTypes.arrayOf(PropTypes.shape({
		key: PropTypes.string.isRequired,
		label: PropTypes.string.isRequired,
		href: PropTypes.string,
		title: PropTypes.string,
		isCurrentPage: PropTypes.bool
	})),
	breadcrumbsOverflowAriaLabel: PropTypes.string,
	cancelButtonText: PropTypes.string.isRequired,
	children: PropTypes.node,
	className: PropTypes.string,
	firstFocusElement: PropTypes.string,
	initialStep: PropTypes.number,
	maxVisibleBreadcrumbs: PropTypes.number,
	modalDangerButtonText: PropTypes.string.isRequired,
	modalDescription: PropTypes.string,
	modalSecondaryButtonText: PropTypes.string.isRequired,
	modalTitle: PropTypes.string.isRequired,
	nextButtonText: PropTypes.string.isRequired,
	noTrailingSlash: PropTypes.bool,
	onClickInfluencerStep: PropTypes.func,
	onClose: PropTypes.func,
	onRequestSubmit: PropTypes.func.isRequired,
	secondaryTitle: PropTypes.string,
	sideNavAriaLabel: PropTypes.string,
	submitButtonText: PropTypes.string.isRequired,
	title: PropTypes.string
};

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