const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_locales = require("../locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/components/systemSettings/registry/components/GroupModal.tsx
const getText = (key) => {
	const globalText = _pisell_utils.locales.getText(key);
	if (globalText !== key) return globalText;
	return (require_locales.default[_pisell_utils.locales.locale || "zh-CN"] || require_locales.default["en"] || {})[key] || key;
};
const GroupModal = ({ open, type, data, onOk, onCancel }) => {
	const [form] = antd.Form.useForm();
	(0, react.useEffect)(() => {
		if (open) if (type === "edit" && data) form.setFieldsValue({
			title: data.title,
			code: data.code,
			description: data === null || data === void 0 ? void 0 : data.description
		});
		else form.resetFields();
	}, [
		open,
		type,
		data,
		form
	]);
	const handleOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			try {
				yield onOk(yield form.validateFields());
				form.resetFields();
			} catch (error) {
				console.error("Validation failed:", error);
			}
		});
		return function handleOk() {
			return _ref.apply(this, arguments);
		};
	}();
	const handleCancel = () => {
		form.resetFields();
		onCancel();
	};
	const title = _pisell_utils.locales.getText(type === "add" ? "registry.modal.add.group" : "registry.modal.edit.group");
	return /* @__PURE__ */ react.default.createElement(antd.Drawer, {
		title,
		open,
		onClose: handleCancel,
		width: 400,
		destroyOnClose: true,
		footer: /* @__PURE__ */ react.default.createElement("div", { style: { textAlign: "right" } }, /* @__PURE__ */ react.default.createElement(antd.Space, { size: "small" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, { onClick: handleCancel }, _pisell_utils.locales.getText("registry.button.cancel")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "primary",
			onClick: handleOk
		}, _pisell_utils.locales.getText("registry.button.save"))))
	}, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		requiredMark: true
	}, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "title",
		label: _pisell_utils.locales.getText("registry.form.title"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("registry.message.required")
		}]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.BaseTranslation, { placeholder: _pisell_utils.locales.getText("registry.form.name.placeholder") })), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "code",
		label: _pisell_utils.locales.getText("registry.form.code"),
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("registry.message.required")
		}, { validator: (_, value) => {
			if (!value) return Promise.resolve();
			if (!/^[a-z0-9\._]+$/.test(value)) return Promise.reject(new Error(getText("registry.message.code.format")));
			if (!/^[a-z]/.test(value)) return Promise.reject(new Error(getText("registry.message.code.start")));
			if (/[\._]$/.test(value)) return Promise.reject(new Error(getText("registry.message.code.end")));
			if (/[\.]{2,}|[\_]{2,}/.test(value)) return Promise.reject(new Error(getText("registry.message.code.consecutive")));
			return Promise.resolve();
		} }]
	}, /* @__PURE__ */ react.default.createElement(antd.Input, { placeholder: _pisell_utils.locales.getText("registry.form.code.placeholder") })), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "description",
		label: _pisell_utils.locales.getText("registry.form.description")
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.BaseTranslation, {
		placeholder: _pisell_utils.locales.getText("registry.form.description.placeholder"),
		renderMode: "edit"
	}))));
};
//#endregion
exports.default = GroupModal;
