import { getText } from "../../locales/index.js";
import PisellModal from "../pisellModal/index.js";
import VersionSelect from "../versionSelect/index.js";
import React, { useEffect, useState } from "react";
import { Col, Form, Input, InputNumber, Radio, Row, message } from "antd";
import "./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] = Form.useForm();
	const [versionType, setVersionType] = 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)) {
					message.error(getText("pisell-version-modal-version-error"));
					return;
				}
				version = `${values.major}.${values.minor}.${values.patch}`;
			}
			onConfirm({
				version,
				note,
				baseVersion,
				versionType
			});
		});
	};
	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.createElement(PisellModal, {
		title: title || getText("pisell-version-modal-title"),
		open: visible,
		onCancel,
		destroyOnClose: true,
		onOk: handleSubmit,
		okText: getText("pisell-version-modal-confirm"),
		cancelText: getText("pisell-version-modal-cancel"),
		confirmLoading
	}, /* @__PURE__ */ React.createElement(Form, {
		form,
		layout: "vertical",
		preserve: false,
		className: "pisell-lowcode-version-modal-form"
	}, isShowCurrentVersion && /* @__PURE__ */ React.createElement("p", { className: "pisell-lowcode-version-modal-current-version" }, getText("pisell-version-modal-current-version"), ": ", currentVersion), isShowBasedOn && /* @__PURE__ */ React.createElement(Form.Item, {
		label: getText("pisell-version-modal-based-on-version"),
		name: "baseVersion",
		initialValue: currentVersion,
		rules: [{ required: true }]
	}, /* @__PURE__ */ React.createElement(VersionSelect, {
		versions,
		fieldNames: {
			label: "label",
			value: "version",
			options: "options"
		}
	})), /* @__PURE__ */ React.createElement(Form.Item, {
		label: getText("pisell-version-modal-version"),
		name: "version",
		initialValue: "patch",
		rules: [{ required: true }]
	}, /* @__PURE__ */ React.createElement(Radio.Group, {
		onChange: handleVersionChange,
		style
	}, /* @__PURE__ */ React.createElement(Radio, { value: patch }, getText("pisell-version-modal-patch"), " (", patch, ")"), /* @__PURE__ */ React.createElement(Radio, { value: minor }, getText("pisell-version-modal-minor"), " (", minor, ")"), /* @__PURE__ */ React.createElement(Radio, { value: major }, getText("pisell-version-modal-major"), " (", major, ")"), /* @__PURE__ */ React.createElement(Radio, { value: "custom" }, getText("pisell-version-modal-custom")))), versionType === "custom" && /* @__PURE__ */ React.createElement(Row, { gutter: 8 }, /* @__PURE__ */ React.createElement(Col, { span: 8 }, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "major",
		label: getText("pisell-version-modal-major"),
		rules: [{
			required: true,
			message: getText("pisell-version-modal-version-required")
		}]
	}, /* @__PURE__ */ React.createElement(InputNumber, {
		min: 0,
		precision: 0,
		style: { width: "100%" },
		placeholder: getText("pisell-version-modal-major")
	}))), /* @__PURE__ */ React.createElement(Col, { span: 8 }, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "minor",
		label: getText("pisell-version-modal-minor"),
		rules: [{
			required: true,
			message: getText("pisell-version-modal-version-required")
		}]
	}, /* @__PURE__ */ React.createElement(InputNumber, {
		precision: 0,
		min: 0,
		style: { width: "100%" },
		placeholder: getText("pisell-version-modal-minor")
	}))), /* @__PURE__ */ React.createElement(Col, { span: 8 }, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "patch",
		label: getText("pisell-version-modal-patch"),
		rules: [{
			required: true,
			message: getText("pisell-version-modal-version-required")
		}]
	}, /* @__PURE__ */ React.createElement(InputNumber, {
		min: 0,
		precision: 0,
		style: { width: "100%" },
		placeholder: getText("pisell-version-modal-patch")
	})))), isShowNote && /* @__PURE__ */ React.createElement(Form.Item, {
		label: getText("pisell-version-modal-update-notes"),
		name: "note"
	}, /* @__PURE__ */ React.createElement(Input.TextArea, { placeholder: getText("pisell-version-modal-describe-update-notes") }))));
};
//#endregion
export { PublishVersionModal as default };
