{"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) => {\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,YACAC,EAAAA,UAAAA,GAAAA;AAEA,IAAA,OAAOA,WAAWC,IAAI,CAAC,CAACC,CAAMA,GAAAA,CAAAA,CAAEC,SAAS,KAAKJ,YAAAA,CAAAA;AAChD,CAAA;AAEA;;;;;;;;AAQC,IACYK,MAAAA,mBAAAA,GAAsB,CACjCL,YACAC,EAAAA,UAAAA,EACAK,eAAe,CAAC,GAAA;IAEhB,MAAMF,SAAAA,GAAYL,cAAcC,YAAcC,EAAAA,UAAAA,CAAAA;;IAG9C,IAAI,CAACG,SAAa,IAAA,CAACA,SAAUG,CAAAA,eAAe,IAAIH,SAAAA,CAAUG,eAAe,CAACC,MAAM,KAAK,CAAG,EAAA;QACtF,OAAOF,YAAAA;AACT;AAEA,IAAA,IAAIG,QAAWH,GAAAA,YAAAA;;AAGfF,IAAAA,SAAAA,CAAUG,eAAe,CAACG,OAAO,CAAC,CAACC,KAAAA,GAAAA;;AAEjC,QAAA,MAAMC,QAAQP,mBAAoBM,CAAAA,KAAAA,CAAMP,SAAS,EAAEH,YAAYK,YAAe,GAAA,CAAA,CAAA;;AAE9E,QAAA,IAAIM,QAAQH,QAAU,EAAA;YACpBA,QAAWG,GAAAA,KAAAA;AACb;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