// Force strict mode and setup for ESM
"use strict";
import {
  getInstallationInfo,
  resolveUpdateCommand
} from "./chunk-64TN6SIX.js";
import {
  performStandaloneUpdate
} from "./chunk-SL7MFP6Y.js";
import {
  updateEventEmitter
} from "./chunk-YAF2MQZ6.js";
import {
  t
} from "./chunk-XP6JY4LW.js";
import {
  createDebugLogger
} from "./chunk-7NFB7LBQ.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/utils/handleAutoUpdate.ts
init_esbuild_shims();

// packages/cli/src/utils/spawnWrapper.ts
init_esbuild_shims();
import { spawn } from "node:child_process";
var spawnWrapper = spawn;

// packages/cli/src/utils/handleAutoUpdate.ts
import os from "node:os";
var debugLogger = createDebugLogger("AUTO_UPDATE");
var UPDATE_SUCCESS_MESSAGE = "Update successful! Please restart Qwen Code to use the new version. Switching model providers before restarting may not work correctly.";
var UPDATE_FAILED_MESSAGE = "Automatic update failed. Please try updating manually.";
function handleAutoUpdate(info, settings, projectRoot, spawnFn = spawnWrapper) {
  if (!info) {
    return;
  }
  const isAutoUpdateEnabled = settings.merged.general?.enableAutoUpdate !== false;
  const installationInfo = getInstallationInfo(
    projectRoot,
    isAutoUpdateEnabled
  );
  let combinedMessage = info.message;
  if (installationInfo.updateMessage) {
    combinedMessage += `
${installationInfo.updateMessage}`;
  }
  updateEventEmitter.emit("update-received", {
    message: combinedMessage
  });
  if (installationInfo.isStandalone && installationInfo.standaloneDir && isAutoUpdateEnabled) {
    performStandaloneUpdate(installationInfo.standaloneDir, info.update.latest).then((result) => {
      const message = result === "deferred" ? t(
        "Update downloaded. It will be applied after you exit this session."
      ) : t(
        "Update successful! The new version will be used on your next run."
      );
      updateEventEmitter.emit("update-success", { message });
    }).catch((err) => {
      updateEventEmitter.emit("update-failed", {
        message: t(
          "Automatic update failed: {{error}}. Re-run the installer to update manually.",
          { error: err.message }
        )
      });
    });
    return;
  }
  if (!installationInfo.updateCommand || !isAutoUpdateEnabled) {
    return;
  }
  const updateCommand = resolveUpdateCommand(
    installationInfo.updateCommand,
    info.update.latest
  );
  const isWindows = os.platform() === "win32";
  const shell = isWindows ? "cmd.exe" : "bash";
  const shellArgs = isWindows ? ["/c", updateCommand] : ["-c", updateCommand];
  const updateProcess = spawnFn(shell, shellArgs, {
    stdio: ["pipe", "ignore", "pipe"]
  });
  let errorOutput = "";
  updateProcess.stderr.on("data", (data) => {
    errorOutput += data.toString();
  });
  updateProcess.on("close", (code) => {
    if (code === 0) {
      updateEventEmitter.emit("update-success", {
        message: t(UPDATE_SUCCESS_MESSAGE)
      });
    } else {
      debugLogger.warn(
        `Automatic update command failed: ${updateCommand}; stderr: ${errorOutput.trim()}`
      );
      updateEventEmitter.emit("update-failed", {
        message: t(UPDATE_FAILED_MESSAGE)
      });
    }
  });
  updateProcess.on("error", (err) => {
    debugLogger.warn("Automatic update command failed to start:", err);
    updateEventEmitter.emit("update-failed", {
      message: t(UPDATE_FAILED_MESSAGE)
    });
  });
  return updateProcess;
}
__name(handleAutoUpdate, "handleAutoUpdate");
function setUpdateHandler(addItem, setUpdateInfo, isIdleRef = { current: true }) {
  let successfullyInstalled = false;
  const pendingNotifications = [];
  const addItemOrDefer = /* @__PURE__ */ __name((item) => {
    if (isIdleRef.current) {
      addItem(item, Date.now());
    } else {
      pendingNotifications.push(item);
    }
  }, "addItemOrDefer");
  const handleUpdateRecieved = /* @__PURE__ */ __name((info) => {
    setUpdateInfo(info);
    const savedMessage = info.message;
    setTimeout(() => {
      if (!successfullyInstalled) {
        addItemOrDefer({
          type: "info" /* INFO */,
          text: savedMessage
        });
      }
      setUpdateInfo(null);
    }, 6e4);
  }, "handleUpdateRecieved");
  const handleUpdateFailed = /* @__PURE__ */ __name((data) => {
    setUpdateInfo(null);
    addItemOrDefer({
      type: "error" /* ERROR */,
      text: data?.message ?? t(UPDATE_FAILED_MESSAGE)
    });
  }, "handleUpdateFailed");
  const handleUpdateSuccess = /* @__PURE__ */ __name((data) => {
    successfullyInstalled = true;
    setUpdateInfo(null);
    addItemOrDefer({
      type: "info" /* INFO */,
      text: data?.message ?? t(UPDATE_SUCCESS_MESSAGE)
    });
  }, "handleUpdateSuccess");
  const handleUpdateInfo = /* @__PURE__ */ __name((data) => {
    addItemOrDefer({
      type: "info" /* INFO */,
      text: data.message
    });
  }, "handleUpdateInfo");
  updateEventEmitter.on("update-received", handleUpdateRecieved);
  updateEventEmitter.on("update-failed", handleUpdateFailed);
  updateEventEmitter.on("update-success", handleUpdateSuccess);
  updateEventEmitter.on("update-info", handleUpdateInfo);
  const cleanup = /* @__PURE__ */ __name(() => {
    updateEventEmitter.off("update-received", handleUpdateRecieved);
    updateEventEmitter.off("update-failed", handleUpdateFailed);
    updateEventEmitter.off("update-success", handleUpdateSuccess);
    updateEventEmitter.off("update-info", handleUpdateInfo);
    pendingNotifications.length = 0;
  }, "cleanup");
  const flush = /* @__PURE__ */ __name(() => {
    while (pendingNotifications.length > 0) {
      const item = pendingNotifications.shift();
      addItem(item, Date.now());
    }
  }, "flush");
  return { cleanup, flush };
}
__name(setUpdateHandler, "setUpdateHandler");

export {
  handleAutoUpdate,
  setUpdateHandler
};
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
