/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-af8d194 | Cube Dev Team */
import { mergeProps } from "../../../../utils/react/mergeProps.js";
import { useEvent } from "../../../../_internal/hooks/use-event.js";
import { warn } from "../../../../utils/warnings.js";
import { useChainedCallback } from "../../../../_internal/hooks/use-chained-callback.js";
import { useField } from "./use-field.js";
import { useInsideLegacyField } from "../Field.js";
import { useDebugValue, useId, useRef } from "react";

//#region src/components/form/Form/use-field/use-field-props.tsx
function useFieldProps(props, params = {}) {
	const isDisabledRef = useRef(params.unsafe__isDisabled ?? false);
	const { valuePropsMapper = ({ value, onChange }) => {
		return {
			value: value ?? null,
			onChange
		};
	}, defaultValidationTrigger = "onBlur" } = params;
	const isInsideLegacyField = useInsideLegacyField();
	if (props.rules && !props.name) warn(`The "rules" prop is not suitable for fields that are not part of a form. Use "name" prop to link the field to a form.`);
	if (isInsideLegacyField || isDisabledRef.current === true) return props;
	const hasName = props.name != null;
	const generatedId = useId();
	if (!hasName) {
		if (!props.id) {
			const result = {
				...props,
				id: generatedId
			};
			if (result.id && !result.labelProps) result.labelProps = { for: result.id };
			else if (result.id && result.labelProps && !result.labelProps.for) result.labelProps = {
				...result.labelProps,
				for: result.id
			};
			return result;
		}
		return props;
	}
	const field = useField(props, { defaultValidationTrigger: params.defaultValidationTrigger });
	const isOutsideOfForm = field?.form == null;
	if (props.rules && isOutsideOfForm) warn(`The "rules" prop is not supported for fields that are not part of a form. The "${props.name}" field is placed outside the form.`);
	const onBlurChained = useChainedCallback(field?.onBlur, "onBlur" in props ? props.onBlur : void 0);
	const onChangeEvent = useEvent((value, dontTouch) => {
		field?.onChange?.(value, dontTouch, field?.validateTrigger ?? defaultValidationTrigger);
	});
	const valueProps = !isOutsideOfForm ? valuePropsMapper({
		value: field.value,
		onChange: onChangeEvent
	}) : {};
	if (isInsideLegacyField && !isOutsideOfForm) {
		const valuePropEventNames = !isOutsideOfForm ? Object.keys(valueProps).filter((name) => name.startsWith("on")) : [];
		for (const valuePropName of valuePropEventNames) if (valuePropName in props) warn(`The "${valuePropName}" listener is not supported for input "${props.name}" that is linked to a form via a <Field> component. Remove the <Field> component and move its properties to the input itself.`);
	}
	const compiledErrorMessage = props.errorMessage !== void 0 ? props.errorMessage : field?.field?.status === "invalid" && field?.field?.errors?.length ? field.field.errors[0] : void 0;
	const { form: _form, ...fieldWithoutForm } = field ?? {};
	const result = isOutsideOfForm ? props : mergeProps(props, fieldWithoutForm, valueProps, {
		validateTrigger: field.validateTrigger ?? defaultValidationTrigger,
		onBlur: onBlurChained,
		errorMessage: compiledErrorMessage
	});
	if (result.id) {
		if (!result.labelProps) result.labelProps = {};
		result.labelProps.for = result.id;
	}
	useDebugValue(result);
	return result;
}

//#endregion
export { useFieldProps };
//# sourceMappingURL=use-field-props.js.map