import { fmt } from './chunk-NEWKASA7.js';
import { gracefulExit } from './chunk-XQSWW35O.js';
import { define } from './chunk-WIL4LMMA.js';
import { CliExitError } from './chunk-7M6WZ65V.js';
import { getUserVarlockDir } from './chunk-JE2IMKMO.js';
import { __name } from './chunk-IBAHCH6O.js';
import { join } from 'path';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { existsSync } from 'fs';

var commandSpec = define({
  name: "telemetry",
  description: "Enable/disable anonymous usage analytics",
  args: {
    mode: {
      type: "positional",
      description: '"enable" or "disable"'
    }
  },
  examples: `
Opts in/out of anonymous usage analytics. This command creates/updates a configuration
file at $XDG_CONFIG_HOME/varlock/config.json (or ~/.config/varlock/config.json) saving
your preference.

Examples:
  varlock telemetry disable    # Opt out of telemetry
  varlock telemetry enable     # Opt in to telemetry

\u{1F4A1} Tip: You can also temporarily opt out by setting VARLOCK_TELEMETRY_DISABLED=1
For more information, visit https://varlock.dev/guides/telemetry/
  `.trim()
});
var commandFn = /* @__PURE__ */ __name(async (ctx) => {
  if (!["enable", "disable"].includes(ctx.values.mode)) {
    throw new CliExitError('additional arg must be "enable" or "disable"', {
      forceExit: true
    });
  }
  const configDir = getUserVarlockDir();
  const configPath = join(configDir, "config.json");
  try {
    if (!existsSync(configDir)) {
      await mkdir(configDir, { recursive: true });
    }
    let config = {};
    if (existsSync(configPath)) {
      const configContent = await readFile(configPath, "utf-8");
      config = JSON.parse(configContent);
    }
    if (ctx.values.mode === "disable") config.telemetryDisabled = true;
    else delete config.telemetryDisabled;
    await writeFile(configPath, JSON.stringify(config, null, 2));
    if (ctx.values.mode === "enable") {
      console.log("\u2705 Successfully enabled anonymous usage analytics");
    } else {
      console.log("\u2705 Successfully disabled anonymous usage analytics");
    }
    console.log("> saved in:", fmt.filePath(configPath));
  } catch (error) {
    console.error("Failed to opt out of analytics:", error);
    return gracefulExit(1);
  }
}, "commandFn");

export { commandFn, commandSpec };
//# sourceMappingURL=chunk-HRKY75S6.js.map
//# sourceMappingURL=chunk-HRKY75S6.js.map