/**
 * 路径解析工具函数
 *
 * 提供路径解析和标准化的工具函数
 */
import path from 'node:path';
export function resolveWorkspaceRoot(workspacePath) {
    if (!workspacePath || !String(workspacePath).trim()) {
        throw new Error('workspacePath is required');
    }
    return path.resolve(String(workspacePath).trim());
}
export function ensureInsideWorkspace(workspaceRoot, targetPath) {
    const resolvedTarget = path.resolve(targetPath);
    const relativePath = path.relative(workspaceRoot, resolvedTarget);
    const isOutside = relativePath.startsWith('..') || path.isAbsolute(relativePath);
    if (isOutside) {
        throw new Error(`Target path is outside workspace: ${resolvedTarget}`);
    }
    return resolvedTarget;
}
export function resolveWorkspaceFilePath(workspacePath, relativeOrAbsolutePath, defaultRelativePath) {
    const normalizedWorkspace = resolveWorkspaceRoot(workspacePath);
    const providedPathStr = relativeOrAbsolutePath
        ? String(relativeOrAbsolutePath).trim() || defaultRelativePath
        : defaultRelativePath;
    const candidatePath = path.isAbsolute(providedPathStr)
        ? providedPathStr
        : path.resolve(normalizedWorkspace, providedPathStr);
    return {
        normalizedWorkspace,
        resolvedTarget: ensureInsideWorkspace(normalizedWorkspace, candidatePath),
    };
}
/**
 * 解析属性文件的目标路径
 *
 * @param workspacePath - 工作区路径
 * @param relativeOrAbsolutePath - 相对或绝对路径
 * @returns 解析后的路径结果
 */
export function resolvePropertiesTargetPath(workspacePath, relativeOrAbsolutePath) {
    return resolveWorkspaceFilePath(workspacePath, relativeOrAbsolutePath, 'vibe-coding/my.properties');
}
