'use client';


import { Input } from "../Input/Input.mjs";
import { Select } from "../Select/Select.mjs";
import { useEffect, useMemo, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/components/DictionaryFieldEditor/EnumKeyInput.tsx
const extractInitialState = (value) => {
	let comparator;
	let numberValue;
	if (typeof value === "number" || !Number.isNaN(Number(value))) {
		comparator = "=";
		numberValue = value.toString();
	} else if (typeof value === "string") {
		const matchedComparator = [
			"<=",
			">=",
			"<",
			">",
			"="
		].find((comp) => value.startsWith(comp));
		if (matchedComparator) {
			comparator = matchedComparator;
			numberValue = value.slice(matchedComparator.length);
		}
	}
	return {
		comparator,
		numberValue
	};
};
/**
* Example of values:
* - `<1`
* - `>-44.3`
* - `=999`
* - `<=1`
* - `>=1`
* - 88
* - `-1`
*
*/
const EnumKeyInput = ({ value, onChange }) => {
	const { comparator: initialComparator, numberValue: initialNumberValue } = useMemo(() => extractInitialState(value), [value]);
	const [comparator, setComparator] = useState(initialComparator ?? null);
	const [numberValue, setNumberValue] = useState(initialNumberValue ?? null);
	useEffect(() => {
		if (comparator && numberValue && (comparator !== initialComparator || numberValue !== initialNumberValue)) onChange(`${comparator}${numberValue}`);
	}, [
		comparator,
		initialComparator,
		initialNumberValue,
		numberValue,
		onChange
	]);
	return /* @__PURE__ */ jsxs("div", {
		className: "flex gap-1",
		children: [/* @__PURE__ */ jsxs(Select, {
			onValueChange: setComparator,
			defaultValue: initialComparator,
			children: [/* @__PURE__ */ jsx(Select.Trigger, {
				className: "w-20",
				children: /* @__PURE__ */ jsx(Select.Value, { placeholder: "Select a comparator" })
			}), /* @__PURE__ */ jsxs(Select.Content, { children: [
				/* @__PURE__ */ jsx(Select.Item, {
					value: "<",
					children: `<`
				}),
				/* @__PURE__ */ jsx(Select.Item, {
					value: "<=",
					children: `<=`
				}),
				/* @__PURE__ */ jsx(Select.Item, {
					value: "=",
					children: `=`
				}),
				/* @__PURE__ */ jsx(Select.Item, {
					value: ">=",
					children: `>=`
				}),
				/* @__PURE__ */ jsx(Select.Item, {
					value: ">",
					children: `>`
				})
			] })]
		}), /* @__PURE__ */ jsx(Input, {
			type: "number",
			"aria-label": "Quantity",
			defaultValue: numberValue ?? void 0,
			onChange: (e) => setNumberValue(e.target.value),
			className: "min-w-4"
		})]
	});
};

//#endregion
export { EnumKeyInput };
//# sourceMappingURL=EnumKeyInput.mjs.map