"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeStructure = exports.findStructureByExtendId = exports.findStructureById = exports.findStructureByName = exports.findStructure = exports.structureToList = exports.structureToNodeMap = void 0;
/**
 * structure to map
 * @param structures
 * @returns
 */
const structureToNodeMap = (structures) => {
    const map = {};
    function dfs(list = []) {
        list.forEach((item) => {
            if (item) {
                let childIds = [];
                if (item.children) {
                    dfs(item.children);
                    childIds = item.children.map((item) => item.id);
                }
                map[item.id] = Object.assign(Object.assign({}, item), { children: [], childIds });
            }
        });
    }
    dfs(structures);
    return map;
};
exports.structureToNodeMap = structureToNodeMap;
/**
 * structure to list
 * @param structures
 * @returns
 */
const structureToList = (structures) => {
    const result = [];
    function dfs(list = []) {
        list.forEach((item) => {
            if (item) {
                if (item.children) {
                    dfs(item.children);
                    item.childIds = item.children.map((item) => item.id);
                }
                result.push(Object.assign(Object.assign({}, item), { children: [] }));
            }
        });
    }
    dfs(structures);
    return result;
};
exports.structureToList = structureToList;
// finders
const createSelector = (key, val) => (structure) => {
    return structure && structure[key] === val;
};
const findStructure = (list, selector) => {
    var _a;
    for (let idx = 0; idx < list.length; idx++) {
        const node = list[idx];
        if (selector(node)) {
            return node;
        }
        if ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length) {
            const result = (0, exports.findStructure)(node.children, selector);
            if (result) {
                return result;
            }
        }
    }
    return null;
};
exports.findStructure = findStructure;
const findStructureByName = (structure, value) => {
    const selector = createSelector('name', value);
    return (0, exports.findStructure)(structure, selector);
};
exports.findStructureByName = findStructureByName;
const findStructureById = (structure, value) => {
    const selector = createSelector('id', value);
    return (0, exports.findStructure)(structure, selector);
};
exports.findStructureById = findStructureById;
const findStructureByExtendId = (dsl, value) => {
    return (0, exports.findStructure)(dsl, (node) => {
        var _a;
        return ((_a = node === null || node === void 0 ? void 0 : node.extension) === null || _a === void 0 ? void 0 : _a.extendId) === value;
    });
};
exports.findStructureByExtendId = findStructureByExtendId;
// filter removed
const removeStructure = (structure = [], removeId) => {
    return structure
        .filter((item) => item.id !== removeId)
        .map((item) => {
        if (item.children) {
            const children = (0, exports.removeStructure)(item.children, removeId);
            const childIds = children.map((child) => child.id);
            return Object.assign(Object.assign({}, item), { children, childIds });
        }
        return item;
    });
};
exports.removeStructure = removeStructure;
