import PisellModal from "../../../pisellModal/index.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useDataSource from "../../hooks/useDataSource.js";
import useFormSetting from "../provider/hooks/useFormSetting.js";
import PisellToast from "../../../pisellToast/index.js";
import { updateDeviceSetting, updateTenantSetting } from "../serve.js";
import { filterSystemFields, setFormFieldsUntouched } from "../utils.js";
import useCtxActions from "../../hooks/useCtxActions.js";
import useFormSettingParams from "../../hooks/useFormSettingParams.js";
import FormItem from "../formItem.js";
import React, { useState } from "react";
import { Button, Form, notification } from "antd";
import "./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] = useState(false);
	const [api, contextHolder] = notification.useNotification();
	const { scope, target } = useFormSettingParams();
	const form = Form.useFormInstance();
	const { renderMode, onDataSourceFinish, formatSubmitValues, customSubmit, beforeSubmit, scene, sceneRequestId } = useFormSetting();
	const { setVisible, refreshTableData } = useCtxActions();
	const { create, update, dataSource } = useDataSource();
	const isDisabled = status === "disabled";
	const onSubmit = function() {
		var _ref = _asyncToGenerator(function* (values) {
			setLoading(true);
			try {
				if (dataSource === null || dataSource === void 0 ? void 0 : dataSource.fromDeviceSettingMeta) return yield updateDeviceSetting({
					scene,
					sceneRequestId: sceneRequestId || "",
					data: values
				});
				if (dataSource === null || dataSource === void 0 ? void 0 : dataSource.fromSettingMeta) return yield 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: () => {
			PisellModal.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 = _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)) PisellToast({ content: toastConfig.title });
				const filteredValues = 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)) PisellToast({ content: toastConfig.title });
				if (afterSubmitType) {
					const afterSubmitFunc = afterSubmitFuncMap[afterSubmitType];
					if (afterSubmitFunc) afterSubmitFunc();
				}
				setFormFieldsUntouched(form);
			} catch (error) {
				PisellToast({ content: `${error.message}` });
			}
		});
		return function handleValideFieldsSubmit(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const onSubmitClick = function() {
		var _ref3 = _asyncToGenerator(function* () {
			const values = yield validateFields();
			if (confirmConfig === null || confirmConfig === void 0 ? void 0 : confirmConfig.enable) return PisellModal.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.createElement(FormItem, null, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode-data-source-form-submit-button-wrapper" }, /* @__PURE__ */ React.createElement(Button, {
		type,
		size: "large",
		loading,
		onClick: onSubmitClick,
		disabled: isDisabled,
		htmlType: "submit"
	}, childrenString), contextHolder));
};
//#endregion
export { SubmitButton as default };
