import { access } from 'node:fs/promises';
import { isAbsolute, relative, resolve } from 'node:path';
export class MemoryPathUtils {
    static resolveMemoryPath(memoryRoot, requestedPath) {
        if (!requestedPath.trim()) {
            return { ok: false, error: `Memory note paths must be non-empty and stay inside ${memoryRoot}.` };
        }
        const targetPath = resolve(memoryRoot, requestedPath);
        const rel = relative(memoryRoot, targetPath);
        if (rel.startsWith('..') || isAbsolute(rel)) {
            return { ok: false, error: `Memory note paths must stay inside ${memoryRoot}. Refusing to access ${targetPath}.` };
        }
        return { ok: true, path: targetPath };
    }
    static toMemoryRelativePath(memoryRoot, filePath) {
        return relative(memoryRoot, filePath) || '.';
    }
    static async pathExists(path) {
        try {
            await access(path);
            return true;
        }
        catch {
            return false;
        }
    }
    static isErrorWithCode(error, code) {
        return error instanceof Error && 'code' in error && error.code === code;
    }
}
//# sourceMappingURL=path-utils.js.map