/** @license MIT | @cube-dev/ui-kit v0.123.0 | Cube Dev Team */
import { chain } from "./chain.js";
import { mergeIds } from "./useId.js";
import { mergeStyles } from "@tenphi/tasty";
import clsx from "clsx";

//#region src/utils/react/mergeProps.tsx
/**
* Merges multiple props objects together. Event handlers are chained,
* classNames are combined, and ids are deduplicated - different ids
* will trigger a side-effect and re-render components hooked up with `useId`.
* For all other props, the last prop object overrides all previous ones.
* @param args - Multiple sets of props to merge together.
*/
function mergeProps(...args) {
	let result = {};
	for (let props of args) {
		for (let key in result) if (/^on[A-Z]/.test(key) && typeof result[key] === "function" && props && typeof props[key] === "function") result[key] = chain(result[key], props[key]);
		else if (key === "className" && typeof result.className === "string" && props && typeof props.className === "string") result[key] = clsx(result.className, props.className);
		else if ((key === "styles" || key.endsWith("Styles")) && typeof result.styles === "object" && props && typeof props.styles === "object") result[key] = mergeStyles(result[key], props[key]);
		else if (key === "mods" && result.mods && props?.mods) result.mods = {
			...result.mods,
			...props?.mods
		};
		else if (key === "id" && result.id && props?.id) result.id = mergeIds(result.id, props?.id);
		else result[key] = props && props[key] !== void 0 ? props[key] : result[key];
		for (let key in props) if (result[key] === void 0) result[key] = props[key];
	}
	return result;
}

//#endregion
export { mergeProps };
//# sourceMappingURL=mergeProps.js.map