// Force strict mode and setup for ESM
"use strict";
import {
  QWEN_DIR,
  Storage,
  sanitizeCwd
} from "./chunk-XP27SJMH.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";
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 process.env["QWEN_CODE_MEMORY_BASE_DIR"];
  }
  return Storage.getGlobalQwenDir();
}
__name(getMemoryBaseDir, "getMemoryBaseDir");
var _autoMemoryRootCache = /* @__PURE__ */ new Map();
function getAutoMemoryRoot(projectRoot) {
  const cached = _autoMemoryRootCache.get(projectRoot);
  if (cached !== void 0) return cached;
  let result;
  if (process.env["QWEN_CODE_MEMORY_LOCAL"] === "1") {
    result = path.join(projectRoot, QWEN_DIR, AUTO_MEMORY_DIRNAME);
  } else {
    const canonicalRoot = findCanonicalGitRoot(projectRoot) ?? path.resolve(projectRoot);
    result = path.join(
      getMemoryBaseDir(),
      "projects",
      sanitizeCwd(canonicalRoot),
      AUTO_MEMORY_DIRNAME
    );
  }
  _autoMemoryRootCache.set(projectRoot, 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");

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