"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const StringUtils_1 = require("./StringUtils");
class BodyUtils {
    bodyToArr(key, obj, toArr, encode) {
        if (null == obj) {
            return toArr;
        }
        if (this.isPrimitiveDataTypes(obj)) {
            if (StringUtils_1.default.isNotEmpty(obj)) {
                let value = obj.toString();
                if (encode) {
                    value = encodeURI(value);
                }
                toArr.push(key + "=" + value);
            }
            return toArr;
        }
        if (Array.isArray(obj)) {
            if (obj.length == 0) {
                return toArr;
            }
            const objects = obj;
            if (this.isPrimitiveDataTypes(obj[0])) {
                if (StringUtils_1.default.isNotEmpty(key)) {
                    toArr.push(key + "=" + this.join("", objects));
                }
                else {
                    toArr.push(this.join("", objects));
                }
                return toArr;
            }
            for (let i = 0; i < objects.length; i++) {
                let o = objects[i];
                if (StringUtils_1.default.isNotEmpty(key)) {
                    this.bodyToArr(key + "." + i, o, toArr, encode);
                }
                else {
                    this.bodyToArr(i.toString(), o, toArr, encode);
                }
            }
            return toArr;
        }
        for (let attr in obj) {
            if (StringUtils_1.default.isNotEmpty(key)) {
                this.bodyToArr(key + "." + attr.toString(), obj[attr], toArr, encode);
            }
            else {
                this.bodyToArr(attr.toString(), obj[attr], toArr, encode);
            }
        }
        return toArr;
    }
    /**
     * 数组拼接成字符串
     * @param arr
     * @return
     */
    join(delimiter, arr) {
        return arr.join(delimiter);
    }
    /**
     * 数组根据ASCII排序，并拼接成字符串
     * @param arr
     * @return
     */
    arrToStr(arr) {
        if (null == arr || arr.length == 0) {
            return "";
        }
        arr.sort();
        return arr.join("&");
    }
    /**
     * 是否基础数据类型判断
     * @param obj
     */
    isPrimitiveDataTypes(obj) {
        return !(typeof obj === 'object');
    }
}
exports.default = new BodyUtils();
//# sourceMappingURL=BodyUtils.js.map