/**
 * 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 uuidv4 from "../../../global/js/utils/uuidv4.js";
import React, { createContext, useState } from "react";
import PropTypes from "prop-types";

//#region src/components/ConditionBuilder/ConditionBuilderContext/ConditionBuilderProvider.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.
*/
const getEmptyState = (statementConfigCustom) => {
	const defaultStatement = statementConfigCustom?.[0];
	const defaultStatementId = defaultStatement?.id ?? "ifAll";
	return {
		operator: "or",
		groups: [{
			groupOperator: defaultStatement?.connector ?? "and",
			statement: defaultStatementId,
			id: uuidv4(),
			conditions: [{
				property: void 0,
				operator: "",
				value: "",
				popoverToOpen: "propertyField",
				id: uuidv4()
			}]
		}]
	};
};
const ConditionBuilderContext = createContext({
	rootState: { groups: [] },
	popOverSearchThreshold: 0
});
const ConditionBuilderProvider = (props) => {
	const [rootState, setRootState] = useState({ groups: [] });
	const [actionState, setActionState] = useState([]);
	const contextValue = {
		rootState,
		setRootState,
		actionState,
		setActionState,
		inputConfig: props.inputConfig,
		popOverSearchThreshold: props.popOverSearchThreshold,
		getOptions: props.getOptions,
		variant: props.variant,
		translateWithId: props.translateWithId,
		conditionBuilderRef: props.conditionBuilderRef,
		statementConfigCustom: props.statementConfigCustom,
		onAddItem: props.onAddItem,
		readOnly: props.readOnly
	};
	return /* @__PURE__ */ React.createElement(ConditionBuilderContext.Provider, { value: contextValue }, props.children);
};
ConditionBuilderProvider.propTypes = {
	children: PropTypes.node.isRequired,
	conditionBuilderRef: PropTypes.object,
	getOptions: PropTypes.func,
	inputConfig: PropTypes.shape({ properties: PropTypes.arrayOf(PropTypes.shape({
		id: PropTypes.string.isRequired,
		label: PropTypes.string.isRequired,
		icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
		description: PropTypes.string,
		type: PropTypes.oneOf([
			"text",
			"textarea",
			"number",
			"date",
			"option",
			"time",
			"custom"
		]).isRequired,
		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
			})),
			long: PropTypes.bool
		})
	})) }).isRequired,
	popOverSearchThreshold: PropTypes.number.isRequired,
	translateWithId: PropTypes.func,
	variant: PropTypes.string.isRequired
};

//#endregion
export { ConditionBuilderContext, ConditionBuilderProvider, getEmptyState };