import {
  resultFromThrowable
} from "./chunk-C7KWYVS3.js";

// src/shared/fs.ts
import { join } from "path";
import { homedir } from "os";
import * as fs from "fs";
var BASE_DIRECTORY = join(homedir(), ".agentcash");
var LEGACY_DIRECTORY = join(homedir(), ".x402scan-mcp");
function migrateFromLegacy() {
  if (!fs.existsSync(LEGACY_DIRECTORY)) return;
  const result = resultFromThrowable(
    "migration",
    "fs",
    () => {
      const files = fs.readdirSync(LEGACY_DIRECTORY);
      for (const file of files) {
        if (file.endsWith(".log")) continue;
        const src = join(LEGACY_DIRECTORY, file);
        const dest = join(BASE_DIRECTORY, file);
        if (!fs.existsSync(dest) && fs.statSync(src).isFile()) {
          fs.copyFileSync(src, dest);
          const { mode } = fs.statSync(src);
          fs.chmodSync(dest, mode);
        }
      }
    },
    (e) => ({
      cause: "migration_failed",
      message: `Failed to migrate: ${e instanceof Error ? e.message : String(e)}`
    })
  );
  if (result.isErr()) {
    console.error(
      `[agentcash] Warning: failed to migrate files from ${LEGACY_DIRECTORY} to ${BASE_DIRECTORY}. Your wallet may still be at ${join(LEGACY_DIRECTORY, "wallet.json")} \u2014 copy it manually to ${join(BASE_DIRECTORY, "wallet.json")}. Error: ${result.error.message}`
    );
  }
}
var initialized = false;
function ensureBaseDir() {
  if (initialized) return;
  if (!fs.existsSync(BASE_DIRECTORY)) {
    fs.mkdirSync(BASE_DIRECTORY, { recursive: true });
  }
  migrateFromLegacy();
  initialized = true;
}
var configFile = (name) => {
  ensureBaseDir();
  const filePath = join(BASE_DIRECTORY, name);
  return filePath;
};

export {
  configFile
};
//# sourceMappingURL=chunk-VDBUUHJF.js.map