import { isAbsolute, join, resolve } from 'node:path';
function requireAbsoluteOwnedRoot(path, name) {
    const trimmed = path.trim();
    if (!trimmed) {
        throw new Error(`ShellPathService ${name} must be a non-empty absolute path.`);
    }
    if (!isAbsolute(trimmed)) {
        throw new Error(`ShellPathService ${name} must be an absolute path.`);
    }
    return resolve(trimmed);
}
export function createShellPathService(options) {
    const workingDirectory = requireAbsoluteOwnedRoot(options.workingDirectory, 'workingDirectory');
    const homeDirectory = requireAbsoluteOwnedRoot(options.homeDirectory, 'homeDirectory');
    const projectGoodVibesRoot = join(workingDirectory, '.goodvibes');
    const userGoodVibesRoot = join(homeDirectory, '.goodvibes');
    const expandHomePath = (path) => {
        if (path === '~')
            return homeDirectory;
        if (path.startsWith('~/')) {
            return join(homeDirectory, path.slice(2));
        }
        return path;
    };
    const resolveWorkspacePath = (path) => {
        const normalized = expandHomePath(path);
        return resolve(workingDirectory, normalized);
    };
    const isWithinWorkingDirectory = (path) => {
        const resolved = resolveWorkspacePath(path);
        const prefix = workingDirectory.endsWith('/') ? workingDirectory : `${workingDirectory}/`;
        return resolved === workingDirectory || resolved.startsWith(prefix);
    };
    return {
        workingDirectory,
        homeDirectory,
        projectGoodVibesRoot,
        userGoodVibesRoot,
        expandHomePath,
        resolveWorkspacePath,
        resolveProjectPath: (...segments) => join(projectGoodVibesRoot, ...segments),
        resolveUserPath: (...segments) => join(userGoodVibesRoot, ...segments),
        isWithinWorkingDirectory,
    };
}
