// Force strict mode and setup for ESM
"use strict";
import {
  QWEN_DIR,
  Storage,
  resolvePath,
  sanitizeCwd
} from "./chunk-R5PDRHEF.js";
import {
  init_esbuild_shims
} from "./chunk-A4BMJM77.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/memory/paths.ts
init_esbuild_shims();
import * as fs from "node:fs";
import * as path from "node:path";
var AUTO_MEMORY_DIRNAME = "memory";
var AUTO_MEMORY_INDEX_FILENAME = "MEMORY.md";
var AUTO_MEMORY_METADATA_FILENAME = "meta.json";
var AUTO_MEMORY_EXTRACT_CURSOR_FILENAME = "extract-cursor.json";
var AUTO_MEMORY_CONSOLIDATION_LOCK_FILENAME = "consolidation.lock";
var USER_AUTO_MEMORY_DIRNAME = "memories";
function findGitRoot(startPath) {
  let current = path.resolve(startPath);
  while (true) {
    const gitPath = path.join(current, ".git");
    if (fs.existsSync(gitPath)) {
      return current;
    }
    const parent = path.dirname(current);
    if (parent === current) {
      return null;
    }
    current = parent;
  }
}
__name(findGitRoot, "findGitRoot");
function findCanonicalGitRoot(startPath) {
  const gitRoot = findGitRoot(startPath);
  if (!gitRoot) {
    return null;
  }
  try {
    const gitContent = fs.readFileSync(path.join(gitRoot, ".git"), "utf-8").trim();
    if (!gitContent.startsWith("gitdir:")) {
      return gitRoot;
    }
    const worktreeGitDir = path.resolve(
      gitRoot,
      gitContent.slice("gitdir:".length).trim()
    );
    const commonDir = path.resolve(
      worktreeGitDir,
      fs.readFileSync(path.join(worktreeGitDir, "commondir"), "utf-8").trim()
    );
    if (path.resolve(path.dirname(worktreeGitDir)) !== path.join(commonDir, "worktrees")) {
      return gitRoot;
    }
    const backlink = fs.realpathSync(
      fs.readFileSync(path.join(worktreeGitDir, "gitdir"), "utf-8").trim()
    );
    if (backlink !== path.join(fs.realpathSync(gitRoot), ".git")) {
      return gitRoot;
    }
    if (path.basename(commonDir) !== ".git") {
      return commonDir.normalize("NFC");
    }
    return path.dirname(commonDir).normalize("NFC");
  } catch {
    return gitRoot;
  }
}
__name(findCanonicalGitRoot, "findCanonicalGitRoot");
function getMemoryBaseDir() {
  if (process.env["QWEN_CODE_MEMORY_BASE_DIR"]) {
    return resolvePath(void 0, process.env["QWEN_CODE_MEMORY_BASE_DIR"]);
  }
  return Storage.getRuntimeBaseDir();
}
__name(getMemoryBaseDir, "getMemoryBaseDir");
var _autoMemoryRootCache = /* @__PURE__ */ new Map();
function getAutoMemoryRoot(projectRoot) {
  const useLocalMemory = process.env["QWEN_CODE_MEMORY_LOCAL"] === "1";
  const memoryBaseDir = useLocalMemory ? "" : getMemoryBaseDir();
  const cacheKey = `${useLocalMemory ? "local" : memoryBaseDir}\0${projectRoot}`;
  const cached = _autoMemoryRootCache.get(cacheKey);
  if (cached !== void 0) return cached;
  let result;
  if (useLocalMemory) {
    result = path.join(projectRoot, QWEN_DIR, AUTO_MEMORY_DIRNAME);
  } else {
    const canonicalRoot = findCanonicalGitRoot(projectRoot) ?? path.resolve(projectRoot);
    result = path.join(
      memoryBaseDir,
      "projects",
      sanitizeCwd(canonicalRoot),
      AUTO_MEMORY_DIRNAME
    );
  }
  _autoMemoryRootCache.set(cacheKey, result);
  return result;
}
__name(getAutoMemoryRoot, "getAutoMemoryRoot");
function clearAutoMemoryRootCache() {
  _autoMemoryRootCache.clear();
}
__name(clearAutoMemoryRootCache, "clearAutoMemoryRootCache");
function getAutoMemoryProjectStateDir(projectRoot) {
  return path.dirname(getAutoMemoryRoot(projectRoot));
}
__name(getAutoMemoryProjectStateDir, "getAutoMemoryProjectStateDir");
function isAutoMemPath(absolutePath, projectRoot) {
  const normalizedPath = path.normalize(absolutePath);
  const memRoot = path.normalize(getAutoMemoryRoot(projectRoot));
  const rel = path.relative(memRoot, normalizedPath);
  return rel === "" || !rel.startsWith("..") && !path.isAbsolute(rel);
}
__name(isAutoMemPath, "isAutoMemPath");
function getAutoMemoryIndexPath(projectRoot) {
  return path.join(getAutoMemoryRoot(projectRoot), AUTO_MEMORY_INDEX_FILENAME);
}
__name(getAutoMemoryIndexPath, "getAutoMemoryIndexPath");
function getAutoMemoryMetadataPath(projectRoot) {
  return path.join(
    getAutoMemoryProjectStateDir(projectRoot),
    AUTO_MEMORY_METADATA_FILENAME
  );
}
__name(getAutoMemoryMetadataPath, "getAutoMemoryMetadataPath");
function getAutoMemoryExtractCursorPath(projectRoot) {
  return path.join(
    getAutoMemoryProjectStateDir(projectRoot),
    AUTO_MEMORY_EXTRACT_CURSOR_FILENAME
  );
}
__name(getAutoMemoryExtractCursorPath, "getAutoMemoryExtractCursorPath");
function getAutoMemoryConsolidationLockPath(projectRoot) {
  return path.join(
    getAutoMemoryProjectStateDir(projectRoot),
    AUTO_MEMORY_CONSOLIDATION_LOCK_FILENAME
  );
}
__name(getAutoMemoryConsolidationLockPath, "getAutoMemoryConsolidationLockPath");
function getAutoMemoryTopicFilename(type) {
  return `${type}.md`;
}
__name(getAutoMemoryTopicFilename, "getAutoMemoryTopicFilename");
function getAutoMemoryTopicPath(projectRoot, type) {
  return path.join(
    getAutoMemoryRoot(projectRoot),
    getAutoMemoryTopicFilename(type)
  );
}
__name(getAutoMemoryTopicPath, "getAutoMemoryTopicPath");
function getAutoMemoryFilePath(projectRoot, relativePath) {
  return path.join(getAutoMemoryRoot(projectRoot), relativePath);
}
__name(getAutoMemoryFilePath, "getAutoMemoryFilePath");
function getUserAutoMemoryRoot() {
  return path.join(getMemoryBaseDir(), USER_AUTO_MEMORY_DIRNAME);
}
__name(getUserAutoMemoryRoot, "getUserAutoMemoryRoot");
function getUserAutoMemoryIndexPath() {
  return path.join(getUserAutoMemoryRoot(), AUTO_MEMORY_INDEX_FILENAME);
}
__name(getUserAutoMemoryIndexPath, "getUserAutoMemoryIndexPath");
function getUserAutoMemoryTopicPath(type) {
  return path.join(getUserAutoMemoryRoot(), getAutoMemoryTopicFilename(type));
}
__name(getUserAutoMemoryTopicPath, "getUserAutoMemoryTopicPath");
function isUserAutoMemPath(absolutePath) {
  const normalizedPath = path.normalize(absolutePath);
  const memRoot = path.normalize(getUserAutoMemoryRoot());
  const rel = path.relative(memRoot, normalizedPath);
  return rel === "" || !rel.startsWith("..") && !path.isAbsolute(rel);
}
__name(isUserAutoMemPath, "isUserAutoMemPath");
function isAnyAutoMemPath(absolutePath, projectRoot) {
  return isAutoMemPath(absolutePath, projectRoot) || isUserAutoMemPath(absolutePath);
}
__name(isAnyAutoMemPath, "isAnyAutoMemPath");

export {
  AUTO_MEMORY_DIRNAME,
  AUTO_MEMORY_INDEX_FILENAME,
  AUTO_MEMORY_METADATA_FILENAME,
  AUTO_MEMORY_EXTRACT_CURSOR_FILENAME,
  AUTO_MEMORY_CONSOLIDATION_LOCK_FILENAME,
  USER_AUTO_MEMORY_DIRNAME,
  getMemoryBaseDir,
  getAutoMemoryRoot,
  clearAutoMemoryRootCache,
  getAutoMemoryProjectStateDir,
  isAutoMemPath,
  getAutoMemoryIndexPath,
  getAutoMemoryMetadataPath,
  getAutoMemoryExtractCursorPath,
  getAutoMemoryConsolidationLockPath,
  getAutoMemoryTopicFilename,
  getAutoMemoryTopicPath,
  getAutoMemoryFilePath,
  getUserAutoMemoryRoot,
  getUserAutoMemoryIndexPath,
  getUserAutoMemoryTopicPath,
  isUserAutoMemPath,
  isAnyAutoMemPath
};
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
