const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_index$1 = require("../pisellModal/index.js");
const require_index$2 = require("../versionSelect/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
require("./index.less");
//#region src/components/versionModal/index.tsx
const style = {
	display: "flex",
	flexDirection: "column",
	gap: 8
};
const parseVersion = (version) => {
	const [major, minor, patch] = version.split(".").map(Number);
	return [
		major || 0,
		minor || 0,
		patch || 0
	];
};
const isVersionValid = (customVersion, currentVersion) => {
	const [cMajor, cMinor, cPatch] = parseVersion(customVersion);
	const [liveMajor, liveMinor, livePatch] = parseVersion(currentVersion);
	if (cMajor > liveMajor) return true;
	if (cMajor === liveMajor && cMinor > liveMinor) return true;
	if (cMajor === liveMajor && cMinor === liveMinor && cPatch > livePatch) return true;
	return false;
};
const getNextVersions = (version) => {
	const [major, minor, patch] = parseVersion(version);
	return {
		patch: `${major}.${minor}.${patch + 1}`,
		minor: `${major}.${minor + 1}.0`,
		major: `${major + 1}.0.0`
	};
};
const PublishVersionModal = ({ visible, currentVersion = "1.0.0", onCancel, onConfirm, isShowNote, confirmLoading = false, versions = [], isShowBasedOn, isShowCurrentVersion, isValidateVersion = true, title }) => {
	const [form] = antd.Form.useForm();
	const [versionType, setVersionType] = (0, react.useState)("");
	const { patch, minor, major } = getNextVersions(currentVersion);
	const handleVersionChange = (e) => {
		setVersionType(e.target.value);
	};
	const handleSubmit = () => {
		form.validateFields().then((values) => {
			let { version, note, baseVersion } = values;
			let versionType = "";
			if (version === "custom") {
				versionType = "custom";
				if (isValidateVersion && !isVersionValid(`${values.major}.${values.minor}.${values.patch}`, currentVersion)) {
					antd.message.error(require_index.getText("pisell-version-modal-version-error"));
					return;
				}
				version = `${values.major}.${values.minor}.${values.patch}`;
			}
			onConfirm({
				version,
				note,
				baseVersion,
				versionType
			});
		});
	};
	(0, react.useEffect)(() => {
		if (visible) {
			setVersionType("");
			form.setFieldsValue({
				version: patch,
				major: parseVersion(patch)[0] || 0,
				minor: parseVersion(patch)[1] || 0,
				patch: parseVersion(patch)[2] || 0,
				baseVersion: currentVersion
			});
		}
	}, [visible]);
	return /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		title: title || require_index.getText("pisell-version-modal-title"),
		open: visible,
		onCancel,
		destroyOnClose: true,
		onOk: handleSubmit,
		okText: require_index.getText("pisell-version-modal-confirm"),
		cancelText: require_index.getText("pisell-version-modal-cancel"),
		confirmLoading
	}, /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		preserve: false,
		className: "pisell-lowcode-version-modal-form"
	}, isShowCurrentVersion && /* @__PURE__ */ react.default.createElement("p", { className: "pisell-lowcode-version-modal-current-version" }, require_index.getText("pisell-version-modal-current-version"), ": ", currentVersion), isShowBasedOn && /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: require_index.getText("pisell-version-modal-based-on-version"),
		name: "baseVersion",
		initialValue: currentVersion,
		rules: [{ required: true }]
	}, /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		versions,
		fieldNames: {
			label: "label",
			value: "version",
			options: "options"
		}
	})), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: require_index.getText("pisell-version-modal-version"),
		name: "version",
		initialValue: "patch",
		rules: [{ required: true }]
	}, /* @__PURE__ */ react.default.createElement(antd.Radio.Group, {
		onChange: handleVersionChange,
		style
	}, /* @__PURE__ */ react.default.createElement(antd.Radio, { value: patch }, require_index.getText("pisell-version-modal-patch"), " (", patch, ")"), /* @__PURE__ */ react.default.createElement(antd.Radio, { value: minor }, require_index.getText("pisell-version-modal-minor"), " (", minor, ")"), /* @__PURE__ */ react.default.createElement(antd.Radio, { value: major }, require_index.getText("pisell-version-modal-major"), " (", major, ")"), /* @__PURE__ */ react.default.createElement(antd.Radio, { value: "custom" }, require_index.getText("pisell-version-modal-custom")))), versionType === "custom" && /* @__PURE__ */ react.default.createElement(antd.Row, { gutter: 8 }, /* @__PURE__ */ react.default.createElement(antd.Col, { span: 8 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "major",
		label: require_index.getText("pisell-version-modal-major"),
		rules: [{
			required: true,
			message: require_index.getText("pisell-version-modal-version-required")
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
		min: 0,
		precision: 0,
		style: { width: "100%" },
		placeholder: require_index.getText("pisell-version-modal-major")
	}))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 8 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "minor",
		label: require_index.getText("pisell-version-modal-minor"),
		rules: [{
			required: true,
			message: require_index.getText("pisell-version-modal-version-required")
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
		precision: 0,
		min: 0,
		style: { width: "100%" },
		placeholder: require_index.getText("pisell-version-modal-minor")
	}))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 8 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "patch",
		label: require_index.getText("pisell-version-modal-patch"),
		rules: [{
			required: true,
			message: require_index.getText("pisell-version-modal-version-required")
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
		min: 0,
		precision: 0,
		style: { width: "100%" },
		placeholder: require_index.getText("pisell-version-modal-patch")
	})))), isShowNote && /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: require_index.getText("pisell-version-modal-update-notes"),
		name: "note"
	}, /* @__PURE__ */ react.default.createElement(antd.Input.TextArea, { placeholder: require_index.getText("pisell-version-modal-describe-update-notes") }))));
};
//#endregion
exports.default = PublishVersionModal;
