{"version":3,"file":"useDerivedField.cjs","sources":["../../../../../packages/react-binding/src/entityEvents/useDerivedField.ts"],"sourcesContent":["import { useCallback, useEffect } from 'react'\nimport { useDesugaredRelativeSingleField, useEntityKey, useGetEntityByKey } from '../accessorPropagation'\nimport type { FieldAccessor } from '@contember/binding'\nimport type { FieldValue, SugaredRelativeSingleField } from '@contember/binding'\n\nconst identityFunction = <Value>(value: Value) => value\n\n// TODO this is fundamentally wrong. This shouldn't only happen on beforeUpdate, but also on initialize.\n// \twe need a useEffect equivalent.\n\n/**\n * Derived fields are meant for cases when the user is expected to primarily edit the `sourceField` whose optionally\n * transformed value is then copied to the `derivedField`. This happens after each update until either the `derivedField`\n * is touched or until it is persisted at which point the tie between the fields is automatically severed.\n * @deprecated This is fundamentally wrong and shouldn't be used for now.\n */\nexport const useDerivedField = <SourceValue extends FieldValue = FieldValue>(\n\tsourceField: string | SugaredRelativeSingleField,\n\tderivedField: string | SugaredRelativeSingleField,\n\ttransform: (sourceValue: SourceValue | null) => SourceValue | null = identityFunction,\n\tagent: string = 'derivedField',\n) => {\n\tconst entityKey = useEntityKey()\n\tconst getEntityByKey = useGetEntityByKey()\n\tconst potentiallyStaleParent = getEntityByKey(entityKey)\n\tconst stableGetEntityReference = potentiallyStaleParent.getAccessor\n\n\tconst desugaredSource = useDesugaredRelativeSingleField(sourceField)\n\tconst desugaredDerived = useDesugaredRelativeSingleField(derivedField)\n\n\tconst potentiallyStaleSourceAccessor = potentiallyStaleParent.getField<SourceValue>(desugaredSource)\n\tconst stableGetSourceReference = potentiallyStaleSourceAccessor.getAccessor\n\n\tconst onBeforeUpdate = useCallback<FieldAccessor.BeforeUpdateListener<SourceValue>>(\n\t\tsourceAccessor => {\n\t\t\tstableGetEntityReference().batchUpdates(getAccessor => {\n\t\t\t\t// This is tricky: we're deliberately getting the Entity, and not the field\n\t\t\t\tconst derivedHostEntity = getAccessor().getEntity(desugaredDerived)\n\n\t\t\t\tif (derivedHostEntity.existsOnServer) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst derivedAccessor = getAccessor().getField<SourceValue>(desugaredDerived)\n\n\t\t\t\tif (derivedAccessor.isTouched) {\n\t\t\t\t\t// Querying the user\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst transformedValue = transform(sourceAccessor.value)\n\t\t\t\tderivedAccessor.updateValue(transformedValue, {\n\t\t\t\t\tagent,\n\t\t\t\t})\n\t\t\t})\n\t\t},\n\t\t[stableGetEntityReference, desugaredDerived, transform, agent],\n\t)\n\n\tuseEffect(\n\t\t() => stableGetSourceReference().addEventListener({ type: 'beforeUpdate' }, onBeforeUpdate),\n\t\t[onBeforeUpdate, stableGetSourceReference],\n\t)\n}\n"],"names":["useEntityKey","useGetEntityByKey","useDesugaredRelativeSingleField","useCallback","useEffect"],"mappings":";;;;;;AAKA,MAAM,mBAAmB,CAAQ,UAAiB;AAW3C,MAAM,kBAAkB,CAC9B,aACA,cACA,YAAqE,kBACrE,QAAgB,mBACZ;AACJ,QAAM,YAAYA,aAAAA,aAAa;AAC/B,QAAM,iBAAiBC,kBAAAA,kBAAkB;AACnC,QAAA,yBAAyB,eAAe,SAAS;AACvD,QAAM,2BAA2B,uBAAuB;AAElD,QAAA,kBAAkBC,gEAAgC,WAAW;AAC7D,QAAA,mBAAmBA,gEAAgC,YAAY;AAE/D,QAAA,iCAAiC,uBAAuB,SAAsB,eAAe;AACnG,QAAM,2BAA2B,+BAA+B;AAEhE,QAAM,iBAAiBC,MAAA;AAAA,IACtB,CAAkB,mBAAA;AACQ,+BAAA,EAAE,aAAa,CAAe,gBAAA;AAEtD,cAAM,oBAAoB,cAAc,UAAU,gBAAgB;AAElE,YAAI,kBAAkB,gBAAgB;AACrC;AAAA,QAAA;AAGD,cAAM,kBAAkB,cAAc,SAAsB,gBAAgB;AAE5E,YAAI,gBAAgB,WAAW;AAE9B;AAAA,QAAA;AAGK,cAAA,mBAAmB,UAAU,eAAe,KAAK;AACvD,wBAAgB,YAAY,kBAAkB;AAAA,UAC7C;AAAA,QAAA,CACA;AAAA,MAAA,CACD;AAAA,IACF;AAAA,IACA,CAAC,0BAA0B,kBAAkB,WAAW,KAAK;AAAA,EAC9D;AAEAC,QAAA;AAAA,IACC,MAAM,yBAA2B,EAAA,iBAAiB,EAAE,MAAM,kBAAkB,cAAc;AAAA,IAC1F,CAAC,gBAAgB,wBAAwB;AAAA,EAC1C;AACD;;"}