import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  PreviewCommand
} from "./chunk-5P4ZUCVU.js";
import {
  VersionType
} from "./chunk-FH6U3Y37.js";
import {
  assertBuildOutputExists
} from "./chunk-O6WUNJLD.js";
import "./chunk-NUHN5M5Q.js";
import "./chunk-BBL4SXGY.js";
import "./chunk-KUGUNAKD.js";
import {
  useARMClient
} from "./chunk-D7O6F2SQ.js";
import {
  useBackendComponents
} from "./chunk-HEHKVE3E.js";
import "./chunk-ZOFE4LJ7.js";
import "./chunk-ZR3N5INU.js";
import "./chunk-7VEP3B3B.js";
import "./chunk-HWET32VV.js";
import "./chunk-K2BZ3TOE.js";
import {
  BackendDeploymentClient,
  isHttpError,
  require_semver,
  useBiLogger,
  useDevCenterClient,
  useHttpClient,
  useProjectModel
} from "./chunk-UVMRGZ5H.js";
import {
  Alert,
  Box_default,
  CliError,
  CliErrorCode,
  ConfirmInput,
  Link,
  Notification,
  SelectInput,
  Spinner,
  Text,
  Trans,
  require_lib,
  require_react,
  useAsync,
  useAsyncCallback,
  useTranslation
} from "./chunk-32MMFZ46.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-PYIAC2GK.js";

// src/components/CreateVersionCommand/index.ts
init_esm_shims();

// src/components/CreateVersionCommand/CreateVersionCommand.tsx
init_esm_shims();
var import_react4 = __toESM(require_react(), 1);
var import_variant2 = __toESM(require_lib(), 1);

// src/components/CreateVersionCommand/ConfirmVersionCreation.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var ConfirmVersionCreation = ({ onSubmit }) => {
  const { t } = useTranslation();
  const [cancelled, setCancelled] = (0, import_react.useState)(false);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    ConfirmInput,
    {
      label: t("create_version_command.confirm_version_creation"),
      initialValue: true,
      onSubmit: (confirmation) => {
        if (confirmation) {
          onSubmit(true);
        } else {
          setCancelled(true);
          onSubmit(false);
        }
      }
    }
  ), cancelled && /* @__PURE__ */ import_react.default.createElement(Text, null, t("create_version_command.confirm_version_creation_aborted")));
};

// src/components/CreateVersionCommand/SelectVersionType.tsx
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
var import_semver = __toESM(require_semver(), 1);
var SelectVersionType = ({ version, onSubmit }) => {
  const { t } = useTranslation();
  return /* @__PURE__ */ import_react2.default.createElement(
    SelectInput,
    {
      label: t("create_version_command.version_type_question"),
      options: [
        {
          title: t("create_version_command.version_type_question_minor", {
            version: (0, import_semver.inc)(version, "minor")
          }),
          value: VersionType.MINOR
        },
        {
          title: t("create_version_command.version_type_question_major", {
            version: (0, import_semver.inc)(version, "major")
          }),
          value: VersionType.MAJOR
        }
      ],
      onSubmit: ({ value }) => onSubmit(value)
    }
  );
};

// src/components/CreateVersionCommand/promoteVersion.ts
init_esm_shims();
var promoteVersion = async ({
  appId,
  tag,
  versionType,
  arm,
  biLogger,
  httpClient,
  backendServerUrl
}) => {
  try {
    const { newVersion } = await arm.promoteToDevCenter(
      appId,
      tag,
      versionType
    );
    if (backendServerUrl) {
      const deployment = new BackendDeploymentClient(biLogger, httpClient);
      await deployment.makrDeploymentAsPermanent(
        appId,
        BackendDeploymentClient.DeploymentUrToDeploymentId(backendServerUrl)
      );
    }
    return newVersion;
  } catch (e) {
    if (isHttpError(e) && e.response?.status === 428) {
      throw new CliError({
        code: CliErrorCode.FailedToCreateVersionForSubmittedApp(),
        cause: e
      });
    }
    throw new CliError({
      code: CliErrorCode.FailedToCreateVersion(),
      cause: e
    });
  }
};

// src/components/CreateVersionCommand/VersionValidationStatus.tsx
init_esm_shims();
var import_react3 = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);
var VersionValidationStatus = ({ appId, version }) => {
  const { t } = useTranslation();
  const { getAppValidationFailuresCount } = useDevCenterClient();
  const { status } = useAsync(async () => {
    return getAppValidationFailuresCount(appId, version);
  }, []);
  return (0, import_variant.match)(status, {
    NotRequested: () => null,
    Error: () => null,
    Loading: () => /* @__PURE__ */ import_react3.default.createElement(Spinner, { text: t("create_version_command.checking_validation_status") }),
    Success: ({ result: validationFailuresCount }) => /* @__PURE__ */ import_react3.default.createElement(import_react3.default.Fragment, null, validationFailuresCount === 0 ? /* @__PURE__ */ import_react3.default.createElement(
      Trans,
      {
        i18nKey: "create_version_command.version_validation_ok",
        components: [
          /* @__PURE__ */ import_react3.default.createElement(Link, { url: `https://dev.wix.com/apps/${appId}/versions` })
        ]
      }
    ) : /* @__PURE__ */ import_react3.default.createElement(Alert, { type: "warning" }, /* @__PURE__ */ import_react3.default.createElement(
      Trans,
      {
        i18nKey: "create_version_command.version_validation_has_failures",
        components: [
          /* @__PURE__ */ import_react3.default.createElement(
            Link,
            {
              url: `https://dev.wix.com/apps/${appId}/home?open-blockers-panel`
            }
          )
        ]
      }
    )))
  });
};

// src/components/CreateVersionCommand/CreateVersionCommand.tsx
var CreateVersionCommand = ({ baseUrl, siteIdOverride, preApprovePreview, versionType }) => {
  const { t } = useTranslation();
  const { model } = useProjectModel();
  const { backendComponentsByType } = useBackendComponents();
  const arm = useARMClient();
  const { getAppByVersion } = useDevCenterClient();
  const [tag, setTag] = (0, import_react4.useState)();
  const [versionCreationApproved, setVersionCreationApproved] = (0, import_react4.useState)(preApprovePreview);
  const httpClient = useHttpClient({ type: "backoffice" });
  const biLogger = useBiLogger();
  const { status } = useAsync(async () => {
    await assertBuildOutputExists(model.projectFolder);
    const { latestVersion } = await getAppByVersion({
      appId: model.config.appId
    });
    return latestVersion;
  }, []);
  const promoteVersionAction = useAsyncCallback(
    (_, versionType2, tag2, backendServerUrl) => {
      return promoteVersion({
        appId: model.config.appId,
        tag: tag2,
        versionType: versionType2,
        arm,
        biLogger,
        httpClient,
        backendServerUrl
      });
    }
  );
  function setTagAndMaybePromote(tag2, backendServerUrl) {
    setTag(tag2);
    if (versionCreationApproved && versionType) {
      void promoteVersionAction.execute(versionType, tag2, backendServerUrl);
    }
  }
  return (0, import_variant2.match)(status, {
    Loading: () => /* @__PURE__ */ import_react4.default.createElement(Spinner, null),
    Error: () => null,
    Success: ({ result: latestVersion }) => /* @__PURE__ */ import_react4.default.createElement(import_react4.default.Fragment, null, /* @__PURE__ */ import_react4.default.createElement(Text, null, t("create_version_command.check_preview_before_version_release")), /* @__PURE__ */ import_react4.default.createElement(
      PreviewCommand,
      {
        baseUrl,
        siteIdOverride,
        onPreviewCreated: setTagAndMaybePromote
      }
    ), tag && backendComponentsByType.Event.length > 0 && /* @__PURE__ */ import_react4.default.createElement(Box_default, { marginBottom: 1 }, /* @__PURE__ */ import_react4.default.createElement(Alert, { type: "warning" }, /* @__PURE__ */ import_react4.default.createElement(Text, null, t("create_version_command.confirm_version_events_warning")))), tag && !preApprovePreview && /* @__PURE__ */ import_react4.default.createElement(Box_default, { marginBottom: 1 }, /* @__PURE__ */ import_react4.default.createElement(ConfirmVersionCreation, { onSubmit: setVersionCreationApproved })), tag && versionCreationApproved && /* @__PURE__ */ import_react4.default.createElement(import_react4.default.Fragment, null, /* @__PURE__ */ import_react4.default.createElement(Box_default, { marginBottom: 1 }, /* @__PURE__ */ import_react4.default.createElement(Text, null, t("create_version_command.current_version", {
      version: latestVersion
    }))), !versionType && /* @__PURE__ */ import_react4.default.createElement(
      SelectVersionType,
      {
        version: latestVersion,
        onSubmit: (versionType2) => promoteVersionAction.execute(versionType2, tag)
      }
    ), (0, import_variant2.match)(promoteVersionAction.status, {
      NotRequested: () => null,
      Error: () => null,
      Loading: () => /* @__PURE__ */ import_react4.default.createElement(Spinner, { text: t("create_version_command.creating_version") }),
      Success: ({ result: version }) => /* @__PURE__ */ import_react4.default.createElement(import_react4.default.Fragment, null, /* @__PURE__ */ import_react4.default.createElement(Alert, { type: "success" }, t("create_version_command.creating_version_success", {
        version
      })), /* @__PURE__ */ import_react4.default.createElement(
        VersionValidationStatus,
        {
          appId: model.config.appId,
          version
        }
      ))
    })))
  });
};

// src/components/CreateVersionCommand/CreateVersionCommandDeprecated.tsx
init_esm_shims();
var import_react5 = __toESM(require_react(), 1);
var CreateVersionCommandDeprecated = ({ children }) => {
  return /* @__PURE__ */ import_react5.default.createElement(import_react5.default.Fragment, null, /* @__PURE__ */ import_react5.default.createElement(Box_default, { marginBottom: 1, flexDirection: "column" }, /* @__PURE__ */ import_react5.default.createElement(Notification, null, /* @__PURE__ */ import_react5.default.createElement(Alert, { type: "warning" }, /* @__PURE__ */ import_react5.default.createElement(
    Trans,
    {
      i18nKey: "create_version_command.deprecated.warn",
      components: [
        /* @__PURE__ */ import_react5.default.createElement(Text, { skin: "info" }),
        /* @__PURE__ */ import_react5.default.createElement(Text, { skin: "info" }),
        /* @__PURE__ */ import_react5.default.createElement(Link, { url: "https://dev.wix.com/docs/build-apps/develop-your-app/frameworks/wix-cli/app-development/cli-command-reference#release" })
      ]
    }
  )))), children);
};
export {
  CreateVersionCommand,
  CreateVersionCommandDeprecated
};
//# sourceMappingURL=CreateVersionCommand-4XOTXR5T.js.map