export function findAndInsert(data, key, targetId, newEntry, insertPosition, childrenKey = 'children' // Allow dynamic key for nested children
) {
    function recursiveSearch(items) {
        for (let i = 0; i < items.length; i++) {
            const item = items[i];
            // Check if the item matches the target id
            if (item && item[key] === targetId) {
                const updatedItems = [...items];
                if (insertPosition === 'replace') {
                    updatedItems[i] = newEntry; // Replace the current item
                }
                else {
                    // Insert above or below
                    updatedItems.splice(insertPosition === 'above' ? i : i + 1, 0, newEntry);
                }
                return { updatedArray: updatedItems };
            }
            // Dynamically check nested children using the `childrenKey`
            if (item && item[childrenKey] && Array.isArray(item[childrenKey])) {
                const result = recursiveSearch(item[childrenKey]);
                if (result) {
                    return {
                        updatedArray: items.map((x, idx) => idx === i ? { ...x, [childrenKey]: result.updatedArray } : x),
                    };
                }
            }
        }
        return null;
    }
    return recursiveSearch(data);
}
//# sourceMappingURL=findAndInsert.js.map