/**
 * 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 { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { prepareProps } from "../../global/js/utils/props-helper.js";
import { TearsheetShell } from "../Tearsheet/TearsheetShell.js";
import React, { createContext, forwardRef, useRef, useState } from "react";
import PropTypes from "prop-types";
import { Form, SideNav, SideNavItems, SideNavMenuItem } from "@carbon/react";

//#region src/components/EditTearsheet/EditTearsheet.tsx
/**
* Copyright IBM Corp. 2022, 2023
*
* 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 = "EditTearsheet";
const blockClass = `${pkg.prefix}--tearsheet-edit`;
const FormContext = createContext(null);
const FormNumberContext = createContext(0);
const defaults = {
	verticalPosition: "normal",
	influencerWidth: "narrow",
	sideNavAriaLabel: "Side navigation"
};
/**
* **This component is deprecated.** <br>
* Use Tearsheet with medium to complex edits. See usage guidance for further information.
* @deprecated
*/
const EditTearsheet = forwardRef(({ cancelButtonText, children, className, description, influencerWidth = defaults.influencerWidth, label, onClose, open, submitButtonText, title, verticalPosition = defaults.verticalPosition, onRequestSubmit, onFormChange, sideNavAriaLabel = defaults.sideNavAriaLabel, ...rest }, ref) => {
	const [isSubmitting, setIsSubmitting] = useState(false);
	const handleOnRequestSubmit = async () => {
		setIsSubmitting(true);
		try {
			await onRequestSubmit();
		} catch (error) {
			console.warn(`${componentName} submit error: ${error}`);
		}
		setIsSubmitting(false);
	};
	const actions = [{
		key: "edit-action-button-submit",
		label: submitButtonText,
		onClick: () => handleOnRequestSubmit(),
		loading: isSubmitting,
		kind: "primary"
	}, {
		key: "edit-action-button-cancel",
		label: cancelButtonText,
		onClick: onClose,
		kind: "ghost"
	}];
	const [currentForm, setCurrentForm] = useState(0);
	const [formTitles, setFormTitles] = useState([]);
	const contentRef = useRef(null);
	const handleCurrentForm = (formIndex) => {
		setCurrentForm(formIndex);
		if (onFormChange) onFormChange(formIndex);
	};
	function defaultInfluencer() {
		return /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__side-nav-wrapper` }, /* @__PURE__ */ React.createElement(SideNav, {
			"aria-label": sideNavAriaLabel,
			className: `${blockClass}__side-nav`,
			expanded: true,
			isFixedNav: false
		}, /* @__PURE__ */ React.createElement(SideNavItems, null, formTitles.map((title, index) => {
			return /* @__PURE__ */ React.createElement(SideNavMenuItem, {
				key: index,
				onClick: () => handleCurrentForm(index),
				isActive: currentForm === index
			}, title);
		}))));
	}
	return /* @__PURE__ */ React.createElement(TearsheetShell, {
		...getDevtoolsProps(componentName),
		...prepareProps(rest),
		actions,
		children,
		className: (0, import_classnames.default)(blockClass, className),
		description,
		influencerPosition: "left",
		influencerWidth,
		label,
		onClose,
		open,
		size: "wide",
		title,
		verticalPosition,
		...rest,
		hasCloseIcon: false,
		influencer: defaultInfluencer(),
		ref
	}, /* @__PURE__ */ React.createElement("div", {
		className: `${blockClass}__content`,
		ref: contentRef,
		role: "main"
	}, /* @__PURE__ */ React.createElement(Form, null, /* @__PURE__ */ React.createElement(FormContext.Provider, { value: {
		currentForm,
		setFormTitles
	} }, React.Children.map(children, (child, index) => /* @__PURE__ */ React.createElement(FormNumberContext.Provider, { value: index }, child))))));
});
/**@ts-ignore*/
EditTearsheet.deprecated = {
	level: "warn",
	details: `This component is deprecated and will be removed in the next major version.`
};
EditTearsheet.displayName = componentName;
EditTearsheet.propTypes = {
	cancelButtonText: PropTypes.string,
	children: PropTypes.node,
	className: PropTypes.string,
	description: PropTypes.node,
	influencerWidth: PropTypes.oneOf(["narrow", "wide"]),
	label: PropTypes.node,
	onClose: PropTypes.func,
	onFormChange: PropTypes.func,
	onRequestSubmit: PropTypes.func.isRequired,
	open: PropTypes.bool,
	sideNavAriaLabel: PropTypes.string,
	submitButtonText: PropTypes.string,
	title: PropTypes.node,
	verticalPosition: PropTypes.oneOf(["normal", "lower"])
};

//#endregion
export { EditTearsheet, FormContext, FormNumberContext };