/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-1fc5ad1 | Cube Dev Team */
import { mergeProps } from "../../../utils/react/mergeProps.js";
import { warn } from "../../../utils/warnings.js";
import { FieldWrapper } from "../FieldWrapper/FieldWrapper.js";
import { useFormProps } from "./Form.js";
import { useField } from "./use-field/use-field.js";
import { Children, cloneElement, createContext, useContext, useMemo } from "react";
import { jsx } from "react/jsx-runtime";

//#region src/components/form/Form/Field.tsx
function getDefaultValidateTrigger(type) {
	type = type || "";
	return type === "Number" || type.includes("Text") ? "onBlur" : "onChange";
}
function getValueProps(type, value, onChange, allowsCustomValue) {
	type = type || "";
	if (type === "Number") return {
		value: value != null ? value : null,
		onChange
	};
	else if (type === "Text") return {
		value: typeof value === "string" || typeof value === "number" ? String(value) : "",
		onChange
	};
	else if (type === "Checkbox") return {
		isSelected: value != null ? value : false,
		isIndeterminate: false,
		onChange
	};
	else if (type === "CheckboxGroup") return {
		value: value != null ? value : [],
		onChange
	};
	else if (type === "RangeSlider") return {
		value: value != null ? value : void 0,
		onChange
	};
	else if (type === "ComboBox") return {
		selectedKey: !allowsCustomValue ? value ?? null : void 0,
		inputValue: allowsCustomValue ? value ?? "" : void 0,
		onInputChange(val) {
			if (!allowsCustomValue) return;
			onChange(val);
		},
		onSelectionChange(val) {
			if (val == null && allowsCustomValue) return;
			onChange(val);
		}
	};
	else if (type === "Select") return {
		selectedKey: value ?? null,
		onSelectionChange: onChange
	};
	return {
		value: value != null ? value : null,
		onChange
	};
}
function Field(props) {
	const allProps = useFormProps(props);
	let { children, form, label, extra, necessityLabel, necessityIndicator, tooltip, isHidden, isDisabled, isLoading, styles, labelProps, labelPosition = "top", labelStyles, labelSuffix } = allProps;
	if (typeof children === "function") children = children(form);
	let child = children == null ? null : Children.only(children);
	const inputType = (child?.type)?.cubeInputType ?? "Text";
	const __props = useField(allProps, { defaultValidationTrigger: getDefaultValidateTrigger(inputType) });
	const { validationState, message, description, errorMessage, isRequired, name, id } = __props;
	if (!child) return null;
	if (id) {
		if (!labelProps) labelProps = {};
		labelProps.for = id;
	}
	if (__props.nonInput) return /* @__PURE__ */ jsx(LegacyFieldProvider, { children: /* @__PURE__ */ jsx(FieldWrapper, {
		isHidden,
		isDisabled,
		validationState: __props.validationState,
		necessityIndicator,
		necessityLabel,
		isRequired,
		label,
		labelProps,
		extra,
		tooltip,
		message,
		description,
		errorMessage,
		Component: child,
		styles,
		labelPosition,
		labelStyles,
		labelSuffix
	}) });
	if (!form) {
		warn("Form Field requires declared form instance if field name is specified");
		return null;
	}
	if (!__props.field) return /* @__PURE__ */ jsx(LegacyFieldProvider, { children: cloneElement(child, mergeProps(child.props, {
		...getValueProps(inputType),
		label: name,
		name,
		id
	})) });
	const newProps = {
		id,
		name,
		onBlur: __props.onBlur
	};
	if (necessityIndicator != null) newProps.necessityIndicator = necessityIndicator;
	if (necessityLabel) newProps.necessityLabel = necessityLabel;
	if (validationState) newProps.validationState = validationState;
	if (isRequired) newProps.isRequired = isRequired;
	if (label) newProps.label = label;
	if (extra) newProps.extra = extra;
	if (tooltip) newProps.tooltip = tooltip;
	if (message !== void 0) newProps.message = message;
	if (errorMessage !== void 0) newProps.errorMessage = errorMessage;
	if (description !== void 0) newProps.description = description;
	if (isHidden != null) newProps.isHidden = isHidden;
	if (isDisabled != null) newProps.isDisabled = isDisabled;
	if (isLoading != null) newProps.isLoading = isLoading;
	Object.assign(newProps, getValueProps(inputType, __props.field?.value, __props.onChange, child.props.allowsCustomValue));
	const { onChange, onSelectionChange, ...childProps } = child.props;
	return /* @__PURE__ */ jsx(LegacyFieldProvider, { children: cloneElement(child, mergeProps(childProps, newProps, onChange ? { onChange: __props.onChange } : {}, onSelectionChange ? { onSelectionChange: __props.onChange } : {})) });
}
const LegacyFieldContext = createContext({ insideLegacyField: false });
function LegacyFieldProvider(props) {
	const value = useMemo(() => ({ insideLegacyField: true }), []);
	return /* @__PURE__ */ jsx(LegacyFieldContext.Provider, {
		value,
		children: props.children
	});
}
function useInsideLegacyField() {
	const { insideLegacyField = false } = useContext(LegacyFieldContext);
	return insideLegacyField;
}

//#endregion
export { Field, useInsideLegacyField };
//# sourceMappingURL=Field.js.map