import { isAbsolute, relative } from 'node:path';
function stripTrailingSlash(value) {
    if (value.length > 1 && value.endsWith('/')) {
        return value.slice(0, -1);
    }
    return value;
}
export function computeClaudeCwd(input) {
    const localPath = stripTrailingSlash(input.localPath);
    const gitRoot = stripTrailingSlash(input.gitRoot);
    const worktreePath = stripTrailingSlash(input.worktreePath);
    const subPath = relative(gitRoot, localPath);
    if (subPath === '') {
        return worktreePath;
    }
    if (subPath.startsWith('..') || isAbsolute(subPath)) {
        throw new Error(`localPath ${input.localPath} is not inside gitRoot ${input.gitRoot}`);
    }
    return `${worktreePath}/${subPath}`;
}
export async function resolveClaudeCwd(input) {
    const result = await input.executor.execute({
        kind: 'rev-parse-toplevel',
        args: ['rev-parse', '--show-toplevel'],
        cwd: input.localPath,
    });
    if (result.exitCode !== 0) {
        return input.worktreePath;
    }
    const gitRoot = result.stdout.trim();
    if (gitRoot.length === 0) {
        return input.worktreePath;
    }
    try {
        return computeClaudeCwd({
            localPath: input.localPath,
            gitRoot,
            worktreePath: input.worktreePath,
        });
    }
    catch {
        return input.worktreePath;
    }
}
//# sourceMappingURL=claudeCwd.js.map