import CodeSnippet from '../CodeSnippet.js';
export default class DartSnippet extends CodeSnippet {
    lang = 'dart';
    computeCommand(url, method, headers, payload) {
        if (!url || !method) {
            return '';
        }
        const parts = [];
        parts.push("import 'package:http/http.dart' as http;");
        parts.push('');
        parts.push('void main() async {');
        let hasHeaders = false;
        if (headers) {
            const map = {};
            for (const [name, value] of headers) {
                hasHeaders = true;
                map[name] = value;
            }
            if (hasHeaders) {
                parts.push(`  final headers = {`);
                Object.keys(map).forEach((key, index, arr) => parts.push(`    "${key}": "${map[key]}"${arr.length === index + 1 ? '' : ','}`));
                parts.push('  }');
            }
        }
        if (payload) {
            parts.push(`  final body = '''${String(payload)
                .split('\n')
                .map((line, index) => (index ? `  ${line}` : line))
                .join('\n')}'''`);
            parts.push('');
        }
        parts.push(`  final request = http.Request('${method}', Uri.parse('${url}'));`);
        if (hasHeaders) {
            parts.push(`  request.headers.addAll(headers);`);
        }
        if (payload) {
            parts.push(`  request.body = body;`);
        }
        parts.push(``);
        parts.push(`  final response = await request.send();`);
        parts.push(`  final responseBody = await res.stream.bytesToString();`);
        parts.push(`  print(responseBody);`);
        parts.push(`}`);
        return parts.join('\n');
    }
}
//# sourceMappingURL=DartSnippet.js.map