import { format } from 'util';
import { CodeBuilder } from '../../helpers/code-builder.js';
import shellHelpers from '../../helpers/shell.js';
import { addIndentation, buildUrlExpression } from '../../helpers/code-helpers.js';
import { normalizeMimeType } from '../../../../utils.js';
import { HTTPSnippet } from '../../index.js';
import { MediaTypes } from '../../../../../constants.js';
const handler = function (source, originalOptions, { target, client }) {
    const options = Object.assign({
        indent: '  ',
        short: false,
        binary: false,
    }, originalOptions);
    const code = new CodeBuilder({
        indentation: options.indent,
        lineJoin: options.indent ? ' \\\n' + options.indent : ' ',
    });
    const oAuth2PrefixCode = source.securityOAuth2ExtraCalls?.length
        ? new HTTPSnippet(source.securityOAuth2ExtraCalls?.[0]).convert(target, client, {
            ...options,
            withComments: false,
        })
        : '';
    code.push(0, 'curl -i %s %s', options.short ? '-X' : '--request', source.method.toUpperCase());
    if (source.basicAuth) {
        code.push(format('-u %s:%s', source.basicAuth.username, source.basicAuth.password));
    }
    const URL = buildUrlExpression(source, code);
    code.push(format('%s%s', options.short ? '' : '--url ', shellHelpers.quote(URL)));
    if (source.httpVersion === 'HTTP/1.0') {
        code.push(options.short ? '-0' : '--http1.0');
    }
    // construct headers
    Object.keys(source.headersObj)
        .sort()
        .forEach(function (key) {
        const header = format('%s: %s', key, source.headersObj[key]);
        code.push('%s %s', options.short ? '-H' : '--header', shellHelpers.quote(header));
    });
    if (source.allHeaders.cookie) {
        code.push('%s %s', options.short ? '-b' : '--cookie', shellHelpers.quote(source.allHeaders.cookie));
    }
    // construct post params
    if (source.postData) {
        switch (normalizeMimeType(source.postData.mimeType)) {
            case MediaTypes.MULTIPART:
                source.postData.params.map(function (param) {
                    let post;
                    if (param.fileName) {
                        post = format('%s=@%s', param.name, param.fileName);
                    }
                    else {
                        post = format('%s=%s', param.name, param.value);
                    }
                    code.push('%s %s', options.short ? '-F' : '--form', shellHelpers.quote(post));
                });
                break;
            case MediaTypes.URL_ENCODED:
                if (source.postData.params) {
                    source.postData.params.map(function (param) {
                        code.push('%s %s', options.binary ? '--data-binary' : options.short ? '-d' : '--data', shellHelpers.quote(format('%s=%s', param.name, param.value)));
                    });
                }
                else {
                    code.push('%s %s', options.binary ? '--data-binary' : options.short ? '-d' : '--data', shellHelpers.quote(source.postData.text));
                }
                break;
            case MediaTypes.JSON:
                if (source.postData.jsonObj) {
                    code.push('%s %s', options.binary ? '--data-binary' : options.short ? '-d' : '--data', addIndentation(shellHelpers.quote(JSON.stringify(source.postData.jsonObj, null, options.indent)), { level: 1, indent: options.indent, firstLine: false }));
                }
                break;
            default:
                // raw request body
                if (source.postData.text) {
                    code.push('%s %s', options.binary ? '--data-binary' : options.short ? '-d' : '--data', addIndentation(shellHelpers.quote(source.postData.text), {
                        level: 1,
                        indent: options.indent,
                        firstLine: false,
                    }));
                }
        }
    }
    const commentPrefixCode = options.withComments ? '# Requires curl >= 7.2\n\n' : '';
    return (commentPrefixCode + (oAuth2PrefixCode ? oAuth2PrefixCode + '\n\n' + code.join() : code.join()));
};
export const info = {
    key: 'curl',
    title: 'curl',
    link: 'http://curl.haxx.se/',
    description: 'cURL is a command line tool and library for transferring data with URL syntax',
};
export default handler;
//# sourceMappingURL=curl.js.map