{"version":3,"file":"RecursionTerminator.cjs","sources":["../../../../../packages/react-binding/src/helperComponents/RecursionTerminator.tsx"],"sourcesContent":["import {\n\tEnvironment,\n\tSugarableHasOneRelation,\n\tSugaredRelativeEntityList,\n\tSugaredRelativeSingleEntity,\n\tTokenRegExps,\n} from '@contember/binding'\nimport { ReactNode, useMemo } from 'react'\nimport { Component } from '../coreComponents'\n\nexport interface RecursionTerminatorOptions {\n\tshouldTerminate?: (args: { node: Environment.AnyNode; field: string; environment: Environment }) => boolean | undefined\n}\n\nexport const recursionTerminatorEnvironmentExtension = Environment.createExtension((options: RecursionTerminatorOptions | undefined) => {\n\treturn options\n})\n\nexport interface RecursionTerminatorProps {\n\tfield:\n\t\t| { kind: 'hasOne'; field: SugaredRelativeSingleEntity['field'] }\n\t\t| { kind: 'hasMany'; field: SugaredRelativeEntityList['field'] }\n\tchildren: ReactNode\n}\n\nexport const RecursionTerminator = Component<RecursionTerminatorProps>(({ field, children }, env) => {\n\tconst isRecursion = useMemo(() => {\n\t\treturn recursionDetector(field, env)\n\t}, [field, env])\n\n\treturn !isRecursion ? children : null\n}, ({ field, children }, env) => {\n\tconst isRecursion = recursionDetector(field, env)\n\treturn !isRecursion ? children : null\n})\n\nconst recursionDetector = (field: RecursionTerminatorProps['field'], env: Environment) => {\n\tconst firstPathItem = getFirstPathItem(field)\n\tif (!firstPathItem) {\n\t\treturn false\n\t}\n\tconst shouldTerminate = env.getExtension(recursionTerminatorEnvironmentExtension)?.shouldTerminate\n\tconst shouldTerminateResult = shouldTerminate?.({ node: env.getSubTreeNode(), field: firstPathItem, environment: env })\n\tif (shouldTerminateResult !== undefined) {\n\t\treturn shouldTerminateResult\n\t}\n\n\tconst node = env.getSubTreeNode()\n\tconst otherSide = node.type === 'entity-list' || node.type === 'entity' ? (node.field.ownedBy ?? node.field.inversedBy) : undefined\n\tif (!otherSide) {\n\t\treturn false\n\t}\n\tif (!firstPathItem) {\n\t\treturn false\n\t}\n\treturn otherSide === firstPathItem\n}\n\nconst getFirstPathItem = (field: RecursionTerminatorProps['field']) => {\n\tif (typeof field.field === 'string') {\n\t\treturn field.field.match(TokenRegExps.identifier)?.[0]\n\t}\n\tconst getFromHasOne = (relation: SugarableHasOneRelation[] | SugarableHasOneRelation | undefined) => {\n\t\tif (Array.isArray(relation)) {\n\t\t\treturn relation?.[0].field\n\t\t}\n\t\treturn relation?.field\n\t}\n\tif (field.kind === 'hasMany') {\n\t\treturn getFromHasOne(field.field.hasOneRelationPath) ?? field.field.hasManyRelation.field\n\t}\n\treturn getFromHasOne(field.field)\n}\n"],"names":["Environment","Component","useMemo","TokenRegExps"],"mappings":";;;;;AAcO,MAAM,0CAA0CA,QAAA,YAAY,gBAAgB,CAAC,YAAoD;AAChI,SAAA;AACR,CAAC;AASM,MAAM,sBAAsBC,UAAoC,UAAA,CAAC,EAAE,OAAO,SAAA,GAAY,QAAQ;AAC9F,QAAA,cAAcC,MAAAA,QAAQ,MAAM;AAC1B,WAAA,kBAAkB,OAAO,GAAG;AAAA,EAAA,GACjC,CAAC,OAAO,GAAG,CAAC;AAER,SAAA,CAAC,cAAc,WAAW;AAClC,GAAG,CAAC,EAAE,OAAO,SAAA,GAAY,QAAQ;AAC1B,QAAA,cAAc,kBAAkB,OAAO,GAAG;AACzC,SAAA,CAAC,cAAc,WAAW;AAClC,CAAC;AAED,MAAM,oBAAoB,CAAC,OAA0C,QAAqB;AACnF,QAAA,gBAAgB,iBAAiB,KAAK;AAC5C,MAAI,CAAC,eAAe;AACZ,WAAA;AAAA,EAAA;AAER,QAAM,kBAAkB,IAAI,aAAa,uCAAuC,GAAG;AAC7E,QAAA,wBAAwB,kBAAkB,EAAE,MAAM,IAAI,eAAe,GAAG,OAAO,eAAe,aAAa,IAAA,CAAK;AACtH,MAAI,0BAA0B,QAAW;AACjC,WAAA;AAAA,EAAA;AAGF,QAAA,OAAO,IAAI,eAAe;AAChC,QAAM,YAAY,KAAK,SAAS,iBAAiB,KAAK,SAAS,WAAY,KAAK,MAA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