const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../../locales/index.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index$1 = require("../../../pisellModal/index.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useDataSource = require("../../hooks/useDataSource.js");
const require_index$2 = require("../../../pisellToast/index.js");
const require_useVariables = require("../../hooks/useVariables.js");
const require_useComponentId = require("../../hooks/useComponentId.js");
const require_index$3 = require("../filter/index.js");
const require_usePagination = require("./usePagination.js");
const require_useColumns = require("./useColumns.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let ahooks = require("ahooks");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
//#region src/components/dataSourceComponents/dataSourceTable/hooks/useTableProps.tsx
/**
* 处理table的props
* 转换title
* @param props
* @returns
*/
const useTableProps = (props) => {
	var _originProps$dataSour;
	const { originProps, openDrawer, closeDrawer, refreshData, handleFilterChange, tableRef, getInitialValues } = props;
	const { title: propsTitle, subTitle: propsSubTitle, titleButtons, tableButtons, operation, filter: propsFilter, search, clickToDetail, onRow: propsOnRow, filterBy, onDeleteFinish, currentSettingKey, __id } = originProps;
	const { items, show, title: operationTitle, width, align, fixed, type } = operation || {};
	const componentId = require_useComponentId.default(originProps);
	const { list, destroy, update } = require_useDataSource.default();
	const { data } = list;
	const pagination = require_usePagination.default({ data });
	(0, react.useEffect)(() => {
		if (tableRef === null || tableRef === void 0 ? void 0 : tableRef.current) tableRef.current.formInstance.setFieldValue("pagination", {
			page: (pagination === null || pagination === void 0 ? void 0 : pagination.current) || 1,
			size: (pagination === null || pagination === void 0 ? void 0 : pagination.pageSize) || 10
		});
	}, [pagination]);
	const { registerValueVariable } = require_useVariables.default();
	const title = (0, react.useMemo)(() => {
		if ((0, _pisell_utils.isString)(propsTitle)) return /* @__PURE__ */ react.default.createElement("span", { className: "data-source-table-title" }, propsTitle);
		return propsTitle;
	}, [propsTitle]);
	const subTitle = (0, react.useMemo)(() => {
		if ((0, _pisell_utils.isString)(propsSubTitle)) return /* @__PURE__ */ react.default.createElement("span", { className: "data-source-table-sub-title" }, propsSubTitle);
		return propsSubTitle;
	}, [propsSubTitle]);
	const handleOpenDrawer = (0, ahooks.useMemoizedFn)((record, item) => {
		var _item$onClick;
		item === null || item === void 0 || (_item$onClick = item.onClick) === null || _item$onClick === void 0 || _item$onClick.call(item, record);
		if (item.openMode === "custom") return;
		registerValueVariable === null || registerValueVariable === void 0 || registerValueVariable(componentId, { currentRecord: record });
		openDrawer(item);
	});
	const handleDelete = (0, ahooks.useMemoizedFn)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (record, softDelete, customDeleteAction) {
			try {
				registerValueVariable === null || registerValueVariable === void 0 || registerValueVariable(componentId, { currentRecord: require_objectSpread2._objectSpread2({}, record) });
				setTimeout(require_asyncToGenerator._asyncToGenerator(function* () {
					if (customDeleteAction) yield customDeleteAction(record);
					else if (softDelete) {
						const deleteTime = (0, dayjs.default)().format("YYYY-MM-DD HH:mm:ss");
						yield update === null || update === void 0 ? void 0 : update.runAsync(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, record), {}, { deleted_at: deleteTime }));
					} else yield destroy === null || destroy === void 0 ? void 0 : destroy.runAsync(record.id);
					onDeleteFinish === null || onDeleteFinish === void 0 || onDeleteFinish(record);
					refreshData();
					require_index$2.default({ content: require_index.getText("pisell-action-delete-success") });
				}));
			} catch (error) {
				console.error(error);
				require_index$2.default({ content: require_index.getText("pisell-action-delete-failed") });
			}
		});
		return function(_x, _x2, _x3) {
			return _ref.apply(this, arguments);
		};
	}());
	const actionsColumns = (0, react.useMemo)(() => {
		if (!show) return [];
		return [{
			title: operationTitle,
			key: "actions",
			align,
			width,
			fixed,
			items,
			type,
			render: (_, record) => {
				return /* @__PURE__ */ react.default.createElement(antd.Space, null, items === null || items === void 0 ? void 0 : items.map((item) => {
					const { label, actionType, show } = item;
					const handleClick = (e) => {
						e.stopPropagation();
						if (actionType === "delete") {
							require_index$1.default.confirm({
								title: require_index.getText("pisell-delete-confirm-title"),
								content: require_index.getText("pisell-delete-confirm-content"),
								okText: require_index.getText("pisell-delete-confirm-ok"),
								cancelText: require_index.getText("pisell-delete-confirm-cancel"),
								onOk: () => {
									handleDelete(record, item === null || item === void 0 ? void 0 : item.softDelete, item === null || item === void 0 ? void 0 : item.customDeleteAction);
								}
							});
							return;
						} else handleOpenDrawer(record, item);
					};
					if ((0, _pisell_utils.isBoolean)(show)) {
						if (!show) return null;
					} else if ((0, _pisell_utils.isFunction)(show)) {
						if (!show(record)) return null;
					}
					return /* @__PURE__ */ react.default.createElement(antd.Button, {
						onClick: handleClick,
						type: type === "link" ? "link" : "default",
						style: type === "link" ? {
							padding: "0",
							height: "auto"
						} : { height: "auto" }
					}, label);
				}));
			}
		}];
	}, [
		operationTitle,
		show,
		width,
		align,
		fixed,
		type,
		items
	]);
	const columns = require_useColumns.default({
		columns: originProps.columns,
		fields: (_originProps$dataSour = originProps.dataSource) === null || _originProps$dataSour === void 0 ? void 0 : _originProps$dataSour.fields,
		actionsColumns
	});
	const buttons = (0, react.useMemo)(() => {
		var _titleButtons$items;
		if (!(titleButtons === null || titleButtons === void 0 ? void 0 : titleButtons.show)) return null;
		return titleButtons === null || titleButtons === void 0 || (_titleButtons$items = titleButtons.items) === null || _titleButtons$items === void 0 ? void 0 : _titleButtons$items.map((button) => {
			const { actionType, buttonProps, beforeOpen } = button;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, button), buttonProps), {}, {
				title: button.label,
				size: "large",
				onClick: () => {
					if (actionType === "add") {
						let result = true;
						if (beforeOpen) result = beforeOpen();
						if (result) handleOpenDrawer(null, button);
					}
				}
			});
		});
	}, [titleButtons]);
	const actionButtons = (0, react.useMemo)(() => {
		var _tableButtons$items;
		if (!(tableButtons === null || tableButtons === void 0 ? void 0 : tableButtons.show)) return null;
		return tableButtons === null || tableButtons === void 0 || (_tableButtons$items = tableButtons.items) === null || _tableButtons$items === void 0 ? void 0 : _tableButtons$items.map((button) => {
			const { actionType, buttonProps } = button;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, button), buttonProps), {}, {
				size: "large",
				title: button.label,
				onClick: () => {
					if (actionType === "add") handleOpenDrawer(null, button);
				}
			});
		});
	}, [tableButtons]);
	const filter = (0, react.useMemo)(() => {
		if (!(propsFilter === null || propsFilter === void 0 ? void 0 : propsFilter.show) && !(search === null || search === void 0 ? void 0 : search.show)) return null;
		return {
			dom: /* @__PURE__ */ react.default.createElement(require_index$3.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, propsFilter), {}, {
				componentId,
				search,
				handleValuesChange: handleFilterChange,
				filterBy,
				getInitialValues
			})),
			items: propsFilter === null || propsFilter === void 0 ? void 0 : propsFilter.items
		};
	}, [propsFilter, search]);
	const onRow = (0, ahooks.useMemoizedFn)((record) => {
		if (!(clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.show)) return propsOnRow;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, propsOnRow), {}, { onClick: (event) => {
			var _propsOnRow$onClick;
			let detail;
			if (clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.useCustomAction) detail = {
				openMode: clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.openMode,
				openContentSize: clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.openContentSize,
				openTitle: clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.openTitle,
				actionType: clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.actionType,
				key: clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.actionType
			};
			else detail = items === null || items === void 0 ? void 0 : items.find((item) => item.key === (clickToDetail === null || clickToDetail === void 0 ? void 0 : clickToDetail.key));
			if (detail) handleOpenDrawer(record, detail);
			propsOnRow === null || propsOnRow === void 0 || (_propsOnRow$onClick = propsOnRow.onClick) === null || _propsOnRow$onClick === void 0 || _propsOnRow$onClick.call(propsOnRow, record, event);
		} });
	});
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, originProps), {}, {
		currentComponentId: componentId,
		title,
		pagination,
		columns,
		subTitle,
		buttons,
		filter,
		onRow,
		actionButtons
	});
};
//#endregion
exports.default = useTableProps;
