import { rt as useFormContext } from "./connect-MjNGHHUz-BfDIyhte.js";
import React, { useEffect } from "react";
//#region node_modules/@powerhousedao/document-engineering/dist/src/scalars/lib/deep-equal.js
function deepEqual(a, b, visited = /* @__PURE__ */ new Map()) {
	if (a === b) return true;
	if (a === null || b === null || a === void 0 || b === void 0) return false;
	if (typeof a !== typeof b) return false;
	if (Array.isArray(a) && Array.isArray(b)) {
		if (visited.get(a) === b) return true;
		visited.set(a, b);
		return a.length === b.length && a.every((value, index) => deepEqual(value, b[index], visited));
	}
	if (typeof a === "object" && typeof b === "object" && !Array.isArray(a) && !Array.isArray(b)) {
		if (visited.get(a) === b) return true;
		visited.set(a, b);
		const aKeys = Object.keys(a);
		const bKeys = Object.keys(b);
		return aKeys.length === bKeys.length && aKeys.every((key) => {
			return key in b && deepEqual(a[key], b[key], visited);
		});
	}
	return false;
}
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/scalars/lib/shared-value-transformers.js
const sharedValueTransformers = {
	trimOnBlur: (ifParam = true) => ({
		transformer: (value) => value?.trim(),
		options: {
			trigger: "blur",
			if: ifParam
		}
	}),
	lowercaseOnChange: (ifParam = true) => ({
		transformer: (value) => value?.toLowerCase(),
		options: {
			trigger: "change",
			if: ifParam
		}
	}),
	uppercaseOnChange: (ifParam = true) => ({
		transformer: (value) => value?.toUpperCase(),
		options: {
			trigger: "change",
			if: ifParam
		}
	}),
	trimOnEnter: (ifParam = true) => ({
		transformer: (value) => value?.trim(),
		options: {
			trigger: "keyDown",
			if: ifParam
		}
	})
};
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/scalars/components/fragments/value-transformer/value-transformer.js
function setNativeValue(element, value) {
	const valueSetter = Object.getOwnPropertyDescriptor(element, "value")?.set;
	const prototype = Object.getPrototypeOf(element);
	const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, "value")?.set;
	if (valueSetter && valueSetter !== prototypeValueSetter) prototypeValueSetter?.call(element, value);
	else valueSetter?.call(element, value);
}
function _applyTransformers(transformers, value, filter = "blur") {
	return transformers.reduce((value, transformer) => {
		if (typeof transformer === "function") {
			if (filter === "blur" || filter === "all") return transformer(value);
			return value;
		}
		if ((transformer.options?.trigger ?? "blur") !== filter && filter !== "all") return value;
		if (transformer.options?.if === void 0 || transformer.options.if) return transformer.transformer(value);
		return value;
	}, value);
}
/**
* Apply transformers to the value of the input element. A transformer is a function that
* will be applied to the value of the input element modifying the value.
*
* @param transformers - An array of transformers. A transformer is an object with a
* `transformer` property, which is a function that will be applied to the value of
* the input element, and an optional `options` property. The `options` property can
* have a `trigger` property, which is the event that triggers the transformer, and
* an `if` property, which is a boolean that indicates if the transformer should be
* applied.
* @param children - The input element to apply the transformers to as children
*
* @example
* <ValueTransformer transformers={[
*   {
*     transformer: (value) => value.toUpperCase(),
*     options: {
*       trigger: "change",
*     },
*   },
* ]}>
*   <Input />
* </ValueTransformer>
*
* @example
* <ValueTransformer transformers={[
*   {
*     // trim the value on blur
*     transformer: (value) => (value as string).trim(),
*   },
* ]}>
*   <Input />
* </ValueTransformer>
*/
function ValueTransformer({ transformers, children }) {
	const setValue = useFormContext()?.setValue;
	useEffect(() => {
		const value = children.props.value;
		const transformedValue = _applyTransformers(transformers, value, "all");
		if (!deepEqual(transformedValue, value)) if (typeof setValue === "function") setValue(children.props.name, transformedValue);
		else children.props.onChange?.({ target: { value: transformedValue } });
	}, [transformers]);
	return React.cloneElement(children, {
		...children.props,
		onChange: (event) => {
			const transformedValue = _applyTransformers(transformers, event.target.value, "change");
			if (transformedValue !== event.target.value) {
				setValue?.(children.props.name, transformedValue);
				setNativeValue(event.target, transformedValue);
			}
			children.props.onChange?.(event);
		},
		onBlur: (event) => {
			children.props.onBlur?.(event);
			const target = event.target;
			const transformedValue = _applyTransformers(transformers, target.value, "blur");
			if (!deepEqual(transformedValue, target.value)) {
				setNativeValue(target, transformedValue);
				const changeEvent = new Event("change", { bubbles: true });
				target.dispatchEvent(changeEvent);
			}
		},
		onKeyDown: (event) => {
			children.props.onKeyDown?.(event);
			if (event.key === "Enter") {
				const target = event.target;
				const transformedValue = _applyTransformers(transformers, target.value, "keyDown");
				if (!deepEqual(transformedValue, target.value)) {
					setNativeValue(target, transformedValue);
					const changeEvent = new Event("change", { bubbles: true });
					target.dispatchEvent(changeEvent);
				}
			}
		}
	});
}
//#endregion
export { sharedValueTransformers as n, deepEqual as r, ValueTransformer as t };

//# sourceMappingURL=value-transformer-BJ7Rs-2f.js.map