{"version":3,"file":"canonicalize-layout.cjs","names":[],"sources":["../../src/utils/canonicalize-layout.ts"],"sourcesContent":["import type { DashboardWidget } from '../types';\n\n/**\n * Canonical form of a layout: widgets sorted by `placement.order` (falling\n * back to array index), then `order` stripped since position now implies\n * it. The single positional encoding shared by the renderer, the commit\n * payload, and the staging enforcement.\n *\n * Returns `layout` itself when it is already canonical, and keeps the\n * reference of every widget without an `order` to strip, so untouched\n * layouts stay identity-comparable.\n *\n * @param {DashboardWidget[]} layout Layout to canonicalize.\n * @return {DashboardWidget[]} The canonical layout.\n */\nexport function canonicalizeLayout(\n\tlayout: DashboardWidget[]\n): DashboardWidget[] {\n\tconst indexed = layout.map( ( widget, index ) => ( {\n\t\twidget,\n\t\torder: widget.placement?.order ?? index,\n\t} ) );\n\n\tindexed.sort( ( a, b ) => a.order - b.order );\n\n\tlet changed = false;\n\tconst canonical = indexed.map( ( { widget }, index ) => {\n\t\tif ( widget !== layout[ index ] ) {\n\t\t\tchanged = true;\n\t\t}\n\t\tif ( ! widget.placement || ! ( 'order' in widget.placement ) ) {\n\t\t\treturn widget;\n\t\t}\n\t\tchanged = true;\n\t\tconst { order: _stripped, ...placement } = widget.placement;\n\t\treturn { ...widget, placement };\n\t} );\n\n\treturn changed ? canonical : layout;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAeA,SAAgB,mBACf,QACoB;CACpB,MAAM,UAAU,OAAO,KAAO,QAAQ,WAAa;EAClD;EACA,OAAO,OAAO,WAAW,SAAS;CACnC,EAAI;CAEJ,QAAQ,MAAQ,GAAG,MAAO,EAAE,QAAQ,EAAE,KAAM;CAE5C,IAAI,UAAU;CACd,MAAM,YAAY,QAAQ,KAAO,EAAE,UAAU,UAAW;EACvD,IAAK,WAAW,OAAQ,QACvB,UAAU;EAEX,IAAK,CAAE,OAAO,aAAa,EAAI,WAAW,OAAO,YAChD,OAAO;EAER,UAAU;EACV,MAAM,EAAE,OAAO,WAAW,GAAG,cAAc,OAAO;EAClD,OAAO;GAAE,GAAG;GAAQ;EAAU;CAC/B,CAAE;CAEF,OAAO,UAAU,YAAY;AAC9B"}