import {
  getOutputUpdateCLIReminder
} from "../chunk-G3BEDHX2.js";
import {
  startAnalytics
} from "../chunk-OWRIOLFJ.js";
import "../chunk-4YRKVURS.js";
import "../chunk-KLJ2TVHE.js";
import "../chunk-VSLR7ET4.js";
import {
  checkForCachedNewVersion,
  checkForNewVersion,
  runAtMinimumInterval
} from "../chunk-HJBER2P6.js";
import "../chunk-BYPQXSAL.js";
import {
  CLI_KIT_VERSION
} from "../chunk-DD26C7GE.js";
import "../chunk-NLUGD7PZ.js";
import {
  initDemoRecorder,
  outputDebug,
  outputWarn
} from "../chunk-LA4HX5WI.js";
import "../chunk-XVNW332R.js";
import "../chunk-XEDHKQZA.js";
import "../chunk-T2GATXSP.js";
import "../chunk-CBD6UXLA.js";
import "../chunk-CBBS4CV7.js";
import "../chunk-SNOECVP4.js";
import "../chunk-NEOIQ6PN.js";
import "../chunk-7Q3MMWAC.js";
import "../chunk-MX4RJDBM.js";
import "../chunk-NB4NLOEJ.js";
import "../chunk-UBB7JKND.js";
import "../chunk-522OB3EU.js";
import "../chunk-PNN7RS7Y.js";
import {
  init_cjs_shims
} from "../chunk-POZ5MGPT.js";

// src/hooks/prerun.ts
init_cjs_shims();

// ../cli-kit/dist/public/node/hooks/prerun.js
init_cjs_shims();
var hook = async (options) => {
  initDemoRecorder();
  let commandContent = parseCommandContent({
    id: options.Command.id,
    aliases: options.Command.aliases,
    pluginAlias: options.Command.plugin?.alias
  }), args = options.argv;
  await warnOnAvailableUpgrade(), outputDebug(`Running command ${commandContent.command}`), await startAnalytics({ commandContent, args, commandClass: options.Command });
};
function parseCommandContent(cmdInfo) {
  let commandContent = parseCreateCommand(cmdInfo.pluginAlias);
  return commandContent || (commandContent = parseNormalCommand(cmdInfo.id, cmdInfo.aliases)), commandContent;
}
function parseNormalCommand(id, aliases) {
  return {
    command: id.replace(/:/g, " "),
    topic: parseTopic(id),
    alias: findAlias(aliases)
  };
}
function parseCreateCommand(pluginAlias) {
  if (pluginAlias?.startsWith("@shopify/create-"))
    return { command: pluginAlias.substring(pluginAlias.indexOf("/") + 1) };
}
function parseTopic(cmd) {
  if (cmd.lastIndexOf(":") !== -1)
    return cmd.slice(0, cmd.lastIndexOf(":")).replace(/:/g, " ");
}
function findAlias(aliases) {
  let existingAlias = aliases.find((alias) => alias.split(":").every((aliasToken) => process.argv.includes(aliasToken)));
  if (existingAlias)
    return existingAlias.replace(/:/g, " ");
}
async function warnOnAvailableUpgrade() {
  let cliDependency = "@shopify/cli", currentVersion = CLI_KIT_VERSION;
  currentVersion.startsWith("0.0.0") || (checkForNewVersion(cliDependency, currentVersion, { cacheExpiryInHours: 24 }), await runAtMinimumInterval("warn-on-available-upgrade", { days: 1 }, async () => {
    let newerVersion = checkForCachedNewVersion(cliDependency, currentVersion);
    newerVersion && outputWarn(getOutputUpdateCLIReminder(newerVersion));
  }));
}
export {
  hook as default
};
//# sourceMappingURL=prerun.js.map
