/**
 * 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 { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { InterstitialScreenContext, blockClass } from "./context.js";
import { Carousel } from "../Carousel/Carousel.js";
import React, { isValidElement, useContext, useEffect, useState } from "react";
import PropTypes from "prop-types";
import { ModalBody } from "@carbon/react";

//#region src/components/InterstitialScreen/InterstitialScreenBody.tsx
/**
* Copyright IBM Corp. 2024, 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.
*/
const InterstitialScreenBody = React.forwardRef((props, ref) => {
	const { className = "", contentRenderer, ...rest } = props;
	const bodyBlockClass = `${blockClass}--internal-body`;
	const [stepType, setStepType] = useState();
	const { setBodyChildrenData, bodyChildrenData, isFullScreen, setProgStep, bodyScrollRef, scrollRef, handleGotoStep, progStep, setStepCount, disableButtonConfig, setDisableButtonConfig } = useContext(InterstitialScreenContext);
	const [scrollPercent, setScrollPercent] = useState(-1);
	useEffect(() => {
		const _bodyContent = contentRenderer?.({
			handleGotoStep,
			progStep,
			disableActionButton
		}) ?? props.children;
		const children = isValidElement(_bodyContent) && _bodyContent.props.children != null ? _bodyContent.props.children : _bodyContent;
		const validChildren = React.Children.toArray(children).filter(isValidElement);
		if (validChildren.length > 1) {
			setStepType("multi");
			setStepCount?.(validChildren.length);
			setBodyChildrenData?.(validChildren);
		} else {
			setStepType("single");
			setStepCount?.(1);
			setBodyChildrenData?.(_bodyContent);
		}
	}, [
		props.children,
		contentRenderer,
		progStep
	]);
	useEffect(() => {
		if (scrollPercent !== -1) {
			const currentStep = scrollPercent / (bodyChildrenData && bodyChildrenData.length > 1 ? parseFloat((1 / (bodyChildrenData.length - 1)).toFixed(2)) : 0);
			setProgStep?.(Math.ceil(currentStep));
		}
	}, [scrollPercent]);
	const disableActionButton = (config) => {
		setDisableButtonConfig?.({
			...disableButtonConfig,
			...config
		});
	};
	const onScrollHandler = (scrollPercent) => setScrollPercent(scrollPercent);
	const renderBody = () => /* @__PURE__ */ React.createElement("div", {
		className: `${blockClass}--body ${className}`,
		ref: bodyScrollRef ?? ref,
		...getDevtoolsProps("InterstitialScreenBody"),
		...isFullScreen ? rest : {}
	}, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}--content` }, stepType === "multi" ? /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__carousel` }, /* @__PURE__ */ React.createElement(Carousel, {
		disableArrowScroll: true,
		disableResetOnResize: true,
		ref: scrollRef,
		onScroll: onScrollHandler
	}, bodyChildrenData)) : stepType === "single" ? bodyChildrenData : ""));
	return isFullScreen ? renderBody() : /* @__PURE__ */ React.createElement(ModalBody, {
		ref,
		className: bodyBlockClass,
		...rest
	}, renderBody());
});
InterstitialScreenBody.propTypes = {
	children: PropTypes.node,
	className: PropTypes.string,
	contentRenderer: PropTypes.func
};

//#endregion
export { InterstitialScreenBody as default };