"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.request = void 0;
const types_1 = require("@scrabble-solver/types");
const follow_redirects_1 = require("follow-redirects");
const request = ({ protocol, ...options }) => {
    const agent = protocol === 'https' ? follow_redirects_1.https : follow_redirects_1.http;
    return new Promise((resolve, reject) => {
        return agent
            .get(options, (response) => {
            let data = '';
            response.setEncoding('utf8');
            response.on('data', (chunk) => {
                data += chunk;
            });
            response.on('end', () => {
                try {
                    resolve(data);
                }
                catch (error) {
                    reject((0, types_1.isError)(error) ? error : new Error(String(error)));
                }
            });
        })
            .on('error', reject);
    });
};
exports.request = request;
