{"version":3,"file":"getMaxDepth.mjs","sources":["../../../admin/src/utils/getMaxDepth.ts"],"sourcesContent":["import type { ComponentWithChildren } from '../components/DataManager/utils/retrieveComponentsThatHaveComponents';\nimport type { NestedComponent } from '../components/DataManager/utils/retrieveNestedComponents';\nimport type { Internal } from '@strapi/types';\n\nconst findComponent = (\n componentUid: Internal.UID.Schema,\n components: Array\n) => {\n return components.find((c) => c.component === componentUid);\n};\n\n/**\n * Recursively calculates the maximum depth of nested child components\n * for a given component UID.\n *\n * @param componentUid - The UID of the component to start from.\n * @param components - The array of all components with their child components.\n * @param currentDepth - The current depth of the recursion. Defaults to 0.\n * @returns The maximum depth of the nested child components.\n */\nexport const getChildrenMaxDepth = (\n componentUid: Internal.UID.Component,\n components: Array,\n currentDepth = 0\n): number => {\n const component = findComponent(componentUid, components);\n\n // If the component doesn't exist or has no child components, return the current depth.\n if (!component || !component.childComponents || component.childComponents.length === 0) {\n return currentDepth;\n }\n\n let maxDepth = currentDepth;\n\n // Iterate through each child component to calculate their respective depths.\n component.childComponents.forEach((child) => {\n // Recursively calculate the depth of the child component.\n const depth = getChildrenMaxDepth(child.component, components, currentDepth + 1);\n // Update the maximum depth if the child's depth is greater.\n if (depth > maxDepth) {\n maxDepth = depth;\n }\n });\n\n return maxDepth;\n};\n\n/**\n * Calculates the depth of a component within a nested component tree.\n * Depth is defined as the level at which the component is nested.\n * For example, a component at Depth 3 is the third nested component.\n *\n * @param component - The UID of the component to find the depth for.\n * @param components - The array of all nested components.\n * @returns The depth level of the component within the nested tree.\n */\nexport const getComponentDepth = (\n component: Internal.UID.Schema,\n components: Array\n) => {\n /**\n * Helper function to recursively calculate the depth of a component.\n *\n * @param currentComponent - The current component being inspected.\n * @param currentLevel - The current level of depth in the tree.\n * @returns An array of depth levels found for the component.\n */\n const getDepth = (currentComponent: NestedComponent, currentLevel: number): Array => {\n const levels = [];\n levels.push(currentLevel);\n\n // If the component has no parent UIDs, return the current levels\n if (!currentComponent.uidsOfAllParents) {\n return levels;\n }\n\n // Iterate over each parent UID to calculate their respective depths\n for (const parentUid of currentComponent.uidsOfAllParents) {\n const parentComponent = findComponent(parentUid, components);\n if (parentComponent) {\n levels.push(...getDepth(parentComponent, currentLevel + 1));\n }\n }\n\n return levels;\n };\n\n const nestedCompo = findComponent(component, components);\n // return depth 0 if component is not nested\n if (!nestedCompo) {\n return 0;\n }\n const compoDepth = Math.max(...getDepth(nestedCompo, 1));\n return compoDepth;\n};\n"],"names":["findComponent","componentUid","components","find","c","component","getChildrenMaxDepth","currentDepth","childComponents","length","maxDepth","forEach","child","depth","getComponentDepth","getDepth","currentComponent","currentLevel","levels","push","uidsOfAllParents","parentUid","parentComponent","nestedCompo","compoDepth","Math","max"],"mappings":"AAIA,MAAMA,aAAAA,GAAgB,CACpBC,YAAAA,EACAC,UAAAA,GAAAA;AAEA,IAAA,OAAOA,WAAWC,IAAI,CAAC,CAACC,CAAAA,GAAMA,CAAAA,CAAEC,SAAS,KAAKJ,YAAAA,CAAAA;AAChD,CAAA;AAEA;;;;;;;;AAQC,IACM,MAAMK,mBAAAA,GAAsB,CACjCL,YAAAA,EACAC,UAAAA,EACAK,eAAe,CAAC,GAAA;IAEhB,MAAMF,SAAAA,GAAYL,cAAcC,YAAAA,EAAcC,UAAAA,CAAAA;;IAG9C,IAAI,CAACG,SAAAA,IAAa,CAACA,SAAAA,CAAUG,eAAe,IAAIH,SAAAA,CAAUG,eAAe,CAACC,MAAM,KAAK,CAAA,EAAG;QACtF,OAAOF,YAAAA;AACT,IAAA;AAEA,IAAA,IAAIG,QAAAA,GAAWH,YAAAA;;AAGfF,IAAAA,SAAAA,CAAUG,eAAe,CAACG,OAAO,CAAC,CAACC,KAAAA,GAAAA;;AAEjC,QAAA,MAAMC,QAAQP,mBAAAA,CAAoBM,KAAAA,CAAMP,SAAS,EAAEH,YAAYK,YAAAA,GAAe,CAAA,CAAA;;AAE9E,QAAA,IAAIM,QAAQH,QAAAA,EAAU;YACpBA,QAAAA,GAAWG,KA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