"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const sdk_1 = require("../sdk");
function nestify(categories) {
    const rootCategories = [];
    const catMap = categories.reduce((map, cat) => {
        map[cat.id] = cat;
        return map;
    }, {});
    categories.forEach(cat => {
        if (cat.parent) {
            const parent = catMap[cat.parent.id];
            parent.subCategories = parent.subCategories || [];
            parent.subCategories.push(cat);
        }
        else {
            rootCategories.push(cat);
        }
    });
    return rootCategories;
}
var Categories;
(function (Categories) {
    function fetchAll(flat = false) {
        return sdk_1.clientProcess({
            uri: sdk_1.getServices()
                .categories.perPage(100)
                .build(),
            method: sdk_1.methods.GET
        }).then(res => {
            if (flat === true) {
                return res;
            }
            return nestify(res);
        });
    }
    Categories.fetchAll = fetchAll;
    function findById(categoryId) {
        return sdk_1.clientExecute({
            uri: sdk_1.getServices()
                .categories.byId(categoryId)
                .build(),
            method: sdk_1.methods.GET
        });
    }
    Categories.findById = findById;
})(Categories = exports.Categories || (exports.Categories = {}));
//# sourceMappingURL=categories.js.map