import { getText } from "../../locales/index.js";
import PisellCard from "../pisellCard/index.js";
import PisellModal from "../pisellModal/index.js";
import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import Button from "../button/index.js";
import icon_default from "../icon/index.js";
import PisellAvatar from "../pisellAvatar/index.js";
import PisellToast from "../pisellToast/index.js";
import VersionSelect from "../versionSelect/index.js";
import PublishVersionModal from "../versionModal/index.js";
import { getMaxVersion } from "./utils.js";
import React, { useMemo, useState } from "react";
import classNames from "classnames";
import "./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] = useState(false);
	const [promoteToLiveModal, setPromoteToLiveModal] = useState(false);
	const [publishVersionModal, setPublishVersionModal] = useState(false);
	const [publishLoading, setPublishLoading] = useState(false);
	const currentVersion = useMemo(() => {
		return versions.find((v) => v.value === value);
	}, [value, versions]);
	const maxVersion = useMemo(() => {
		return getMaxVersion(versions.map((v) => v.version));
	}, [versions]);
	const handlePromoteToLive = function() {
		var _ref = _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 = _asyncToGenerator(function* (data) {
			setPublishLoading(true);
			if (data.versionType === "custom") {
				if (versions.find((v) => v.version === data.version)) {
					setPublishLoading(false);
					return PisellToast({ content: 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.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(PisellCard, {
		className: classNames("pisell-lowcode-app-version-control", className),
		style
	}, /* @__PURE__ */ React.createElement(PisellAvatar, {
		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.createElement(icon_default, {
		type: "pisell2-container",
		className: "pisell-lowcode-app-version-control-avatar-icon"
	})), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode-app-version-control-title" }, (currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion.name) || ""), /* @__PURE__ */ React.createElement(VersionSelect, {
		versions,
		value,
		onChange: handleVersionChange
	}), allowCreate && /* @__PURE__ */ React.createElement(Button, {
		icon: /* @__PURE__ */ React.createElement(icon_default, { type: "pisell2-plus" }),
		block: true,
		size: "large",
		onClick: () => setPublishVersionModal(true)
	}, getText("pisell-lowcode-app-version-control-new-version")), allowPublish && (currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion.status) !== "live" && /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		block: true,
		size: "large",
		onClick: () => setPromoteToLiveModal(true)
	}, getText("pisell-lowcode-app-version-control-promote-to-live"))), /* @__PURE__ */ React.createElement(PisellModal.Information, {
		open: promoteToLiveModal,
		title: getText("pisell-lowcode-app-version-control-promote-version")(currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion.version),
		describe: getText("pisell-lowcode-app-version-control-promote-version-describe"),
		okText: getText("pisell-lowcode-app-version-control-promote-version-ok"),
		cancelText: getText("pisell-lowcode-app-version-control-promote-version-cancel"),
		onOk: handlePromoteToLive,
		confirmLoading: promoteLoading,
		onCancel: () => setPromoteToLiveModal(false)
	}), /* @__PURE__ */ React.createElement(PublishVersionModal, {
		title: 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
export { AppVersionControl as default };
