/**
 * 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 { HIERARCHICAL_VARIANT, blockClass, focusThisField } from "../utils/util.js";
import { ConditionBuilderContext } from "../ConditionBuilderContext/ConditionBuilderProvider.js";
import { ConditionBuilderButton } from "../ConditionBuilderButton/ConditionBuilderButton.js";
import { useTranslations } from "../utils/useTranslations.js";
import { ConditionBuilderItem } from "../ConditionBuilderItem/ConditionBuilderItem.js";
import { ItemOption } from "../ConditionBuilderItem/ConditionBuilderItemOption/ItemOption.js";
import { useDataConfigs } from "../utils/useDataConfigs.js";
import React, { useCallback, useContext } from "react";
import PropTypes from "prop-types";

//#region src/components/ConditionBuilder/ConditionBuilderConnector/ConditionConnector.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 ConditionConnector = ({ operator, className, onChange, ...rest }) => {
	const { variant, conditionBuilderRef } = useContext(ConditionBuilderContext);
	const [connectorText] = useTranslations(["connectorText"]);
	const { connectorConfig } = useDataConfigs();
	const handleConnectorHover = useCallback((parentGroup, isHover) => {
		if (isHover) parentGroup.classList.add("hoveredConnector");
		else parentGroup.classList.remove("hoveredConnector");
	}, []);
	const activeConnectorHandler = (evt) => {
		const parentGroup = evt?.currentTarget.closest(".eachGroup");
		handleConnectorHover(parentGroup, true);
	};
	const inActiveConnectorHandler = (evt) => {
		const parentGroup = evt?.currentTarget.closest(".eachGroup");
		handleConnectorHover(parentGroup, false);
	};
	const onChangeHandler = (op, evt) => {
		onChange?.(op);
		focusThisField(evt, conditionBuilderRef);
	};
	return variant == HIERARCHICAL_VARIANT ? /* @__PURE__ */ React.createElement("span", { className: `${className} ${blockClass}__connector ${blockClass}__connector--disabled` }, /* @__PURE__ */ React.createElement(ConditionBuilderButton, { label: operator })) : /* @__PURE__ */ React.createElement(ConditionBuilderItem, {
		label: operator,
		title: connectorText,
		"data-name": "connectorField",
		onMouseEnter: activeConnectorHandler,
		onMouseLeave: inActiveConnectorHandler,
		onFocus: activeConnectorHandler,
		onBlur: inActiveConnectorHandler,
		...rest,
		popOverClassName: `${className} ${blockClass}__connector`,
		className: `${blockClass}__connector-button`
	}, /* @__PURE__ */ React.createElement(ItemOption, {
		config: { options: connectorConfig },
		conditionState: {
			value: operator,
			label: connectorText
		},
		onChange: onChangeHandler
	}));
};
ConditionConnector.propTypes = {
	className: PropTypes.string,
	onChange: PropTypes.func,
	operator: PropTypes.string
};

//#endregion
export { ConditionConnector as default };