window.switchVersion = function(targetVersion) {
    const currentUrl = window.location.href;
    const currentPath = window.location.pathname;
    const hash = window.location.hash;
    
    if (currentUrl.includes('#/')) {
        const hashMatch = currentUrl.match(/#\/([^\/]+)\/([^\/]+)\/(.*)/);
        if (hashMatch) {
            const project = hashMatch[1];
            const docId = hashMatch[2];
            const chapter = hashMatch[3] || '';
            
            if (targetVersion === 'web') {
                window.location.href = '/docs/' + project + '/' + docId + '/web/index.html';
            } else {
                window.location.href = '/docs/' + project + '/' + docId + '/index.html';
            }
            return;
        }
        
        const simpleHashMatch = currentUrl.match(/#\/([^\/]+)\/([^\/]+)/);
        if (simpleHashMatch) {
            const project = simpleHashMatch[1];
            const docId = simpleHashMatch[2];
            
            if (targetVersion === 'web') {
                window.location.href = '/docs/' + project + '/' + docId + '/web/index.html';
            } else {
                window.location.href = '/docs/' + project + '/' + docId + '/index.html';
            }
            return;
        }
    }
    
    const docsPattern = /\/docs\/([^\/]+)\/([^\/]+)/;
    const docsMatch = currentPath.match(docsPattern);
    if (docsMatch) {
        const project = docsMatch[1];
        const docId = docsMatch[2];
        
        if (targetVersion === 'web') {
            window.location.href = '/docs/' + project + '/' + docId + '/web/index.html';
        } else {
            window.location.href = '/docs/' + project + '/' + docId + '/index.html';
        }
        return;
    }
    
    const helpIndex = currentPath.indexOf('/help/');
    if (helpIndex !== -1) {
        const baseUrl = currentPath.substring(0, helpIndex + 6);
        if (targetVersion === 'web') {
            window.location.href = baseUrl + 'web/index.html';
        } else {
            window.location.href = baseUrl + 'index.html';
        }
        return;
    }
    
    const manualsIndex = currentPath.indexOf('/manuals/');
    if (manualsIndex !== -1) {
        const baseUrl = currentPath.substring(0, manualsIndex + 9);
        if (targetVersion === 'web') {
            window.location.href = baseUrl + 'web/index.html';
        } else {
            window.location.href = baseUrl + 'index.html';
        }
        return;
    }
    
    const htmlIndex = currentPath.indexOf('/html/');
    if (htmlIndex !== -1) {
        const baseUrl = currentPath.substring(0, htmlIndex + 6);
        if (targetVersion === 'web') {
            window.location.href = baseUrl + 'web/index.html';
        } else {
            window.location.href = baseUrl + 'index.html';
        }
        return;
    }
    
    const parts = currentPath.split('/').filter(p => p && !p.includes('.html'));
    const depth = parts.length;
    const goUp = depth === 0 ? './' : '../'.repeat(depth);
    
    if (targetVersion === 'web') {
        window.location.href = goUp + 'web/index.html';
    } else {
        window.location.href = goUp + 'index.html';
    }
};

const createSwitcher = () => {
    if (document.getElementById('version-switcher')) return;
    
    const switcher = document.createElement('div');
    switcher.id = 'version-switcher';
    
    switcher.innerHTML = `
        <select onchange="switchVersion(this.value)" 
                style="margin:7px; padding:4px; border:1px solid #ccc; border-radius:3px;">
            <option value="main">🖥️ Клиент</option>
            <option value="web">☁️ Веб</option>
        </select>
    `;
    
    const target = document.querySelector('.header-article-items__start') || document.body;
    target.appendChild(switcher);
    
    const select = switcher.querySelector('select');
    const currentUrl = window.location.href;
    const currentPath = window.location.pathname;
    
    let isWeb = false;
    
    if (currentUrl.includes('#/')) {
        if (currentUrl.includes('/web/')) {
            isWeb = true;
        } else if (currentUrl.match(/#\/[^\/]+\/[^\/]+\/web\//)) {
            isWeb = true;
        }
    } else if (currentPath.includes('/web/')) {
        isWeb = true;
    } else if (currentPath.match(/\/web\//)) {
        isWeb = true;
    }
    
    select.value = isWeb ? 'web' : 'main';
};

if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', createSwitcher);
} else {
    createSwitcher();
}