import {
  LUMENFLOW_MEMORY_PATHS
} from "./chunk-4N74J3UT.js";

// ../memory/dist/mem-init-core.js
import fs from "fs/promises";
import path from "path";
import yaml from "yaml";
var MEMORY_PATHS = {
  /** Memory directory relative to project root */
  MEMORY_DIR: LUMENFLOW_MEMORY_PATHS.MEMORY_DIR,
  /** Memory JSONL file name */
  MEMORY_FILE: "memory.jsonl",
  /** Config YAML file name */
  CONFIG_FILE: "config.yaml"
};
var DEFAULT_CONFIG = {
  /** Config schema version */
  version: 1,
  /** Retention policy for each lifecycle */
  retention: {
    /** Ephemeral nodes are discarded immediately after use */
    ephemeral: 0,
    /** Session nodes expire after 1 hour */
    session: 3600,
    /** WU nodes expire after 7 days */
    wu: 604800,
    /** Project nodes never expire (-1 = infinite) */
    project: -1
  }
};
async function fileExists(filePath) {
  try {
    await fs.access(filePath);
    return true;
  } catch {
    return false;
  }
}
function generateConfigYaml() {
  const header = "# Memory layer configuration\n# Generated by pnpm mem:init (WU-1464)\n";
  return header + yaml.stringify(DEFAULT_CONFIG);
}
async function initMemory(baseDir) {
  const memoryDir = path.join(baseDir, MEMORY_PATHS.MEMORY_DIR);
  const memoryJsonlPath = path.join(memoryDir, MEMORY_PATHS.MEMORY_FILE);
  const configYamlPath = path.join(memoryDir, MEMORY_PATHS.CONFIG_FILE);
  const result = {
    success: false,
    alreadyInitialized: false,
    paths: {
      memoryDir,
      memoryJsonl: memoryJsonlPath,
      configYaml: configYamlPath
    },
    created: {
      directory: false,
      memoryJsonl: false,
      configYaml: false
    }
  };
  const dirExists = await fileExists(memoryDir);
  const memoryExists = await fileExists(memoryJsonlPath);
  const configExists = await fileExists(configYamlPath);
  if (dirExists && memoryExists && configExists) {
    result.alreadyInitialized = true;
  }
  if (!dirExists) {
    await fs.mkdir(memoryDir, { recursive: true });
    result.created.directory = true;
  }
  if (!memoryExists) {
    await fs.writeFile(memoryJsonlPath, "", { encoding: "utf-8" });
    result.created.memoryJsonl = true;
  }
  if (!configExists) {
    const configContent = generateConfigYaml();
    await fs.writeFile(configYamlPath, configContent, { encoding: "utf-8" });
    result.created.configYaml = true;
  }
  result.success = true;
  return result;
}

export {
  initMemory
};
