import { getText } from "../../../locales/index.js";
import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getValueByKeys } from "../Table/fields/utils/index.js";
import getColumnNumberProps from "../TableFilter/numberFilter.js";
import getColumnStringProps from "../TableFilter/stringFilter.js";
import getColumnSelectProps from "../TableFilter/selectFilter.js";
import SortIcon from "../TableFilter/SortIcon.js";
import React, { useCallback, useMemo } from "react";
import { isBoolean } from "@pisell/utils";
import { Form } from "antd";
//#region src/components/table/hooks/useTransColumns.tsx
const genOnCell = ({ bordered, position }) => (_) => {
	let styleObj = {};
	let notGroupStyle = {};
	const borderStr = bordered ? "1px solid #EAECF0" : "";
	if (position === "start") {
		styleObj = {
			borderLeft: borderStr,
			borderRadius: "8px 0px 0px 0px",
			padding: "12px"
		};
		notGroupStyle = { borderLeft: borderStr };
	} else if (position === "end") {
		styleObj = {
			borderRight: borderStr,
			borderRadius: "0px 8px 0px 0px"
		};
		notGroupStyle = { borderRight: borderStr };
	}
	return { style: _.__is_group ? _objectSpread2({ borderTop: "1px solid var(--Gray-200, #EAECF0)" }, styleObj) : _objectSpread2({}, notGroupStyle) };
};
const useTransColumns = (params) => {
	var _group$;
	const { columns, form, handleSave, handleResize, dragSort, bordered } = params;
	const group = Form.useWatch("group_by", {
		form,
		preserve: true
	});
	const groupField = group === null || group === void 0 || (_group$ = group[0]) === null || _group$ === void 0 ? void 0 : _group$.field;
	const groupFieldRender = useCallback((text, record) => {
		if (!groupField) return void 0;
		const { children } = record;
		const { render, dataIndex } = columns.find((item) => `${item.dataIndex}` === groupField) || {};
		let dom = /* @__PURE__ */ React.createElement("div", null, getValueByKeys(children === null || children === void 0 ? void 0 : children[0], dataIndex || groupField.split(",")));
		if (render) dom = render(getValueByKeys(children === null || children === void 0 ? void 0 : children[0], dataIndex), record.children[0]);
		return /* @__PURE__ */ React.createElement("div", { style: {
			display: "flex",
			justifyContent: "space-between",
			alignItems: "start"
		} }, /* @__PURE__ */ React.createElement("span", null, dom), /* @__PURE__ */ React.createElement("span", null, children.length, " ", getText("table-column-group-records")));
	}, [groupField, columns]);
	return { columns: useMemo(() => {
		var _newColumns$;
		let newColumns = columns.filter((item) => !(isBoolean(item.isShow) && !item.isShow));
		if (dragSort && !groupField && (newColumns === null || newColumns === void 0 || (_newColumns$ = newColumns[0]) === null || _newColumns$ === void 0 ? void 0 : _newColumns$.key) !== "__sort") newColumns.unshift({
			key: "__sort",
			width: 60,
			align: "center",
			className: "drag-sort-cell"
		});
		return newColumns.map((col, index) => {
			var _col$filters;
			let position = "";
			if (index === 0) position = "start";
			else if (index === newColumns.length - 1) position = "end";
			let groupObj = {};
			if (groupField) groupObj = { render: (text, record) => {
				var _col$render;
				if (record.__is_group) return null;
				return (col === null || col === void 0 || (_col$render = col.render) === null || _col$render === void 0 ? void 0 : _col$render.call(col, text, record)) || text;
			} };
			if (position === "start" && groupField) groupObj = {
				fixed: "left",
				width: 350,
				render: (text, record) => {
					var _col$render2;
					if (record.__is_group && groupFieldRender) return groupFieldRender(text, record);
					return (col === null || col === void 0 || (_col$render2 = col.render) === null || _col$render2 === void 0 ? void 0 : _col$render2.call(col, text, record)) || text;
				}
			};
			const item = {
				editable: col.editable || true,
				dataIndex: col.dataIndex,
				title: col.title,
				field_type: col.field_type,
				lock: col.lock,
				width: col.width || 250,
				action: col.action || "",
				render: col.render,
				contentClassName: col.contentClassName || ""
			};
			let filterObj = {};
			if (col.defaultFilters) {
				if (col.field_type === "number") filterObj = getColumnNumberProps({ dataIndex: col.dataIndex });
				if (col.field_type === "text") filterObj = getColumnStringProps({ dataIndex: col.dataIndex });
			}
			if (((_col$filters = col.filters) === null || _col$filters === void 0 ? void 0 : _col$filters.length) > 0) filterObj = _objectSpread2(_objectSpread2({}, getColumnSelectProps({
				dataIndex: col.dataIndex,
				filters: col.filters
			})), {}, { filters: col.filters });
			return _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, col), filterObj), groupObj), {}, {
				sortIcon: (props) => /* @__PURE__ */ React.createElement(SortIcon, props),
				onCell: (record) => _objectSpread2(_objectSpread2(_objectSpread2({ record }, item), genOnCell({
					position,
					bordered
				})(record)), {}, { handleSave }),
				onHeaderCell: (record) => _objectSpread2(_objectSpread2({ record }, item), {}, {
					onResize: handleResize === null || handleResize === void 0 ? void 0 : handleResize(index, record),
					position
				})
			});
		});
	}, [
		columns,
		groupFieldRender,
		dragSort,
		groupField,
		bordered
	]) };
};
//#endregion
export { useTransColumns as default };
