import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getFieldComponent } from "../../fields/index.js";
import React, { useMemo } from "react";
//#region src/components/dataSourceComponents/dataSourceTable/hooks/useColumns.tsx
const genFieldProps = (field, fieldProps) => {
	let props = {
		renderMode: "tableView",
		noStyle: true
	};
	if (field && [
		"Select",
		"Radio.Group",
		"Checkbox.Group"
	].includes(field.uiSchema["x-component"]) && field.uiSchema.enum.length > 0) {
		if (["Radio.Group", "Checkbox.Group"].includes(field.uiSchema["x-component"])) props.direction = "horizontal";
		props.options = field.uiSchema.enum;
	}
	props = _objectSpread2(_objectSpread2({}, props), fieldProps || {});
	return props;
};
const useColumns = ({ columns, fields, actionsColumns }) => {
	const fieldMap = useMemo(() => {
		if (!fields) return {};
		return fields.reduce((acc, field) => {
			acc[field.name] = field;
			return acc;
		}, {});
	}, [fields]);
	return useMemo(() => {
		if (Object.keys(fieldMap).length === 0) return [];
		return columns.map((column) => {
			var _other;
			const field = fieldMap[column === null || column === void 0 ? void 0 : column.dataIndex];
			const title = (column === null || column === void 0 ? void 0 : column.title) || (field === null || field === void 0 ? void 0 : field.title);
			const FieldComponent = getFieldComponent(column === null || column === void 0 ? void 0 : column.fieldComponent) || "div";
			const fieldProps = genFieldProps(field, (_other = column.other) === null || _other === void 0 ? void 0 : _other.fieldProps);
			return _objectSpread2(_objectSpread2({}, column), {}, {
				title,
				render: (text, record, index) => {
					if (column === null || column === void 0 ? void 0 : column.render) return column.render(text, record, index);
					return /* @__PURE__ */ React.createElement(FieldComponent, _objectSpread2(_objectSpread2({}, fieldProps), {}, { value: text }));
				}
			});
		}).concat(actionsColumns || []);
	}, [
		columns,
		fieldMap,
		actionsColumns
	]);
};
//#endregion
export { useColumns as default };
