const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useSharedState = require("../../../../../../hooks/useSharedState.js");
const require_asyncToGenerator = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_model = require("../../../../model.js");
const require_index = require("../../../fields/index.js");
const require_index$1 = require("../index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/table/Table/tableConfig/body/CellProvider/index.tsx
const CellProvider = (props) => {
	var _getFieldByType, _getFieldByType2;
	const { title, editable, children, dataIndex, field_type, record, handleSave, render } = props;
	const { __is_group } = record || {};
	if (render || __is_group) return children;
	const [editing, setEditing] = (0, react.useState)(false);
	const inputRef = (0, react.useRef)(null);
	const form = require_useSharedState.default(require_index$1.EditableContext);
	const { state, dispatch } = require_useSharedState.default(require_model.Context);
	const KEY = `${dataIndex}-${record === null || record === void 0 ? void 0 : record.key}`;
	(0, react.useEffect)(() => {
		if (editing) {
			var _inputRef$current, _inputRef$current$foc;
			(_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || (_inputRef$current$foc = _inputRef$current.focus) === null || _inputRef$current$foc === void 0 || _inputRef$current$foc.call(_inputRef$current);
		}
	}, [editing]);
	const toggleEdit = () => {
		if (!require_index.fieldMaps[field_type]) return;
		setEditing(!editing);
		form.setFieldsValue({ [dataIndex]: record[dataIndex] });
	};
	const save = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			try {
				const values = yield form.validateFields();
				toggleEdit();
				handleSave(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, record), values));
			} catch (errInfo) {
				console.log("Save failed:", errInfo);
			}
		});
		return function save() {
			return _ref.apply(this, arguments);
		};
	}();
	let childNode = children;
	const Com = (_getFieldByType = require_index.getFieldByType(field_type)) === null || _getFieldByType === void 0 ? void 0 : _getFieldByType.show;
	const EditCom = (_getFieldByType2 = require_index.getFieldByType(field_type)) === null || _getFieldByType2 === void 0 ? void 0 : _getFieldByType2.edit;
	const editComRender = (0, react.useMemo)(() => {
		const NewEditCom = EditCom || antd.Input;
		return /* @__PURE__ */ react.default.createElement(NewEditCom, {
			ref: inputRef,
			onBlur: save,
			style: { height: "100%" },
			bordered: false
		});
	}, [EditCom, save]);
	if (editable) childNode = editing ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		style: { margin: 0 },
		name: dataIndex,
		rules: [{
			required: true,
			message: `${title} is required.`
		}]
	}, editComRender) : /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("editable-cell-value-wrap") }, Com ? /* @__PURE__ */ react.default.createElement(Com, props) : children);
	const focus = (0, react.useMemo)(() => {
		return state.currentCell === KEY;
	}, [KEY, state.currentCell]);
	(0, react.useEffect)(() => {
		if (!focus) setEditing(false);
	}, [focus]);
	const onFocus = () => {
		if (!state.setting.data.edit) return;
		if (record) if (!focus) dispatch({
			type: "setCurrentCell",
			payload: focus ? null : KEY
		});
		else toggleEdit();
	};
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("cell-provider", { ["focus"]: focus }),
		onClick: onFocus
	}, childNode);
};
//#endregion
exports.default = CellProvider;
