import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  PreviewCommand,
  assertBuildOutputExists
} from "./chunk-6X7N6N75.js";
import {
  useARMClient
} from "./chunk-PSN6WZCB.js";
import {
  useWixCliAppBi
} from "./chunk-EH63OURU.js";
import {
  useBackendComponents
} from "./chunk-MK5OQBNN.js";
import {
  Alert,
  Box_default,
  Cart,
  CliError,
  CliErrorCode,
  ConfirmInput,
  Link,
  Notification,
  SelectInput,
  SelectOptionAvailability,
  Spinner,
  Statistics,
  Text,
  TextInput,
  Trans,
  require_lib,
  require_react,
  require_semver,
  useAsync,
  useAsyncCallback,
  useProjectModel,
  useTranslation
} from "./chunk-VCFM6SMQ.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-4EFJZ3GQ.js";

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

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

// src/components/ReleaseCommand/Release.tsx
init_esm_shims();
var import_react7 = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);
var import_semver = __toESM(require_semver(), 1);

// src/components/ReleaseCommand/Steps/CreatePreview.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var CreatePreview = (props) => {
  const { baseUrl, siteIdOverride, onCreated } = props;
  return /* @__PURE__ */ import_react.default.createElement(Box_default, { flexDirection: "column", gap: 1 }, /* @__PURE__ */ import_react.default.createElement(Trans, { i18nKey: "release_command.check_preview_before_version_release" }), /* @__PURE__ */ import_react.default.createElement(Box_default, { flexDirection: "column" }, /* @__PURE__ */ import_react.default.createElement(
    PreviewCommand,
    {
      baseUrl,
      siteIdOverride,
      onPreviewCreated: ({ armTag }) => onCreated(armTag)
    }
  )));
};

// src/components/ReleaseCommand/Steps/Warnings.tsx
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
var Warnings = () => {
  const { backendComponentsByType } = useBackendComponents();
  if (backendComponentsByType.Event.length === 0) {
    return null;
  }
  return /* @__PURE__ */ import_react2.default.createElement(Box_default, { flexDirection: "column", paddingBottom: 1 }, /* @__PURE__ */ import_react2.default.createElement(Notification, null, /* @__PURE__ */ import_react2.default.createElement(Alert, { type: "warning" }, /* @__PURE__ */ import_react2.default.createElement(Trans, { i18nKey: "release_command.confirm_version_events_warning" }))));
};

// src/components/ReleaseCommand/Steps/ReleaseComment.tsx
init_esm_shims();
var import_react3 = __toESM(require_react(), 1);
var ReleaseComment = (props) => {
  const { onEntered } = props;
  const { t } = useTranslation();
  const bi = useWixCliAppBi();
  const validate = (0, import_react3.useCallback)(
    (value) => {
      if (value && value.length > 250) {
        return t("release_command.release_comment_requirement");
      }
      return true;
    },
    [t]
  );
  return /* @__PURE__ */ import_react3.default.createElement(
    TextInput,
    {
      label: t("release_command.enter_release_comment"),
      onSubmit: (answer) => {
        bi.cliFlowStepAnswered({
          question: t("release_command.enter_release_comment"),
          questionKey: "release_command.enter_release_comment",
          answer
        });
        onEntered(answer);
      },
      validate
    }
  );
};

// src/components/ReleaseCommand/Steps/ChooseVersion.tsx
init_esm_shims();
var import_react4 = __toESM(require_react(), 1);

// src/components/ReleaseCommand/types.ts
init_esm_shims();

// ../../node_modules/@wix/ambassador-ctp-gradual-rollout-v1-app-config/build/es/types.impl.js
init_esm_shims();
var VersionType;
(function(VersionType2) {
  VersionType2["BUILD"] = "BUILD";
  VersionType2["MINOR"] = "MINOR";
  VersionType2["MAJOR"] = "MAJOR";
})(VersionType || (VersionType = {}));

// src/components/ReleaseCommand/types.ts
var ReleaseVersionType = ((ReleaseVersionType2) => {
  ReleaseVersionType2[ReleaseVersionType2["MINOR"] = VersionType.MINOR] = "MINOR";
  ReleaseVersionType2[ReleaseVersionType2["MAJOR"] = VersionType.MAJOR] = "MAJOR";
  return ReleaseVersionType2;
})(ReleaseVersionType || {});

// src/components/ReleaseCommand/Steps/ChooseVersion.tsx
var ChooseVersion = (props) => {
  const { nextMinor, nextMajor, onChosen } = props;
  const bi = useWixCliAppBi();
  const { t } = useTranslation();
  const options = [
    {
      title: nextMinor ? t("release_command.version.minor", {
        version: nextMinor
      }) : t("release_command.version.minor_disabled"),
      description: t("release_command.version.minor.description"),
      value: ReleaseVersionType.MINOR,
      availability: nextMinor ? SelectOptionAvailability.Enabled() : SelectOptionAvailability.Disabled({
        reason: /* @__PURE__ */ import_react4.default.createElement(
          Trans,
          {
            i18nKey: "release_command.version.minor.disabled",
            components: [
              // https://dev.wix.com/docs/build-apps/manage-your-app/versioning/about-app-versioning
              /* @__PURE__ */ import_react4.default.createElement(Link, { url: "https://wix.to/ZR80Y7p" })
            ]
          }
        )
      })
    },
    {
      title: t("release_command.version.major", {
        version: nextMajor
      }),
      description: t("release_command.version.major.description"),
      value: ReleaseVersionType.MAJOR
    }
  ];
  return /* @__PURE__ */ import_react4.default.createElement(
    SelectInput,
    {
      label: t("release_command.version_type_question"),
      options,
      initialIndex: nextMinor ? 0 : 1,
      onSubmit: ({ value }) => {
        bi.cliFlowStepAnswered({
          question: t("release_command.version_type_question"),
          questionKey: "release_command.version_type_question",
          answer: value
        });
        onChosen(value);
      }
    }
  );
};

// src/components/ReleaseCommand/Steps/ConfirmMinorVersion.tsx
init_esm_shims();
var import_react5 = __toESM(require_react(), 1);
var ConfirmMinorVersion = (props) => {
  const { onConfirmed } = props;
  const bi = useWixCliAppBi();
  const { t } = useTranslation();
  return /* @__PURE__ */ import_react5.default.createElement(Box_default, { flexDirection: "column", paddingBottom: 1 }, /* @__PURE__ */ import_react5.default.createElement(
    ConfirmInput,
    {
      label: t("release_command.confirm_version_creation"),
      initialValue: true,
      onSubmit: (confirm) => {
        bi.cliFlowStepAnswered({
          question: t("release_command.confirm_version_creation"),
          questionKey: "release_command.confirm_version_creation",
          answer: String(confirm)
        });
        if (confirm) {
          onConfirmed();
        }
      }
    }
  ));
};

// src/components/ReleaseCommand/Steps/NextSteps.tsx
init_esm_shims();
var import_react6 = __toESM(require_react(), 1);
var NextSteps = () => {
  const {
    model: {
      config: { appId }
    }
  } = useProjectModel();
  const homeUrl = `https://dev.wix.com/apps/${appId}/home?referralInfo=wix-cli`;
  const statisticsUrl = `https://dev.wix.com/apps/${appId}/statistics?referralInfo=wix-cli`;
  return /* @__PURE__ */ import_react6.default.createElement(Box_default, { flexDirection: "column", gap: 1 }, /* @__PURE__ */ import_react6.default.createElement(Text, { bold: true }, /* @__PURE__ */ import_react6.default.createElement(Trans, { i18nKey: "release_command.next_steps" })), /* @__PURE__ */ import_react6.default.createElement(
    Trans,
    {
      i18nKey: "release_command.open_stats",
      components: [/* @__PURE__ */ import_react6.default.createElement(Statistics, null), /* @__PURE__ */ import_react6.default.createElement(Link, { url: statisticsUrl })]
    }
  ), /* @__PURE__ */ import_react6.default.createElement(
    Trans,
    {
      i18nKey: "release_command.distribute",
      components: [/* @__PURE__ */ import_react6.default.createElement(Cart, null), /* @__PURE__ */ import_react6.default.createElement(Link, { url: homeUrl })]
    }
  ));
};

// src/components/ReleaseCommand/Release.tsx
var Release = ({
  baseUrl,
  siteIdOverride,
  preEnteredComment,
  preSelectedVersionType,
  onReleaseDraftCreated
}) => {
  const {
    model: {
      config: { appId }
    }
  } = useProjectModel();
  const { createDraftVersion, releaseDraftVersion } = useARMClient();
  const [tag, setTag] = (0, import_react7.useState)();
  const [comment, setComment] = (0, import_react7.useState)();
  const [versionType, setVersionType] = (0, import_react7.useState)();
  const [userShouldSelectVersion, setUserShouldSelectVersion] = (0, import_react7.useState)(false);
  const createDraftAction = useAsyncCallback(async (_, tag2) => {
    const { nextMajor, nextMinor } = await createDraftVersion(appId, tag2);
    const appWasNeverReleased = nextMajor === "1.0.0" && !nextMinor;
    const parsedNextMajor = (0, import_semver.parse)(nextMajor);
    if (parsedNextMajor == null) {
      throw new CliError({
        code: CliErrorCode.FailedToParseNextVersion({ version: nextMajor }),
        cause: void 0
      });
    }
    return {
      nextMinor,
      nextMajor,
      appWasNeverReleased,
      latestMajor: parsedNextMajor.major - 1
    };
  });
  const releaseDraftAction = useAsyncCallback(
    async (_, versionType2, comment2, latestMajor) => {
      const result = await releaseDraftVersion(appId, {
        createNewMajor: versionType2 === ReleaseVersionType.MAJOR,
        notes: comment2,
        latestMajor
      });
      onReleaseDraftCreated?.(result.releasedVersion);
      return result;
    }
  );
  function onPreviewCreated({ tag: tag2 }) {
    setTag(tag2);
    if (isString(preEnteredComment)) {
      void onReleaseCommentEntered({ tag: tag2, comment: preEnteredComment });
    }
  }
  async function onReleaseCommentEntered({
    tag: tag2,
    comment: comment2
  }) {
    setComment(comment2);
    const result = await createDraftAction.execute(tag2).catch(() => null);
    if (!result) {
      return;
    }
    const { appWasNeverReleased, nextMinor, latestMajor } = result;
    const chosenVersion = appWasNeverReleased ? ReleaseVersionType.MAJOR : preSelectedVersionType;
    if (preSelectedVersionType === ReleaseVersionType.MINOR && (appWasNeverReleased || !nextMinor)) {
      throw new CliError({
        cause: null,
        code: CliErrorCode.CannotReleaseMinorInNoninteractive()
      });
    }
    if (chosenVersion) {
      void releaseDraftAction.execute(chosenVersion, comment2, latestMajor);
    } else {
      setUserShouldSelectVersion(true);
    }
  }
  return /* @__PURE__ */ import_react7.default.createElement(import_react7.default.Fragment, null, /* @__PURE__ */ import_react7.default.createElement(
    CreatePreview,
    {
      baseUrl,
      siteIdOverride,
      onCreated: (tag2) => onPreviewCreated({ tag: tag2 })
    }
  ), tag && /* @__PURE__ */ import_react7.default.createElement(Warnings, null), tag && !isString(preEnteredComment) && /* @__PURE__ */ import_react7.default.createElement(
    ReleaseComment,
    {
      onEntered: (comment2) => onReleaseCommentEntered({ tag, comment: comment2 })
    }
  ), isString(comment) && (0, import_variant.match)(createDraftAction.status, {
    NotRequested: () => null,
    Error: () => null,
    Loading: () => /* @__PURE__ */ import_react7.default.createElement(
      Spinner,
      {
        text: /* @__PURE__ */ import_react7.default.createElement(Trans, { i18nKey: "release_command.creating_draft" })
      }
    ),
    Success: ({ result: { nextMajor, nextMinor, latestMajor } }) => /* @__PURE__ */ import_react7.default.createElement(import_react7.default.Fragment, null, userShouldSelectVersion && /* @__PURE__ */ import_react7.default.createElement(
      ChooseVersion,
      {
        nextMinor,
        nextMajor,
        onChosen: (selectedVersionType) => {
          setVersionType(selectedVersionType);
          if (selectedVersionType === ReleaseVersionType.MAJOR) {
            void releaseDraftAction.execute(
              selectedVersionType,
              comment,
              latestMajor
            );
          }
        }
      }
    ), versionType === ReleaseVersionType.MINOR && /* @__PURE__ */ import_react7.default.createElement(
      ConfirmMinorVersion,
      {
        onConfirmed: () => releaseDraftAction.execute(
          versionType,
          comment,
          latestMajor
        )
      }
    ))
  }), (0, import_variant.match)(releaseDraftAction.status, {
    NotRequested: () => null,
    Error: () => null,
    Loading: () => /* @__PURE__ */ import_react7.default.createElement(Spinner, { text: /* @__PURE__ */ import_react7.default.createElement(Trans, { i18nKey: "release_command.releasing" }) }),
    Success: ({ result: { releasedVersion } }) => /* @__PURE__ */ import_react7.default.createElement(Box_default, { flexDirection: "column", gap: 1 }, /* @__PURE__ */ import_react7.default.createElement(Alert, { type: "success" }, /* @__PURE__ */ import_react7.default.createElement(Text, { bold: true }, /* @__PURE__ */ import_react7.default.createElement(
      Trans,
      {
        i18nKey: "release_command.released",
        values: { version: releasedVersion }
      }
    ))), /* @__PURE__ */ import_react7.default.createElement(NextSteps, null))
  }));
};
function isString(value) {
  return typeof value === "string";
}

// src/components/ReleaseCommand/ReleaseCommand.tsx
var ReleaseCommand = (props) => {
  const {
    model: { projectFolder }
  } = useProjectModel();
  const { status } = useAsync(async () => {
    await assertBuildOutputExists(projectFolder);
  }, []);
  return (0, import_variant2.match)(status, {
    Error: () => null,
    Loading: () => /* @__PURE__ */ import_react8.default.createElement(Spinner, { text: /* @__PURE__ */ import_react8.default.createElement(Trans, { i18nKey: "release_command.loading_app_details" }) }),
    Success: () => /* @__PURE__ */ import_react8.default.createElement(Release, { ...props })
  });
};

export {
  ReleaseVersionType,
  ReleaseCommand
};
//# sourceMappingURL=chunk-HNIJZROR.js.map