import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import Form from "../../form/index.js";
import useDataSource from "../hooks/useDataSource.js";
import useFormSetting from "./provider/hooks/useFormSetting.js";
import useJsonPrefixPath from "./provider/hooks/useJsonPrefixPath.js";
import { toArr } from "./utils.js";
import React, { cloneElement, isValidElement, useMemo } from "react";
import { isArr } from "@pisell/utils";
//#region src/components/dataSourceComponents/dataSourceForm/formItem.tsx
const _excluded = [
	"children",
	"name",
	"label",
	"mode"
];
const FormItem = (props) => {
	var _field$uiSchema;
	const { children, name, label: propsLabel, mode: propsMode } = props, other = _objectWithoutProperties(props, _excluded);
	const { path: prefixPath = [] } = useJsonPrefixPath();
	const { dataSource } = useDataSource();
	const fields = dataSource === null || dataSource === void 0 ? void 0 : dataSource.fields;
	const { renderMode: globalMode } = useFormSetting();
	const field = fields === null || fields === void 0 ? void 0 : fields.find((item) => item.name === name);
	const label = propsLabel || (field === null || field === void 0 || (_field$uiSchema = field.uiSchema) === null || _field$uiSchema === void 0 ? void 0 : _field$uiSchema.title) || name;
	const effectiveMode = propsMode || globalMode;
	const renderChildren = useMemo(() => {
		if (isArr(children) && children.every(isValidElement)) return children.map((c) => cloneElement(c, { renderMode: effectiveMode }));
		return children;
	}, [children, effectiveMode]);
	const concatName = name !== void 0 ? [...prefixPath, ...toArr(name)] : void 0;
	return /* @__PURE__ */ React.createElement(Form.Item, _objectSpread2({
		name: concatName,
		label
	}, other), renderChildren);
};
//#endregion
export { FormItem as default };
