/**
 * 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 { blockClass } from "../../utils/util.js";
import { useTranslations } from "../../utils/useTranslations.js";
import React from "react";
import PropTypes from "prop-types";
import { NumberInput } from "@carbon/react";
//#region src/components/ConditionBuilder/ConditionBuilderItem/ConditionBuilderItemNumber/ConditionBuilderItemNumber.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 ConditionBuilderItemNumber = ({ conditionState, config, onChange }) => {
	const [invalidNumberWarnText] = useTranslations(["invalidNumberWarnText"]);
	const onChangeHandler = (e, { value }) => {
		if (value !== "" && !isNaN(value) && checkIfValid(value)) onChange(config?.unit ? `${value} ${config.unit}` : String(value));
		else onChange("INVALID");
	};
	const checkIfValid = (value) => {
		if (!config) return true;
		const { min, max } = config;
		if (max !== void 0 && min === void 0 && value > max) return false;
		if (min !== void 0 && max === void 0 && value < min) return false;
		if (min !== void 0 && max !== void 0 && (value < min || value > max)) return false;
		return true;
	};
	const getDefaultValue = () => {
		return conditionState.value?.split(" ")?.[0] ?? "";
	};
	return /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__item-number` }, /* @__PURE__ */ React.createElement(NumberInput, {
		...config,
		label: conditionState.property,
		hideLabel: true,
		id: conditionState.property?.replace(/\s/g, ""),
		invalidText: invalidNumberWarnText,
		allowEmpty: true,
		onChange: onChangeHandler,
		defaultValue: getDefaultValue()
	}));
};
ConditionBuilderItemNumber.propTypes = {
	/**
	* current condition object
	*/
	conditionState: PropTypes.object,
	/**
	* current config object that this property is part of
	*/
	config: PropTypes.object,
	/**
	* callback to update state oin date change
	*/
	onChange: PropTypes.func
};
//#endregion
export { ConditionBuilderItemNumber };
