import path from 'path';
/**
 * Normalize V3 SSE / tool paths to a safe workspace-relative POSIX path.
 * Decodes vigthoria:// boundary URIs from server scrubbing before local writes.
 */
export function normalizeV3WorkspaceRelativePath(rawPath, rootPath) {
    let input = String(rawPath || '').trim().replace(/\\/g, '/').replace(/^\.\//, '');
    if (!input) {
        return '';
    }
    const safeRelative = (candidate) => {
        const stripped = String(candidate || '').replace(/^\/+/, '');
        if (!stripped) {
            return '';
        }
        if (process.platform === 'win32' && /:/.test(stripped) && !/^[a-zA-Z]:/.test(stripped)) {
            return '';
        }
        if (/^[a-zA-Z]:\//.test(stripped)) {
            return '';
        }
        const normalized = path.posix.normalize(stripped);
        if (!normalized || normalized === '.' || normalized === '..' || normalized.startsWith('../') || path.posix.isAbsolute(normalized)) {
            return '';
        }
        return normalized;
    };
    const decodeBoundaryUri = (value) => {
        const match = value.match(/^vigthoria:\/\/(?:workspace|server-internal)\/?(.*)$/i);
        if (match) {
            return safeRelative(match[1] || '');
        }
        return null;
    };
    const decoded = decodeBoundaryUri(input);
    if (decoded !== null) {
        return decoded;
    }
    if (/^vigthoria:\/\//i.test(input)) {
        return '';
    }
    const internalWorkspacePatterns = [
        /^\/?var\/www\/\.vigthoria\/v3-temp\/vig-remote-[^/]+\/(.+)$/i,
        /^\/?var\/www\/vigthoria:\/\/?server-internal\/?(.+)$/i,
        /^var\/www\/vigthoria:\/\/?server-internal\/?(.+)$/i,
        /^var\/www\/vigthoria:\/\/server-internal\/?(.+)$/i,
        /^\.vigthoria\/v3-temp\/vig-remote-[^/]+\/(.+)$/i,
        /^\/?tmp\/vig-remote(?:-server)?-[^/]+\/(.+)$/i,
        /^\/?tmp\/vig-fork-[^/]+\/(.+)$/i,
        /^\/?home\/user\/(.+)$/i,
        /^\/?root\/(.+)$/i,
    ];
    for (const pattern of internalWorkspacePatterns) {
        const match = input.match(pattern);
        if (match && match[1]) {
            return safeRelative(match[1]);
        }
    }
    const normalizedRoot = String(rootPath || '').trim().replace(/\\/g, '/').replace(/\/+$/g, '');
    if (normalizedRoot) {
        const rootNoLeadingSlash = normalizedRoot.replace(/^\//, '');
        const rootBase = path.posix.basename(normalizedRoot);
        const prefixes = [
            `${normalizedRoot}/`,
            `${rootNoLeadingSlash}/`,
            `${rootBase}/`,
        ];
        for (const prefix of prefixes) {
            if (input.startsWith(prefix)) {
                return safeRelative(input.slice(prefix.length));
            }
        }
        const embeddedRoot = `/${rootBase}/`;
        const embeddedIndex = input.indexOf(embeddedRoot);
        if (embeddedIndex >= 0) {
            return safeRelative(input.slice(embeddedIndex + embeddedRoot.length));
        }
    }
    return safeRelative(input);
}
/** Join workspace root + relative path without path.resolve URI corruption on Windows. */
export function joinV3WorkspacePath(rootPath, relativePath) {
    const segments = relativePath.split('/').filter((segment) => segment && segment !== '.');
    return path.join(rootPath, ...segments);
}
