import { corrigirPontuacaoECodigo, limitarLinhasEmBranco, normalizarFimDeLinha, normalizarNewlinesFinais, removerBom } from './commons.js';
function corrigirImportsGo(lines) {
    const out = [];
    const ctx = {
        inImportBlock: false,
        inVarBlock: false,
        inConstBlock: false,
        inMultilineComment: false,
        inString: false,
        stringChar: '',
        braceDepth: 0
    };
    const flushImports = (imports, beforeLines) => {
        if (imports.length === 0)
            return;
        const sorted = [...imports].sort();
        const stdlib = [];
        const thirdParty = [];
        for (const imp of sorted) {
            const path = imp.replace(/.*"([^"]+)".*/, '$1');
            if (path && !path.includes('.')) {
                stdlib.push(imp);
            }
            else {
                thirdParty.push(imp);
            }
        }
        if (beforeLines.length > 0 && beforeLines[beforeLines.length - 1] !== '') {
            out.push('');
        }
        if (stdlib.length + thirdParty.length === 1) {
            out.push(...stdlib, ...thirdParty);
        }
        else {
            out.push('import (');
            for (const imp of stdlib) {
                out.push(`\t${imp.trim()}`);
            }
            if (stdlib.length > 0 && thirdParty.length > 0) {
                out.push('');
            }
            for (const imp of thirdParty) {
                out.push(`\t${imp.trim()}`);
            }
            out.push(')');
        }
    };
    let pendingImports = [];
    let beforeImportLines = [];
    for (const raw of lines) {
        const trimmed = raw.trim();
        if (ctx.inMultilineComment) {
            out.push(raw);
            if (trimmed.includes('*/')) {
                ctx.inMultilineComment = false;
            }
            continue;
        }
        if (trimmed.startsWith('/*') && !trimmed.includes('*/')) {
            ctx.inMultilineComment = true;
            out.push(raw);
            continue;
        }
        if (trimmed.startsWith('//')) {
            if (ctx.inImportBlock) {
                pendingImports.push(raw);
            }
            else {
                out.push(raw);
            }
            continue;
        }
        if (/^import\s+\(/.test(trimmed)) {
            ctx.inImportBlock = true;
            pendingImports = [];
            continue;
        }
        if (ctx.inImportBlock && trimmed === ')') {
            ctx.inImportBlock = false;
            flushImports(pendingImports, beforeImportLines);
            pendingImports = [];
            beforeImportLines = [];
            continue;
        }
        if (/^import\s+"/.test(trimmed)) {
            flushImports([trimmed], beforeImportLines);
            beforeImportLines = [];
            continue;
        }
        if (ctx.inImportBlock) {
            if (trimmed) {
                pendingImports.push(trimmed);
            }
            continue;
        }
        if (/^package\s+/.test(trimmed)) {
            out.push(raw);
            continue;
        }
        out.push(raw);
    }
    if (pendingImports.length > 0) {
        flushImports(pendingImports, beforeImportLines);
    }
    return out;
}
function corrigirEspacamentoGo(lines) {
    const out = [];
    let prevWasFunc = false;
    for (const raw of lines) {
        const trimmed = raw.trim();
        if (!trimmed || trimmed.startsWith('//') || trimmed.startsWith('/*')) {
            out.push(raw);
            continue;
        }
        if (/^(func|type)\s+/.test(trimmed) && prevWasFunc === false && out.length > 0) {
            if (out[out.length - 1] !== '') {
                out.push('');
            }
        }
        out.push(raw.replace(/[ \t]+$/, ''));
        prevWasFunc = /^(func|type)\s+/.test(trimmed);
    }
    return out;
}
export function formatarGoMinimo(code) {
    const normalized = normalizarFimDeLinha(removerBom(code));
    const lines = normalized.split('\n');
    const comImports = corrigirImportsGo(lines);
    const comEspacamento = corrigirEspacamentoGo(comImports);
    const { code: limitado } = limitarLinhasEmBranco(comEspacamento.join('\n'), 2);
    const { text: comPontuacao, stats } = corrigirPontuacaoECodigo(limitado);
    const formatted = normalizarNewlinesFinais(comPontuacao);
    const baseline = normalizarNewlinesFinais(normalized);
    const anyChanged = formatted !== baseline;
    return {
        ok: true,
        parser: 'go',
        formatted,
        changed: anyChanged,
        reason: anyChanged
            ? `estilo-tutanus-go(punct:${stats.pontuacaoRepetida},space:${stats.espacamentoIncorreto},ops:${stats.operadoresRelacionais})`
            : 'normalizacao-basica'
    };
}
//# sourceMappingURL=go.js.map