import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { homedir } from "node:os";
import { isAbsolute, relative, resolve } from "node:path";
import { Box, Text } from "ink";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function MemoryUpdateNotification(props) {
    const width = props.width ?? 80;
    const displayPath = relativeMemoryPath(props.memoryPath, props.projectPath);
    return (_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: "memory", tone: "success" }), _jsx(Text, { color: "gray", children: " " }), _jsx(Text, { color: "gray", children: truncateCells(`updated ${displayPath}; use /memory to review or /memory add <text> to append`, Math.max(24, width - 12)) })] }));
}
export function relativeMemoryPath(memoryPath, cwd = process.cwd(), home = homedir()) {
    const absolutePath = resolve(memoryPath);
    const candidates = [
        pathInsideLabel(absolutePath, resolve(cwd), "."),
        pathInsideLabel(absolutePath, resolve(home), "~"),
    ].filter((value) => Boolean(value));
    if (candidates.length === 0)
        return memoryPath;
    return candidates.sort((left, right) => left.length - right.length)[0] ?? memoryPath;
}
function pathInsideLabel(absolutePath, basePath, label) {
    if (!isInsideOrSame(absolutePath, basePath))
        return null;
    const rel = relative(basePath, absolutePath);
    if (!rel)
        return label;
    const normalized = rel.replace(/\\/g, "/");
    return label === "." ? `./${normalized}` : `${label}/${normalized}`;
}
function isInsideOrSame(absolutePath, basePath) {
    const rel = relative(basePath, absolutePath);
    return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
}
