"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseBodyData = exports.parseColor = exports.formatUrl = exports.parseUrl = exports.parsePriceAll = exports.parsePrice = void 0;
const parse_color_1 = __importDefault(require("parse-color"));
const querystring_1 = __importDefault(require("querystring"));
const url_1 = __importDefault(require("url"));
const assert_js_1 = require("./assert.js");
const price = __importStar(require("./price.js"));
function parsePrice(text, parseNegative = false) {
    const p = price.parseFirst(text, { parseNegative });
    return p ? {
        value: p.value,
        currencyCode: p.currencyCode,
    } : null;
}
exports.parsePrice = parsePrice;
function parsePriceAll(text, parseNegative = false) {
    return price.parseAll(text, { parseNegative }).map(p => {
        return {
            value: p.value,
            currencyCode: p.currencyCode,
        };
    });
}
exports.parsePriceAll = parsePriceAll;
function parseUrl(url) {
    const parsed = url_1.default.parse(url);
    const { href = null, protocol = null, hostname = null, host = null, port = null, pathname = null, hash = null, } = parsed;
    if (href && protocol && host && hostname && pathname) {
        const query = querystring_1.default.parse(parsed.query || '');
        return {
            href,
            protocol,
            hostname,
            host,
            port,
            pathname,
            hash,
            query,
        };
    }
    return null;
}
exports.parseUrl = parseUrl;
function formatUrl(obj) {
    const { url = '', query = {}, protocol, hostname, host, pathname, hash } = obj;
    const parsedUrl = url_1.default.parse(url);
    assignOverrides(parsedUrl, { protocol, hostname, host, pathname, hash });
    if (!parsedUrl.protocol) {
        parsedUrl.protocol = 'http:';
    }
    (0, assert_js_1.assertPlayback)(parsedUrl.hostname || parsedUrl.host, 'url or host is required');
    const parsedQuery = querystring_1.default.parse(parsedUrl.query || '');
    Object.assign(parsedQuery, query);
    const qs = querystring_1.default.stringify(parsedQuery);
    parsedUrl.search = qs ? '?' + qs : '';
    return url_1.default.format(parsedUrl);
}
exports.formatUrl = formatUrl;
function assignOverrides(object, overrides) {
    for (const key of Object.keys(overrides)) {
        const value = overrides[key];
        if (value) {
            object[key] = value;
        }
    }
}
function parseColor(str) {
    const color = (0, parse_color_1.default)(str);
    return color.rgb ? color : null;
}
exports.parseColor = parseColor;
function parseBodyData(buffer, format) {
    switch (format) {
        case 'none':
            return null;
        case 'text':
            return buffer.toString('utf-8');
        case 'base64':
            return buffer.toString('base64');
        case 'json':
            return JSON.parse(buffer.toString('utf-8'));
        case 'urlencoded':
            return querystring_1.default.parse(buffer.toString('utf-8'));
        default:
            (0, assert_js_1.assertScript)(false, `Unknown body format: ${format}`);
    }
}
exports.parseBodyData = parseBodyData;
//# sourceMappingURL=parse.js.map