// https://gist.github.com/turboza/d30675f1a309e6e8799f243dcf9c170c
/**
 * Serialize JS object into URL Query string format.
 *
 * @param  {object} params - { a: 10, filters: { x: 2, y: true } }
 * @return {string} - '?a=10&filters[x]=2&filters[y]=true'
 */
export default function paramSerialize(params) {
    if (!isObject(params)) {
        // console.warn(`params (${params}) must be an object`);
        return '';
    }
    const queryArray = objectToQueryList(params);
    return queryArray.length > 0 ? '?' + queryArray.join('&') : '';
}
/**
 * Helper - Object to QueryString array
 * -----------------------------------------
 *
 * Usage:
 * objectToQueryList({ a: 10, b: 'hi' }) === ['a=10', 'b=hi']
 * objectToQueryList({ a: 10, filters: { x: 2, y: true } }) === ['a:10', 'filters[x]=2', 'filters[y]=true']
 * objectToQueryList({ filters: { extra: { m: 5 } } }) === ['filters[extra][m]=5']
 */
function objectToQueryList(object, parentKey) {
    return Object.keys(object).reduce((prevList, key) => {
        const value = object[key];
        if (isInvalidValue(value)) {
            return prevList;
        }
        const encodedKey = encodeURIComponent(key);
        const fullKey = parentKey ? `${parentKey}[${encodedKey}]` : encodedKey;
        if (isObjectOrArray(value)) {
            return prevList.concat(objectToQueryList(value, fullKey));
        }
        const encodedValue = encodeURIComponent(value);
        return prevList.concat(`${fullKey}=${encodedValue}`);
    }, []);
}
/**
 * Helper - isObject (Not Array)
 * -----------------------------------------
 */
function isObject(input) {
    return input && typeof input === 'object' && !Array.isArray(input);
}
/**
 * Helper - isInvalidValue
 * -----------------------------------------
 */
function isInvalidValue(input) {
    return typeof input === 'undefined' || input === null;
}
/**
 * Helper - isObjectOrArray
 * -----------------------------------------
 */
function isObjectOrArray(input) {
    return input && typeof input === 'object';
}
