import { atomicWrite, ensureDir, readTextOrEmpty } from "../fs-utils.js";
import { MEMORY_DIR, MEMORY_FILE, USER_FILE } from "../paths.js";
const MAX_BYTES = {
    memory: 4096,
    user: 2048,
};
function fileFor(store) {
    return store === "memory" ? MEMORY_FILE : USER_FILE;
}
export async function readMemory(store) {
    return await readTextOrEmpty(fileFor(store));
}
export async function writeMemory(store, content) {
    await ensureDir(MEMORY_DIR);
    const trimmed = content.replace(/\s+$/g, "") + "\n";
    if (Buffer.byteLength(trimmed, "utf8") > MAX_BYTES[store]) {
        throw new Error(`memory "${store}" exceeds ${MAX_BYTES[store]} bytes. Trim or split entries.`);
    }
    await atomicWrite(fileFor(store), trimmed);
}
export async function appendMemory(store, entry) {
    const current = await readMemory(store);
    const sep = current && !current.endsWith("\n") ? "\n" : "";
    await writeMemory(store, `${current}${sep}- ${entry.trim()}`);
}
export async function replaceInMemory(store, oldString, newString) {
    const current = await readMemory(store);
    if (!current.includes(oldString)) {
        throw new Error(`old_string not found in ${store} memory`);
    }
    const occurrences = current.split(oldString).length - 1;
    if (occurrences > 1) {
        throw new Error(`old_string matches ${occurrences} places in ${store} memory. Add more context.`);
    }
    await writeMemory(store, current.replace(oldString, newString));
}
export async function removeFromMemory(store, fragment) {
    const current = await readMemory(store);
    const lines = current.split(/\r?\n/);
    const kept = lines.filter((line) => !line.includes(fragment));
    if (kept.length === lines.length) {
        throw new Error(`fragment not found in ${store} memory`);
    }
    await writeMemory(store, kept.join("\n"));
}
//# sourceMappingURL=store.js.map