{
  "version": 3,
  "sources": ["../../src/utils/enforce-layout-policy.ts"],
  "sourcesContent": ["import fastDeepEqual from 'fast-deep-equal/es6/index.js';\nimport type { WidgetType } from '@wordpress/widget-primitives';\nimport { canonicalizeLayout } from './canonicalize-layout';\nimport type {\n\tCanPerformDashboardOperation,\n\tDashboardInstanceOperation,\n\tDashboardTilePlacement,\n\tDashboardWidget,\n} from '../types';\n\n/*\n * Placement fields by facet. `resize` owns the spans; `move` owns the\n * masonry lane pin. Explicit `order` never reaches the diff: both\n * layouts are canonicalized first, so position IS the array position.\n */\nconst SIZE_FIELDS = [ 'width', 'height' ] as const;\nconst POSITION_FIELDS = [ 'lane' ] as const;\n\ntype PlacementFields = readonly string[];\n\nfunction placementOf( widget: DashboardWidget ): Record< string, unknown > {\n\treturn ( widget.placement ?? {} ) as Record< string, unknown >;\n}\n\nfunction placementFieldsEqual(\n\ta: DashboardWidget,\n\tb: DashboardWidget,\n\tfields: PlacementFields\n): boolean {\n\tconst pa = placementOf( a );\n\tconst pb = placementOf( b );\n\treturn fields.every( ( field ) => pa[ field ] === pb[ field ] );\n}\n\nfunction restorePlacementFields(\n\twidget: DashboardWidget,\n\tprior: DashboardWidget,\n\tfields: PlacementFields\n): DashboardWidget {\n\tconst restored = { ...placementOf( widget ) };\n\tconst priorPlacement = placementOf( prior );\n\tfor ( const field of fields ) {\n\t\tif ( field in priorPlacement ) {\n\t\t\trestored[ field ] = priorPlacement[ field ];\n\t\t} else {\n\t\t\tdelete restored[ field ];\n\t\t}\n\t}\n\tif ( ! Object.keys( restored ).length && ! prior.placement ) {\n\t\tconst { placement: _dropped, ...rest } = widget;\n\t\treturn rest;\n\t}\n\treturn { ...widget, placement: restored as DashboardTilePlacement };\n}\n\ninterface EnforceLayoutPolicyArgs {\n\t/**\n\t * The staging layout the incoming one replaces.\n\t */\n\tprevious: DashboardWidget[];\n\n\t/**\n\t * The incoming layout.\n\t */\n\tnext: DashboardWidget[];\n\n\t/**\n\t * The resolved policy.\n\t */\n\tcanPerform: CanPerformDashboardOperation;\n\n\t/**\n\t * Registered types, to resolve each instance's `WidgetType` for the\n\t * policy request.\n\t */\n\twidgetTypes: WidgetType[];\n}\n\n/**\n * Re-asserts every change the policy denies before a layout enters\n * staging, so no composed trigger can slip a denied operation past the\n * interface: what the UI hides, the staging layer rejects.\n *\n * Per instance: a new instance whose type the policy rejects for\n * `insert` is dropped. An unregistered type has no type to ask about,\n * so a new instance of one passes, and instance operations on a\n * surviving tile still ask with `widgetType` absent: a lock keyed on\n * the type does not fire. Denied `edit` keeps the staged `attributes`;\n * denied `resize` keeps the placement spans; denied `move` keeps the\n * masonry `lane` and holds the instance's canonical position, adjusted\n * for allowed membership changes: the same hold `remove` uses, and the\n * position the grid's pinned items keep.\n *\n * Both layouts are canonicalized before the diff (sorted by\n * `order ?? index` with `order` stripped, as the renderer and the\n * commit read them), so a move is a move whichever encoding expressed\n * it. Returns `next` itself when it was already canonical and nothing\n * was re-asserted.\n *\n * @param {EnforceLayoutPolicyArgs} args The layouts to diff and the policy.\n * @return {DashboardWidget[]} The incoming layout with denied changes re-asserted.\n */\nexport function enforceLayoutPolicy( {\n\tprevious: rawPrevious,\n\tnext: rawNext,\n\tcanPerform,\n\twidgetTypes,\n}: EnforceLayoutPolicyArgs ): DashboardWidget[] {\n\tconst previous = canonicalizeLayout( rawPrevious );\n\tconst next = canonicalizeLayout( rawNext );\n\n\tconst typeOf = ( widget: DashboardWidget ) =>\n\t\twidgetTypes.find( ( type ) => type.name === widget.type );\n\tconst priorByUuid = new Map(\n\t\tprevious.map( ( widget ) => [ widget.uuid, widget ] )\n\t);\n\n\t// Instances free to take the position `next` gives them, in `next`\n\t// order; move-denied survivors are staged aside to be held instead.\n\tconst free: DashboardWidget[] = [];\n\tconst heldByUuid = new Map< string, DashboardWidget >();\n\n\tfor ( const incoming of next ) {\n\t\tconst prior = priorByUuid.get( incoming.uuid );\n\n\t\tif ( ! prior ) {\n\t\t\tconst widgetType = typeOf( incoming );\n\t\t\tif (\n\t\t\t\twidgetType &&\n\t\t\t\t! canPerform( { operation: 'insert', widgetType } )\n\t\t\t) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfree.push( incoming );\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst widgetType = typeOf( prior );\n\t\tconst allows = ( operation: DashboardInstanceOperation ) =>\n\t\t\tcanPerform( { operation, widget: prior, widgetType } );\n\n\t\tlet staged = incoming;\n\t\tif (\n\t\t\t! fastDeepEqual( prior.attributes, incoming.attributes ) &&\n\t\t\t! allows( 'edit' )\n\t\t) {\n\t\t\tstaged = { ...staged };\n\t\t\tif ( 'attributes' in prior ) {\n\t\t\t\tstaged.attributes = prior.attributes;\n\t\t\t} else {\n\t\t\t\tdelete staged.attributes;\n\t\t\t}\n\t\t}\n\t\tif (\n\t\t\t! placementFieldsEqual( prior, staged, SIZE_FIELDS ) &&\n\t\t\t! allows( 'resize' )\n\t\t) {\n\t\t\tstaged = restorePlacementFields( staged, prior, SIZE_FIELDS );\n\t\t}\n\n\t\tif ( allows( 'move' ) ) {\n\t\t\tfree.push( staged );\n\t\t\tcontinue;\n\t\t}\n\t\tif ( ! placementFieldsEqual( prior, staged, POSITION_FIELDS ) ) {\n\t\t\tstaged = restorePlacementFields( staged, prior, POSITION_FIELDS );\n\t\t}\n\t\theldByUuid.set( staged.uuid, staged );\n\t}\n\n\t// Walk `previous` to place what holds a position: move-denied\n\t// survivors and denied removals land at their previous index,\n\t// discounting the instances that legitimately left.\n\tconst nextUuids = new Set( next.map( ( widget ) => widget.uuid ) );\n\tconst result = [ ...free ];\n\tlet absent = 0;\n\tconst place = ( widget: DashboardWidget, index: number ) =>\n\t\tresult.splice( Math.min( index - absent, result.length ), 0, widget );\n\n\tprevious.forEach( ( widget, index ) => {\n\t\tif ( ! nextUuids.has( widget.uuid ) ) {\n\t\t\tconst removable = canPerform( {\n\t\t\t\toperation: 'remove',\n\t\t\t\twidget,\n\t\t\t\twidgetType: typeOf( widget ),\n\t\t\t} );\n\t\t\tif ( removable ) {\n\t\t\t\tabsent += 1;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tplace( widget, index );\n\t\t\treturn;\n\t\t}\n\n\t\tconst held = heldByUuid.get( widget.uuid );\n\t\tif ( held ) {\n\t\t\tplace( held, index );\n\t\t}\n\t} );\n\n\tconst untouched =\n\t\tresult.length === next.length &&\n\t\tresult.every( ( widget, index ) => widget === next[ index ] );\n\treturn untouched ? next : result;\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAA0B;AAE1B,iCAAmC;AAanC,IAAM,cAAc,CAAE,SAAS,QAAS;AACxC,IAAM,kBAAkB,CAAE,MAAO;AAIjC,SAAS,YAAa,QAAqD;AAC1E,SAAS,OAAO,aAAa,CAAC;AAC/B;AAEA,SAAS,qBACR,GACA,GACA,QACU;AACV,QAAM,KAAK,YAAa,CAAE;AAC1B,QAAM,KAAK,YAAa,CAAE;AAC1B,SAAO,OAAO,MAAO,CAAE,UAAW,GAAI,KAAM,MAAM,GAAI,KAAM,CAAE;AAC/D;AAEA,SAAS,uBACR,QACA,OACA,QACkB;AAClB,QAAM,WAAW,EAAE,GAAG,YAAa,MAAO,EAAE;AAC5C,QAAM,iBAAiB,YAAa,KAAM;AAC1C,aAAY,SAAS,QAAS;AAC7B,QAAK,SAAS,gBAAiB;AAC9B,eAAU,KAAM,IAAI,eAAgB,KAAM;AAAA,IAC3C,OAAO;AACN,aAAO,SAAU,KAAM;AAAA,IACxB;AAAA,EACD;AACA,MAAK,CAAE,OAAO,KAAM,QAAS,EAAE,UAAU,CAAE,MAAM,WAAY;AAC5D,UAAM,EAAE,WAAW,UAAU,GAAG,KAAK,IAAI;AACzC,WAAO;AAAA,EACR;AACA,SAAO,EAAE,GAAG,QAAQ,WAAW,SAAmC;AACnE;AAiDO,SAAS,oBAAqB;AAAA,EACpC,UAAU;AAAA,EACV,MAAM;AAAA,EACN;AAAA,E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