{"version":3,"file":"useEntity.cjs","sources":["../../../../../packages/react-binding/src/accessorPropagation/useEntity.ts"],"sourcesContent":["import { useConstantLengthInvariant, useConstantValueInvariant } from '@contember/react-utils'\nimport { useCallback } from 'react'\nimport { useEntityKey, useGetEntityByKey } from './index'\nimport type { EntityAccessor } from '@contember/binding'\nimport { useOnConnectionUpdate } from '../entityEvents'\nimport type { SugaredRelativeSingleEntity } from '@contember/binding'\nimport { useAccessorUpdateSubscription } from './useAccessorUpdateSubscription'\nimport { useDesugaredRelativeSingleEntity } from './useDesugaredRelativeSingleEntity'\n\nfunction useEntity(): EntityAccessor\nfunction useEntity(sugaredRelativeSingleEntity: string | SugaredRelativeSingleEntity): EntityAccessor\nfunction useEntity(\n\tsugaredRelativeSingleEntity: string | SugaredRelativeSingleEntity | undefined,\n): EntityAccessor | undefined\nfunction useEntity(...entity: [] | [string | SugaredRelativeSingleEntity | undefined]): EntityAccessor | undefined {\n\tuseConstantValueInvariant(entity.length, 'useEntity: cannot alternate between providing and omitting the argument.')\n\n\tconst entityKey = useEntityKey()\n\tconst getEntityByKey = useGetEntityByKey()\n\n\tif (entity.length === 0) {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst getEntityAccessor = useCallback(() => getEntityByKey(entityKey), [entityKey, getEntityByKey])\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\treturn useAccessorUpdateSubscription(getEntityAccessor)[0]\n\t}\n\tconst sugaredRelativeSingleEntity = entity[0]\n\n\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\tconst relativeSingleEntity = useDesugaredRelativeSingleEntity(sugaredRelativeSingleEntity)\n\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\tconst getEntity = useCallback(() => {\n\t\tconst parent = getEntityByKey(entityKey)\n\t\treturn parent.getEntity(relativeSingleEntity!)\n\t}, [entityKey, getEntityByKey, relativeSingleEntity])\n\n\tif (relativeSingleEntity) {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tuseConstantLengthInvariant(\n\t\t\trelativeSingleEntity.hasOneRelationPath,\n\t\t\t'Cannot change the length of the hasOneRelation path!',\n\t\t)\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst [entity, { update }] = useAccessorUpdateSubscription(getEntity)\n\n\t\tif (relativeSingleEntity.hasOneRelationPath.length) {\n\t\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\t\tuseOnConnectionUpdate(relativeSingleEntity.hasOneRelationPath[0].field, update)\n\t\t}\n\n\t\treturn entity\n\t}\n\treturn undefined\n}\n\nexport { useEntity }\n"],"names":["useConstantValueInvariant","useEntityKey","useGetEntityByKey","useCallback","useAccessorUpdateSubscription","useDesugaredRelativeSingleEntity","useConstantLengthInvariant","entity","useOnConnectionUpdate"],"mappings":";;;;;;;;;AAcA,SAAS,aAAa,QAA6F;AACxFA,uCAAA,OAAO,QAAQ,0EAA0E;AAEnH,QAAM,YAAYC,aAAAA,aAAa;AAC/B,QAAM,iBAAiBC,kBAAAA,kBAAkB;AAErC,MAAA,OAAO,WAAW,GAAG;AAElB,UAAA,oBAAoBC,kBAAY,MAAM,eAAe,SAAS,GAAG,CAAC,WAAW,cAAc,CAAC;AAE3F,WAAAC,8BAAA,8BAA8B,iBAAiB,EAAE,CAAC;AAAA,EAAA;AAEpD,QAAA,8BAA8B,OAAO,CAAC;AAGtC,QAAA,uBAAuBC,kEAAiC,2BAA2B;AAEnF,QAAA,YAAYF,MAAAA,YAAY,MAAM;AAC7B,UAAA,SAAS,eAAe,SAAS;AAChC,WAAA,OAAO,UAAU,oBAAqB;AAAA,EAC3C,GAAA,CAAC,WAAW,gBAAgB,oBAAoB,CAAC;AAEpD,MAAI,sBAAsB;AAEzBG,eAAA;AAAA,MACC,qBAAqB;AAAA,MACrB;AAAA,IACD;AAEA,UAAM,CAACC,SAAQ,EAAE,OAAQ,CAAA,IAAIH,8BAAAA,8BAA8B,SAAS;AAEhE,QAAA,qBAAqB,mBAAmB,QAAQ;AAEnDI,4BAAA,sBAAsB,qBAAqB,mBAAmB,CAAC,EAAE,OAAO,MAAM;AAAA,IAAA;AAGxED,WAAAA;AAAAA,EAAA;AAED,SAAA;AACR;;"}