"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchOrgCategories = fetchOrgCategories;
const arcgis_rest_request_1 = require("@esri/arcgis-rest-request");
/**
 * Fetch the categorySchema for all the categories in an org, including
 * the ones with 0 count, then recursively construct an array of
 * fully qualified category paths.
 *
 * @param orgId The organization id
 * @param hubRequestOptions The hub request options
 * @returns a flat array of category paths (e.g. ["/Categories/Category1", "/Categories/Category1/Subcategory1", ...])
 */
async function fetchOrgCategories(orgId, hubRequestOptions) {
    const url = `${hubRequestOptions.portal}/portals/${orgId}/categorySchema`;
    try {
        const { categorySchema } = (await (0, arcgis_rest_request_1.request)(url, hubRequestOptions));
        // All categories need to be prefixed with "/Categories" to be valid
        return parseOrgCategories(categorySchema[0].categories, "/Categories");
    }
    catch (_e) {
        return [];
    }
}
/**
 * Convert the categories into a format that can be used
 * by the IUiSchemaComboboxItem interface
 *
 * @param obj The object to convert
 * @param parentValue The parent value to append to the value
 * @returns the converted object where `title` becomes `value` and `categories` becomes `children`
 */
function parseOrgCategories(nodes, parentValue) {
    return nodes.reduce((categories, node) => {
        // If there is a parentValue, we append it to the category title. This follows the schema that ArcGIS Online
        // uses to save categories on items (e.g. `/Categories/path/to/my/category`) and gives this combobox item
        // a unique value to distinguish it from others
        const value = `${parentValue}/${node.title}`;
        categories.push(value);
        // If there are child categories, we recursively parse them
        if (node.categories && node.categories.length > 0) {
            categories.push(...parseOrgCategories(node.categories, value));
        }
        return categories;
    }, []);
}
//# sourceMappingURL=fetchOrgCategories.js.map