import { _objectSpread2 } from "../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useSharedState from "../../../../../../hooks/useSharedState.js";
import { _asyncToGenerator } from "../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { Context } from "../../../../model.js";
import { fieldMaps, getFieldByType } from "../../../fields/index.js";
import { EditableContext } from "../index.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { Form, Input } from "antd";
import classNames from "classnames";
import "./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] = useState(false);
	const inputRef = useRef(null);
	const form = useSharedState(EditableContext);
	const { state, dispatch } = useSharedState(Context);
	const KEY = `${dataIndex}-${record === null || record === void 0 ? void 0 : record.key}`;
	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 (!fieldMaps[field_type]) return;
		setEditing(!editing);
		form.setFieldsValue({ [dataIndex]: record[dataIndex] });
	};
	const save = function() {
		var _ref = _asyncToGenerator(function* () {
			try {
				const values = yield form.validateFields();
				toggleEdit();
				handleSave(_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 = getFieldByType(field_type)) === null || _getFieldByType === void 0 ? void 0 : _getFieldByType.show;
	const EditCom = (_getFieldByType2 = getFieldByType(field_type)) === null || _getFieldByType2 === void 0 ? void 0 : _getFieldByType2.edit;
	const editComRender = useMemo(() => {
		const NewEditCom = EditCom || Input;
		return /* @__PURE__ */ React.createElement(NewEditCom, {
			ref: inputRef,
			onBlur: save,
			style: { height: "100%" },
			bordered: false
		});
	}, [EditCom, save]);
	if (editable) childNode = editing ? /* @__PURE__ */ React.createElement(Form.Item, {
		style: { margin: 0 },
		name: dataIndex,
		rules: [{
			required: true,
			message: `${title} is required.`
		}]
	}, editComRender) : /* @__PURE__ */ React.createElement("div", { className: classNames("editable-cell-value-wrap") }, Com ? /* @__PURE__ */ React.createElement(Com, props) : children);
	const focus = useMemo(() => {
		return state.currentCell === KEY;
	}, [KEY, state.currentCell]);
	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.createElement("div", {
		className: classNames("cell-provider", { ["focus"]: focus }),
		onClick: onFocus
	}, childNode);
};
//#endregion
export { CellProvider as default };
