{"version":3,"file":"SetOrderFieldOnCreate.cjs","sources":["../../../../../packages/react-binding/src/accessorSorting/SetOrderFieldOnCreate.tsx"],"sourcesContent":["import type { Input } from '@contember/client'\nimport type {\n\tSugaredOrderBy,\n\tSugaredQualifiedEntityList,\n\tSugaredRelativeSingleField,\n\tSugaredUnconstrainedQualifiedSingleEntity,\n} from '@contember/binding'\nimport { QueryLanguage } from '@contember/binding'\nimport { Component, EntityListSubTree, EntitySubTree, Field } from '../coreComponents'\nimport { SugaredField } from '../helperComponents'\nimport { addEntityAtIndex } from './addEntityAtIndex'\n\nexport interface SetOrderFieldOnCreateOwnProps {\n\torderField: SugaredRelativeSingleField | string\n\tnewOrderFieldValue?: number\n}\n\nexport interface SetOrderFieldOnCreateProps\n\textends SetOrderFieldOnCreateOwnProps,\n\tPick<SugaredUnconstrainedQualifiedSingleEntity, 'entity'> {}\n\nexport const SetOrderFieldOnCreate = Component<SetOrderFieldOnCreateProps>(\n\t() => null,\n\t({ orderField, newOrderFieldValue, entity }, environment) => {\n\t\tconst desugaredOrderField = QueryLanguage.desugarRelativeSingleField(orderField, environment)\n\t\tconst getOrderBy = (order: 'asc' | 'desc'): SugaredOrderBy => [\n\t\t\tdesugaredOrderField.hasOneRelationPath\n\t\t\t\t.map(relation => relation.field)\n\t\t\t\t.reduceRight(\n\t\t\t\t\t(accumulator, currentValue) => ({\n\t\t\t\t\t\t[currentValue]: accumulator,\n\t\t\t\t\t}),\n\t\t\t\t\t{\n\t\t\t\t\t\t[desugaredOrderField.field]: order,\n\t\t\t\t\t} as Input.OrderBy<`${Input.OrderDirection}`>,\n\t\t\t\t),\n\t\t]\n\n\t\tif (newOrderFieldValue === undefined) {\n\t\t\tconst qel: SugaredQualifiedEntityList = {\n\t\t\t\tentities: entity,\n\t\t\t\texpectedMutation: 'none',\n\t\t\t\tlimit: 1,\n\t\t\t\torderBy: getOrderBy('desc'),\n\t\t\t}\n\t\t\treturn (\n\t\t\t\t<>\n\t\t\t\t\t<EntitySubTree\n\t\t\t\t\t\tentity={entity}\n\t\t\t\t\t\tisCreating\n\t\t\t\t\t\tonBeforePersist={(getAccessor, bindingOperations) => {\n\t\t\t\t\t\t\tconst listSubTree = bindingOperations.getEntityListSubTree(qel)\n\t\t\t\t\t\t\tconst entities = Array.from(listSubTree)\n\t\t\t\t\t\t\tconst newOrderFieldValue = !entities.length\n\t\t\t\t\t\t\t\t? 0\n\t\t\t\t\t\t\t\t: (entities[0].getField<number>(desugaredOrderField).value ?? 0) + 1\n\t\t\t\t\t\t\tgetAccessor().getField<number>(desugaredOrderField).updateValue(newOrderFieldValue)\n\t\t\t\t\t\t}}\n\t\t\t\t\t>\n\t\t\t\t\t\t<SugaredField field={orderField} />\n\t\t\t\t\t</EntitySubTree>\n\t\t\t\t\t<EntityListSubTree {...qel}>\n\t\t\t\t\t\t<SugaredField field={orderField} />\n\t\t\t\t\t</EntityListSubTree>\n\t\t\t\t</>\n\t\t\t)\n\t\t}\n\t\treturn (\n\t\t\t<>\n\t\t\t\t<Field field={desugaredOrderField} defaultValue={newOrderFieldValue} isNonbearing />\n\t\t\t\t<EntityListSubTree\n\t\t\t\t\tentities={entity}\n\t\t\t\t\texpectedMutation=\"anyMutation\"\n\t\t\t\t\torderBy={getOrderBy('asc')}\n\t\t\t\t\tonBeforePersist={(getAccessor, bindingOperations) => {\n\t\t\t\t\t\t// We're creating a new entity which adjusts the numbering of the other ones if applicable\n\t\t\t\t\t\t// and then deleting it again which leaves a hole for newOrderFieldValue that is set above.\n\t\t\t\t\t\tlet newEntityKey: string | undefined = undefined\n\t\t\t\t\t\taddEntityAtIndex(getAccessor(), desugaredOrderField, newOrderFieldValue, getNewEntity => {\n\t\t\t\t\t\t\tnewEntityKey = getNewEntity().key\n\t\t\t\t\t\t})\n\t\t\t\t\t\tnewEntityKey && bindingOperations.getEntityByKey(newEntityKey).deleteEntity()\n\t\t\t\t\t}}\n\t\t\t\t>\n\t\t\t\t\t<SugaredField field={orderField} />\n\t\t\t\t</EntityListSubTree>\n\t\t\t</>\n\t\t)\n\t},\n\t'SetOrderFieldOnCreate',\n)\n"],"names":["Component","QueryLanguage","jsxs","Fragment","jsx","EntitySubTree","newOrderFieldValue","SugaredField","EntityListSubTree","Field","addEntityAtIndex"],"mappings":";;;;;;;;;;AAqBO,MAAM,wBAAwBA,UAAA;AAAA,EACpC,MAAM;AAAA,EACN,CAAC,EAAE,YAAY,oBAAoB,OAAA,GAAU,gBAAgB;AAC5D,UAAM,sBAAsBC,QAAA,cAAc,2BAA2B,YAAY,WAAW;AACtF,UAAA,aAAa,CAAC,UAA0C;AAAA,MAC7D,oBAAoB,mBAClB,IAAI,CAAY,aAAA,SAAS,KAAK,EAC9B;AAAA,QACA,CAAC,aAAa,kBAAkB;AAAA,UAC/B,CAAC,YAAY,GAAG;AAAA,QAAA;AAAA,QAEjB;AAAA,UACC,CAAC,oBAAoB,KAAK,GAAG;AAAA,QAAA;AAAA,MAC9B;AAAA,IAEH;AAEA,QAAI,uBAAuB,QAAW;AACrC,YAAM,MAAkC;AAAA,QACvC,UAAU;AAAA,QACV,kBAAkB;AAAA,QAClB,OAAO;AAAA,QACP,SAAS,WAAW,MAAM;AAAA,MAC3B;AACA,aAEEC,2BAAA,KAAAC,qBAAA,EAAA,UAAA;AAAA,QAAAC,2BAAA;AAAA,UAACC,cAAA;AAAA,UAAA;AAAA,YACA;AAAA,YACA,YAAU;AAAA,YACV,iBAAiB,CAAC,aAAa,sBAAsB;AAC9C,oBAAA,cAAc,kBAAkB,qBAAqB,GAAG;AACxD,oBAAA,WAAW,MAAM,KAAK,WAAW;AACvC,oBAAMC,sBAAqB,CAAC,SAAS,SAClC,KACC,SAAS,CAAC,EAAE,SAAiB,mBAAmB,EAAE,SAAS,KAAK;AACpE,0BAAc,EAAA,SAAiB,mBAAmB,EAAE,YAAYA,mBAAkB;AAAA,YACnF;AAAA,YAEA,UAAAF,2BAAAA,IAACG,aAAAA,cAAa,EAAA,OAAO,WAAY,CAAA;AAAA,UAAA;AAA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