// Force strict mode and setup for ESM
"use strict";
import {
  writeStderrLine
} from "./chunk-YUZI3WAC.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/utils/update-relaunch.ts
init_esbuild_shims();
var UPDATE_CHECK_FAILED_MESSAGE = "Failed to check for updates. Please check your network or registry configuration.";
var UPDATE_FAILED_MESSAGE = "Automatic update failed. Please try updating manually.";
async function updateBeforeRelaunch(settings, projectRoot) {
  let translate = /* @__PURE__ */ __name((message) => message, "translate");
  try {
    const [
      { checkForUpdatesDetailed },
      { handleAutoUpdate },
      { getInstallationInfo },
      { performStandaloneUpdate },
      { t }
    ] = await Promise.all([
      import("./updateCheck-BOQAQIBZ.js"),
      import("./handleAutoUpdate-CDMSRDZJ.js"),
      import("./installationInfo-RNTTABWP.js"),
      import("./standalone-update-436CKG2F.js"),
      import("./i18n-CAH3TXMB.js")
    ]);
    translate = t;
    const result = await checkForUpdatesDetailed();
    if (result.status === "update") {
      writeStderrLine(result.info.message);
      const installationInfo = getInstallationInfo(projectRoot, true);
      if (installationInfo.isStandalone && installationInfo.standaloneDir) {
        const standaloneResult = await performStandaloneUpdate(
          installationInfo.standaloneDir,
          result.info.update.latest
        );
        writeStderrLine(
          t(
            standaloneResult === "deferred" ? "Update downloaded. It will be applied after you exit this session." : "Update successful! The new version will be used on your next run."
          )
        );
        return standaloneResult !== "deferred";
      }
      if (!installationInfo.updateCommand) {
        writeStderrLine(
          installationInfo.updateMessage ?? t("Manual update required. Please reinstall Qwen Code.")
        );
        return false;
      }
      const updateProcess = handleAutoUpdate(
        result.info,
        settings,
        projectRoot
      );
      const success = updateProcess ? await new Promise((resolve) => {
        updateProcess.once("close", (code) => resolve(code === 0));
        updateProcess.once("error", () => resolve(false));
      }) : false;
      writeStderrLine(
        t(
          success ? "Update successful! The new version will be used on your next run." : UPDATE_FAILED_MESSAGE
        )
      );
      return success;
    } else if (result.status === "error") {
      writeStderrLine(t(UPDATE_CHECK_FAILED_MESSAGE));
    }
  } catch {
    writeStderrLine(translate(UPDATE_FAILED_MESSAGE));
  }
  return false;
}
__name(updateBeforeRelaunch, "updateBeforeRelaunch");
export {
  updateBeforeRelaunch
};
/**
 * @license
 * Copyright 2026 Qwen Code
 * SPDX-License-Identifier: Apache-2.0
 */
