const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_index = 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_useFormSetting = require("../provider/hooks/useFormSetting.js");
const require_index$1 = require("../../../pisellToast/index.js");
const require_serve = require("../serve.js");
const require_utils = require("../utils.js");
const require_useCtxActions = require("../../hooks/useCtxActions.js");
const require_useFormSettingParams = require("../../hooks/useFormSettingParams.js");
const require_formItem = require("../formItem.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
require("./index.less");
//#region src/components/dataSourceComponents/dataSourceForm/submitButton/index.tsx
const SubmitButton = (props) => {
	const { children, status, type, confirmConfig, toastConfig, refreshData, afterSubmitType, afterSubmitModalConfig, afterSubmitNotificationConfig, childrenString } = props;
	const [loading, setLoading] = (0, react.useState)(false);
	const [api, contextHolder] = antd.notification.useNotification();
	const { scope, target } = require_useFormSettingParams.default();
	const form = antd.Form.useFormInstance();
	const { renderMode, onDataSourceFinish, formatSubmitValues, customSubmit, beforeSubmit, scene, sceneRequestId } = require_useFormSetting.default();
	const { setVisible, refreshTableData } = require_useCtxActions.default();
	const { create, update, dataSource } = require_useDataSource.default();
	const isDisabled = status === "disabled";
	const onSubmit = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (values) {
			setLoading(true);
			try {
				if (dataSource === null || dataSource === void 0 ? void 0 : dataSource.fromDeviceSettingMeta) return yield require_serve.updateDeviceSetting({
					scene,
					sceneRequestId: sceneRequestId || "",
					data: values
				});
				if (dataSource === null || dataSource === void 0 ? void 0 : dataSource.fromSettingMeta) return yield require_serve.updateTenantSetting({
					scope,
					target,
					data: values
				});
				if (customSubmit) return yield customSubmit(values);
				if (renderMode === "edit") return yield update.runAsync(values);
				else if (renderMode === "add") {
					const res = yield create.runAsync(values);
					form === null || form === void 0 || form.resetFields();
					return res;
				}
			} finally {
				setLoading(false);
			}
		});
		return function onSubmit(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const afterSubmitFuncMap = {
		close: () => {
			form.resetFields();
			setVisible === null || setVisible === void 0 || setVisible(false);
		},
		showModal: () => {
			require_index.default.success({
				title: afterSubmitModalConfig === null || afterSubmitModalConfig === void 0 ? void 0 : afterSubmitModalConfig.title,
				content: afterSubmitModalConfig === null || afterSubmitModalConfig === void 0 ? void 0 : afterSubmitModalConfig.content,
				okText: afterSubmitModalConfig === null || afterSubmitModalConfig === void 0 ? void 0 : afterSubmitModalConfig.okText,
				onOk: afterSubmitModalConfig === null || afterSubmitModalConfig === void 0 ? void 0 : afterSubmitModalConfig.onOk
			});
		},
		showNotification: () => {
			api.open({
				message: afterSubmitNotificationConfig === null || afterSubmitNotificationConfig === void 0 ? void 0 : afterSubmitNotificationConfig.title,
				description: afterSubmitNotificationConfig === null || afterSubmitNotificationConfig === void 0 ? void 0 : afterSubmitNotificationConfig.content,
				duration: afterSubmitNotificationConfig === null || afterSubmitNotificationConfig === void 0 ? void 0 : afterSubmitNotificationConfig.duration,
				placement: afterSubmitNotificationConfig === null || afterSubmitNotificationConfig === void 0 ? void 0 : afterSubmitNotificationConfig.placement
			});
		}
	};
	const validateFields = () => {
		return form.validateFields();
	};
	const handleValideFieldsSubmit = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (values) {
			try {
				yield validateFields();
				const allValues = form.getFieldsValue(true);
				if (allValues.id) values.id = allValues.id;
				if ((toastConfig === null || toastConfig === void 0 ? void 0 : toastConfig.enable) && !(toastConfig === null || toastConfig === void 0 ? void 0 : toastConfig.showAfterSubmit)) require_index$1.default({ content: toastConfig.title });
				const filteredValues = require_utils.filterSystemFields(allValues);
				const formattedValues = formatSubmitValues ? formatSubmitValues(filteredValues) : filteredValues;
				if (beforeSubmit) {
					if (!(yield beforeSubmit(formattedValues))) return;
				}
				const result = yield onSubmit(formattedValues);
				if (onDataSourceFinish) onDataSourceFinish(formattedValues, result, form);
				if (refreshData) refreshTableData === null || refreshTableData === void 0 || refreshTableData();
				if ((toastConfig === null || toastConfig === void 0 ? void 0 : toastConfig.enable) && (toastConfig === null || toastConfig === void 0 ? void 0 : toastConfig.showAfterSubmit)) require_index$1.default({ content: toastConfig.title });
				if (afterSubmitType) {
					const afterSubmitFunc = afterSubmitFuncMap[afterSubmitType];
					if (afterSubmitFunc) afterSubmitFunc();
				}
				require_utils.setFormFieldsUntouched(form);
			} catch (error) {
				require_index$1.default({ content: `${error.message}` });
			}
		});
		return function handleValideFieldsSubmit(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const onSubmitClick = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
			const values = yield validateFields();
			if (confirmConfig === null || confirmConfig === void 0 ? void 0 : confirmConfig.enable) return require_index.default.confirm({
				title: confirmConfig.title,
				content: confirmConfig.content,
				okText: confirmConfig.okText,
				cancelText: confirmConfig.cancelText,
				onOk: () => {
					handleValideFieldsSubmit(values);
				}
			});
			handleValideFieldsSubmit(values);
		});
		return function onSubmitClick() {
			return _ref3.apply(this, arguments);
		};
	}();
	if (status === "hidden" || renderMode === "view") return null;
	return /* @__PURE__ */ react.default.createElement(require_formItem.default, null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode-data-source-form-submit-button-wrapper" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		type,
		size: "large",
		loading,
		onClick: onSubmitClick,
		disabled: isDisabled,
		htmlType: "submit"
	}, childrenString), contextHolder));
};
//#endregion
exports.default = SubmitButton;
