/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
/**
 * Returns a deep copy of the given {@link MapTree}.
 * @privateRemarks This is implemented iteratively (rather than recursively, which is much simpler)
 * to avoid the possibility of a stack overflow for very deep trees.
 */
export function deepCopyMapTree(mapTree) {
    const rootFields = new Map();
    const nexts = [[rootFields, mapTree.fields]];
    for (let next = nexts.pop(); next !== undefined; next = nexts.pop()) {
        const [fields, sourceFields] = next;
        for (const [key, field] of sourceFields) {
            if (field.length > 0) {
                const newField = [];
                for (const child of field) {
                    const childClone = { ...child, fields: new Map() };
                    newField.push(childClone);
                    nexts.push([childClone.fields, child.fields]);
                }
                fields.set(key, newField);
            }
        }
    }
    return {
        ...mapTree,
        fields: rootFields,
    };
}
//# sourceMappingURL=mapTree.js.map