const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_index$1 = require("../pisellCard/index.js");
const require_index$2 = require("../pisellModal/index.js");
const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index$3 = require("../button/index.js");
const require_index$4 = require("../icon/index.js");
const require_index$5 = require("../pisellAvatar/index.js");
const require_index$6 = require("../pisellToast/index.js");
const require_index$7 = require("../versionSelect/index.js");
const require_index$8 = require("../versionModal/index.js");
const require_utils = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/appVersionControl/index.tsx
const AppVersionControl = (props) => {
	const { value, versions = [], onVersionChange, onCreateVersion, onPromoteToLive, className, style, allowPublish = true, allowCreate = true } = props;
	const [promoteLoading, setPromoteLoading] = (0, react.useState)(false);
	const [promoteToLiveModal, setPromoteToLiveModal] = (0, react.useState)(false);
	const [publishVersionModal, setPublishVersionModal] = (0, react.useState)(false);
	const [publishLoading, setPublishLoading] = (0, react.useState)(false);
	const currentVersion = (0, react.useMemo)(() => {
		return versions.find((v) => v.value === value);
	}, [value, versions]);
	const maxVersion = (0, react.useMemo)(() => {
		return require_utils.getMaxVersion(versions.map((v) => v.version));
	}, [versions]);
	const handlePromoteToLive = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			setPromoteLoading(true);
			try {
				yield onPromoteToLive === null || onPromoteToLive === void 0 ? void 0 : onPromoteToLive(value, currentVersion);
				setPromoteToLiveModal(false);
			} catch (error) {
				console.error("Failed to promote version:", error);
			} finally {
				setPromoteLoading(false);
			}
		});
		return function handlePromoteToLive() {
			return _ref.apply(this, arguments);
		};
	}();
	const handlePublishVersion = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (data) {
			setPublishLoading(true);
			if (data.versionType === "custom") {
				if (versions.find((v) => v.version === data.version)) {
					setPublishLoading(false);
					return require_index$6.default({ content: require_index.getText("pisell-lowcode-app-version-control-version-exists") });
				}
			}
			try {
				yield onCreateVersion === null || onCreateVersion === void 0 ? void 0 : onCreateVersion({
					version: data.version,
					baseVersion: data.baseVersion || ""
				});
				setPublishVersionModal(false);
			} catch (error) {
				console.error("Failed to create version:", error);
			} finally {
				setPublishLoading(false);
			}
		});
		return function handlePublishVersion(_x) {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleVersionChange = (value) => {
		const version = versions.find((v) => v.value === value);
		onVersionChange === null || onVersionChange === void 0 || onVersionChange(value, version);
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		className: (0, classnames.default)("pisell-lowcode-app-version-control", className),
		style
	}, /* @__PURE__ */ react.default.createElement(require_index$5.default, {
		size: "xl",
		shape: "square",
		borderRadius: 10,
		useTextAvatar: true,
		className: "pisell-lowcode-app-version-control-avatar",
		src: currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion.avatar
	}, /* @__PURE__ */ react.default.createElement(require_index$4.default, {
		type: "pisell2-container",
		className: "pisell-lowcode-app-version-control-avatar-icon"
	})), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode-app-version-control-title" }, (currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion.name) || ""), /* @__PURE__ */ react.default.createElement(require_index$7.default, {
		versions,
		value,
		onChange: handleVersionChange
	}), allowCreate && /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		icon: /* @__PURE__ */ react.default.createElement(require_index$4.default, { type: "pisell2-plus" }),
		block: true,
		size: "large",
		onClick: () => setPublishVersionModal(true)
	}, require_index.getText("pisell-lowcode-app-version-control-new-version")), allowPublish && (currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion.status) !== "live" && /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		type: "primary",
		block: true,
		size: "large",
		onClick: () => setPromoteToLiveModal(true)
	}, require_index.getText("pisell-lowcode-app-version-control-promote-to-live"))), /* @__PURE__ */ react.default.createElement(require_index$2.default.Information, {
		open: promoteToLiveModal,
		title: require_index.getText("pisell-lowcode-app-version-control-promote-version")(currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion.version),
		describe: require_index.getText("pisell-lowcode-app-version-control-promote-version-describe"),
		okText: require_index.getText("pisell-lowcode-app-version-control-promote-version-ok"),
		cancelText: require_index.getText("pisell-lowcode-app-version-control-promote-version-cancel"),
		onOk: handlePromoteToLive,
		confirmLoading: promoteLoading,
		onCancel: () => setPromoteToLiveModal(false)
	}), /* @__PURE__ */ react.default.createElement(require_index$8.default, {
		title: require_index.getText("pisell-lowcode-app-version-control-version-create-title"),
		currentVersion: maxVersion || "",
		visible: publishVersionModal,
		onCancel: () => setPublishVersionModal(false),
		onConfirm: handlePublishVersion,
		isShowNote: false,
		confirmLoading: publishLoading,
		versions,
		isShowBasedOn: true,
		isValidateVersion: false
	}));
};
//#endregion
exports.default = AppVersionControl;
