/**
 * 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 { blockClass } from "../utils/util.js";
import { ConditionBuilderContext } from "../ConditionBuilderContext/ConditionBuilderProvider.js";
import { ConditionBuilderButton } from "../ConditionBuilderButton/ConditionBuilderButton.js";
import { useTranslations } from "../utils/useTranslations.js";
import React, { useContext } from "react";
import PropTypes from "prop-types";
import { AddAlt, TextNewLine } from "@carbon/react/icons";

//#region src/components/ConditionBuilder/ConditionBuilderAdd/ConditionBuilderAdd.tsx
/**
* Copyright IBM Corp. 2024
*
* 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 ConditionBuilderAdd = ({ className, onClick, addConditionSubGroupHandler, showConditionSubGroupPreviewHandler, hideConditionSubGroupPreviewHandler, showConditionPreviewHandler, hideConditionPreviewHandler, enableSubGroup, buttonLabel, tabIndex, group }) => {
	const [addConditionText, addConditionRowText, addSubgroupText] = useTranslations([
		"addConditionText",
		"addConditionRowText",
		"addSubgroupText"
	]);
	const { onAddItem, rootState } = useContext(ConditionBuilderContext);
	const onClickHandler = () => {
		const { preventAdd } = onAddItem?.({
			type: "condition",
			state: rootState,
			group
		}) ?? {};
		if (!preventAdd) {
			hideConditionPreviewHandler?.();
			onClick();
		}
	};
	const previewHandlers = () => {
		return enableSubGroup ? {
			onMouseEnter: showConditionPreviewHandler,
			onMouseLeave: hideConditionPreviewHandler,
			onFocus: showConditionPreviewHandler,
			onBlur: hideConditionPreviewHandler
		} : {};
	};
	const previewHandlersForSubgroup = () => ({
		onMouseEnter: showConditionSubGroupPreviewHandler,
		onMouseLeave: hideConditionSubGroupPreviewHandler,
		onFocus: showConditionSubGroupPreviewHandler,
		onBlur: hideConditionSubGroupPreviewHandler
	});
	const handleAddSubGroup = () => {
		const { preventAdd } = onAddItem?.({
			type: "subgroup",
			state: rootState,
			group
		}) ?? {};
		if (!preventAdd) addConditionSubGroupHandler?.();
	};
	const getAriaLabel = () => {
		return buttonLabel ? buttonLabel : !enableSubGroup ? addConditionRowText : void 0;
	};
	const wrapperProps = enableSubGroup ? { role: "gridcell" } : {};
	return /* @__PURE__ */ React.createElement("div", {
		className: `${className} ${blockClass}__add-button-wrapper`,
		role: !enableSubGroup ? "gridcell" : "none",
		"aria-label": getAriaLabel()
	}, /* @__PURE__ */ React.createElement(ConditionBuilderButton, {
		renderIcon: AddAlt,
		onClick: onClickHandler,
		...previewHandlers(),
		wrapperProps,
		className: `${blockClass}__add-button`,
		hideLabel: true,
		"data-name": "addButton",
		label: buttonLabel ?? addConditionText,
		tabIndex,
		wrapperClassName: `${blockClass}__add-condition-wrapper`
	}), enableSubGroup && /* @__PURE__ */ React.createElement(ConditionBuilderButton, {
		renderIcon: TextNewLine,
		onClick: handleAddSubGroup,
		className: (0, import_classnames.default)(`${blockClass}__add-condition-sub-group`),
		hideLabel: true,
		label: addSubgroupText,
		wrapperProps,
		wrapperClassName: `${blockClass}__add-condition-sub-group-wrapper ${blockClass}__gap-left`,
		...previewHandlersForSubgroup()
	}));
};
ConditionBuilderAdd.propTypes = {
	addConditionSubGroupHandler: PropTypes.func,
	buttonLabel: PropTypes.string,
	className: PropTypes.string,
	enableSubGroup: PropTypes.bool,
	hideConditionPreviewHandler: PropTypes.func,
	hideConditionSubGroupPreviewHandler: PropTypes.func,
	onClick: PropTypes.func,
	showConditionPreviewHandler: PropTypes.func,
	showConditionSubGroupPreviewHandler: PropTypes.func,
	tabIndex: PropTypes.number
};

//#endregion
export { ConditionBuilderAdd as default };