import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import locales_default from "../locales.js";
import React, { useEffect } from "react";
import { Drawer, Form, Input, Space } from "antd";
import { locales } from "@pisell/utils";
import { BaseTranslation, Button as Button$1 } from "@pisell/materials";
//#region src/components/systemSettings/registry/components/GroupModal.tsx
const getText = (key) => {
	const globalText = locales.getText(key);
	if (globalText !== key) return globalText;
	return (locales_default[locales.locale || "zh-CN"] || locales_default["en"] || {})[key] || key;
};
const GroupModal = ({ open, type, data, onOk, onCancel }) => {
	const [form] = Form.useForm();
	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 = _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 = locales.getText(type === "add" ? "registry.modal.add.group" : "registry.modal.edit.group");
	return /* @__PURE__ */ React.createElement(Drawer, {
		title,
		open,
		onClose: handleCancel,
		width: 400,
		destroyOnClose: true,
		footer: /* @__PURE__ */ React.createElement("div", { style: { textAlign: "right" } }, /* @__PURE__ */ React.createElement(Space, { size: "small" }, /* @__PURE__ */ React.createElement(Button$1, { onClick: handleCancel }, locales.getText("registry.button.cancel")), /* @__PURE__ */ React.createElement(Button$1, {
			type: "primary",
			onClick: handleOk
		}, locales.getText("registry.button.save"))))
	}, /* @__PURE__ */ React.createElement(Form, {
		form,
		layout: "vertical",
		requiredMark: true
	}, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "title",
		label: locales.getText("registry.form.title"),
		rules: [{
			required: true,
			message: locales.getText("registry.message.required")
		}]
	}, /* @__PURE__ */ React.createElement(BaseTranslation, { placeholder: locales.getText("registry.form.name.placeholder") })), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "code",
		label: locales.getText("registry.form.code"),
		rules: [{
			required: true,
			message: 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.createElement(Input, { placeholder: locales.getText("registry.form.code.placeholder") })), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "description",
		label: locales.getText("registry.form.description")
	}, /* @__PURE__ */ React.createElement(BaseTranslation, {
		placeholder: locales.getText("registry.form.description.placeholder"),
		renderMode: "edit"
	}))));
};
//#endregion
export { GroupModal as default };
