var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/appVersionControl/index.tsx
var appVersionControl_exports = {};
__export(appVersionControl_exports, {
  default: () => appVersionControl_default
});
module.exports = __toCommonJS(appVersionControl_exports);
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_button = __toESM(require("../button"));
var import_pisellCard = __toESM(require("../pisellCard"));
var import_pisellModal = __toESM(require("../pisellModal"));
var import_pisellAvatar = __toESM(require("../pisellAvatar"));
var import_icon = __toESM(require("../icon"));
var import_versionModal = __toESM(require("../versionModal"));
var import_versionSelect = __toESM(require("../versionSelect"));
var import_pisellToast = __toESM(require("../pisellToast"));
var import_locales = require("../../locales");
var import_utils = require("./utils");
var import_index = require("./index.less");
var AppVersionControl = (props) => {
  const {
    value,
    versions = [],
    onVersionChange,
    onCreateVersion,
    onPromoteToLive,
    className,
    style,
    allowPublish = true,
    allowCreate = true
  } = props;
  const [promoteLoading, setPromoteLoading] = (0, import_react.useState)(false);
  const [promoteToLiveModal, setPromoteToLiveModal] = (0, import_react.useState)(false);
  const [publishVersionModal, setPublishVersionModal] = (0, import_react.useState)(false);
  const [publishLoading, setPublishLoading] = (0, import_react.useState)(false);
  const currentVersion = (0, import_react.useMemo)(() => {
    return versions.find((v) => v.value === value);
  }, [value, versions]);
  const maxVersion = (0, import_react.useMemo)(() => {
    return (0, import_utils.getMaxVersion)(versions.map((v) => v.version));
  }, [versions]);
  const handlePromoteToLive = async () => {
    setPromoteLoading(true);
    try {
      await (onPromoteToLive == null ? void 0 : onPromoteToLive(value, currentVersion));
      setPromoteToLiveModal(false);
    } catch (error) {
      console.error("Failed to promote version:", error);
    } finally {
      setPromoteLoading(false);
    }
  };
  const handlePublishVersion = async (data) => {
    setPublishLoading(true);
    if (data.versionType === "custom") {
      const version = versions.find((v) => v.version === data.version);
      if (version) {
        setPublishLoading(false);
        return (0, import_pisellToast.default)({
          content: (0, import_locales.getText)("pisell-lowcode-app-version-control-version-exists")
        });
      }
    }
    try {
      await (onCreateVersion == null ? void 0 : onCreateVersion({
        version: data.version,
        baseVersion: data.baseVersion || ""
      }));
      setPublishVersionModal(false);
    } catch (error) {
      console.error("Failed to create version:", error);
    } finally {
      setPublishLoading(false);
    }
  };
  const handleVersionChange = (value2) => {
    const version = versions.find((v) => v.value === value2);
    onVersionChange == null ? void 0 : onVersionChange(value2, version);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_pisellCard.default,
    {
      className: (0, import_classnames.default)("pisell-lowcode-app-version-control", className),
      style
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_pisellAvatar.default,
      {
        size: "xl",
        shape: "square",
        borderRadius: 10,
        useTextAvatar: true,
        className: "pisell-lowcode-app-version-control-avatar",
        src: currentVersion == null ? void 0 : currentVersion.avatar
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_icon.default,
        {
          type: "pisell2-container",
          className: "pisell-lowcode-app-version-control-avatar-icon"
        }
      )
    ),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-app-version-control-title" }, (currentVersion == null ? void 0 : currentVersion.name) || ""),
    /* @__PURE__ */ import_react.default.createElement(
      import_versionSelect.default,
      {
        versions,
        value,
        onChange: handleVersionChange
      }
    ),
    allowCreate && /* @__PURE__ */ import_react.default.createElement(
      import_button.default,
      {
        icon: /* @__PURE__ */ import_react.default.createElement(import_icon.default, { type: "pisell2-plus" }),
        block: true,
        size: "large",
        onClick: () => setPublishVersionModal(true)
      },
      (0, import_locales.getText)("pisell-lowcode-app-version-control-new-version")
    ),
    allowPublish && (currentVersion == null ? void 0 : currentVersion.status) !== "live" && /* @__PURE__ */ import_react.default.createElement(
      import_button.default,
      {
        type: "primary",
        block: true,
        size: "large",
        onClick: () => setPromoteToLiveModal(true)
      },
      (0, import_locales.getText)("pisell-lowcode-app-version-control-promote-to-live")
    )
  ), /* @__PURE__ */ import_react.default.createElement(
    import_pisellModal.default.Information,
    {
      open: promoteToLiveModal,
      title: (0, import_locales.getText)("pisell-lowcode-app-version-control-promote-version")(currentVersion == null ? void 0 : currentVersion.version),
      describe: (0, import_locales.getText)("pisell-lowcode-app-version-control-promote-version-describe"),
      okText: (0, import_locales.getText)("pisell-lowcode-app-version-control-promote-version-ok"),
      cancelText: (0, import_locales.getText)("pisell-lowcode-app-version-control-promote-version-cancel"),
      onOk: handlePromoteToLive,
      confirmLoading: promoteLoading,
      onCancel: () => setPromoteToLiveModal(false)
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_versionModal.default,
    {
      currentVersion: maxVersion || "",
      visible: publishVersionModal,
      onCancel: () => setPublishVersionModal(false),
      onConfirm: handlePublishVersion,
      isShowNote: false,
      confirmLoading: publishLoading,
      versions,
      isShowBasedOn: true,
      isValidateVersion: false
    }
  ));
};
var appVersionControl_default = AppVersionControl;
