/**
 * 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 { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { NON_HIERARCHICAL_VARIANT, blockClass } from "./utils/util.js";
import { ConditionBuilderProvider } from "./ConditionBuilderContext/ConditionBuilderProvider.js";
import { handleKeyDown } from "./utils/handleKeyboardEvents.js";
import ConditionBuilderContent from "./ConditionBuilderContent/ConditionBuilderContent.js";
import React, { useRef } from "react";
import PropTypes from "prop-types";
import { VStack } from "@carbon/react";

//#region src/components/ConditionBuilder/ConditionBuilder.tsx
/**
* Copyright IBM Corp. 2024, 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.
*/
/**@ts-ignore */
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const componentName = "ConditionBuilder";
/**
* TODO: A description of the component.
*/
const ConditionBuilder = React.forwardRef(({ className, inputConfig, startConditionLabel = "Add Condition", popOverSearchThreshold, getOptions, initialState, getConditionState, getActionsState, variant = NON_HIERARCHICAL_VARIANT, actions, translateWithId, statementConfigCustom, onAddItem, readOnly, ...rest }, ref) => {
	const localRef = useRef(null);
	const conditionBuilderRef = ref || localRef;
	const handleKeyDownHandler = (evt) => {
		handleKeyDown(evt, conditionBuilderRef, variant);
	};
	return /* @__PURE__ */ React.createElement(ConditionBuilderProvider, {
		inputConfig,
		popOverSearchThreshold,
		getOptions,
		variant,
		translateWithId,
		conditionBuilderRef,
		statementConfigCustom,
		onAddItem,
		readOnly: !!readOnly
	}, /* @__PURE__ */ React.createElement("div", {
		...rest,
		className: (0, import_classnames.default)(blockClass, className, { [`${blockClass}__readonly`]: readOnly }, {}),
		ref: conditionBuilderRef,
		...getDevtoolsProps(componentName)
	}, /* @__PURE__ */ React.createElement(VStack, {
		className: `${blockClass}__${variant}`,
		onKeyDown: handleKeyDownHandler
	}, /* @__PURE__ */ React.createElement(ConditionBuilderContent, {
		startConditionLabel,
		getConditionState,
		getActionsState,
		initialState,
		actions
	}))));
});
ConditionBuilder.displayName = componentName;
ConditionBuilder.propTypes = {
	actions: PropTypes.arrayOf(PropTypes.shape({
		id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
		label: PropTypes.string.isRequired
	})),
	className: PropTypes.string,
	getActionsState: PropTypes.func,
	getConditionState: PropTypes.func.isRequired,
	getOptions: PropTypes.func,
	initialState: PropTypes.shape({
		state: PropTypes.shape({
			groups: PropTypes.arrayOf(PropTypes.shape({
				groupOperator: PropTypes.string,
				statement: PropTypes.string,
				conditions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.shape({
					property: PropTypes.string,
					operator: PropTypes.string,
					value: PropTypes.oneOfType([
						PropTypes.string,
						PropTypes.arrayOf(PropTypes.shape({
							id: PropTypes.string,
							label: PropTypes.string
						})),
						PropTypes.shape({
							id: PropTypes.string,
							label: PropTypes.string
						})
					])
				}), PropTypes.object]))
			})),
			operator: PropTypes.string
		}),
		enabledDefault: PropTypes.bool
	}),
	inputConfig: PropTypes.shape({ properties: PropTypes.arrayOf(PropTypes.shape({
		id: PropTypes.string.isRequired,
		label: PropTypes.string.isRequired,
		icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
		type: PropTypes.oneOf([
			"text",
			"textarea",
			"number",
			"date",
			"option",
			"time",
			"custom"
		]).isRequired,
		description: PropTypes.string,
		getIsDisabled: PropTypes.func,
		getIsHidden: PropTypes.func,
		config: PropTypes.shape({
			options: PropTypes.arrayOf(PropTypes.shape({
				id: PropTypes.string.isRequired,
				label: PropTypes.string.isRequired,
				icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
			})),
			component: PropTypes.func,
			operators: PropTypes.arrayOf(PropTypes.shape({
				id: PropTypes.string.isRequired,
				label: PropTypes.string.isRequired
			})),
			valueFormatter: PropTypes.func
		})
	})) }).isRequired,
	onAddItem: PropTypes.func,
	popOverSearchThreshold: PropTypes.number.isRequired,
	readOnly: PropTypes.bool,
	startConditionLabel: PropTypes.string,
	statementConfigCustom: PropTypes.arrayOf(PropTypes.shape({
		id: PropTypes.string.isRequired,
		connector: PropTypes.oneOf(["and", "or"]).isRequired,
		label: PropTypes.string.isRequired,
		secondaryLabel: PropTypes.string
	})),
	translateWithId: PropTypes.func,
	variant: PropTypes.oneOf(["Non-Hierarchical", "Hierarchical"])
};

//#endregion
export { ConditionBuilder };