{
  "version": 3,
  "sources": ["../../src/context/dashboard-context.tsx"],
  "sourcesContent": ["import fastDeepEqual from 'fast-deep-equal/es6/index.js';\nimport type { ReactNode } from 'react';\nimport { debounce, useEvent } from '@wordpress/compose';\nimport {\n\tcreateContext,\n\tuseCallback,\n\tuseContext,\n\tuseEffect,\n\tuseMemo,\n\tuseState,\n} from '@wordpress/element';\nimport type {\n\tResolveWidgetModule,\n\tWidgetType,\n} from '@wordpress/widget-primitives';\nimport { canonicalizeLayout } from '../utils/canonicalize-layout';\nimport { DEFAULT_GRID } from '../utils/default-grid';\nimport { enforceLayoutPolicy } from '../utils/enforce-layout-policy';\nimport { normalizeGridSettings } from '../utils/normalize-grid-settings';\nimport { resolveDashboardColumnCap } from '../utils/resolve-dashboard-column-count/resolve-dashboard-column-count';\nimport { DEFAULT_ROW_HEIGHT } from '../utils/row-height-presets';\nimport { useDashboardPolicy } from '../components/dashboard-policy';\nimport type {\n\tCanPerformDashboardOperation,\n\tWidgetGridSettings,\n\tDashboardWidget,\n} from '../types';\n\ntype GridSettingsWithColumns = WidgetGridSettings & { columns: number };\n\nfunction resolveGridSettings(\n\tsettings: WidgetGridSettings\n): GridSettingsWithColumns {\n\tconst normalized = normalizeGridSettings( settings, DEFAULT_ROW_HEIGHT );\n\treturn {\n\t\t...normalized,\n\t\tcolumns: resolveDashboardColumnCap( normalized.columns ),\n\t};\n}\n\nconst DEFAULT_RESOLVE_WIDGET_MODULE: ResolveWidgetModule = ( moduleId ) =>\n\timport( /* webpackIgnore: true */ /* @vite-ignore */ moduleId );\n\n/**\n * Inline widget-instance edits stage live, then publish once the user pauses.\n * A single global timer, so editing several widgets settles into one save.\n */\nconst AUTO_SAVE_DELAY_MS = 5000;\n\n/**\n * Rich state distributed to every compound component inside `WidgetDashboard`.\n * Internal — compounds reach the full state via `useDashboardInternalContext()`.\n *\n * `layout`/`onLayoutChange` here operate on the staging layer, not the\n * committed props. Mutations from compound children stay in staging until\n * `commit` publishes them on the consumer.\n */\ninterface InternalDashboardContextValue {\n\twidgetTypes: WidgetType[];\n\tisResolvingWidgetTypes: boolean;\n\tlayout: DashboardWidget[];\n\tonLayoutChange: ( layout: DashboardWidget[] ) => void;\n\tonLayoutReset?: () => void;\n\tgridSettings: GridSettingsWithColumns;\n\n\t/**\n\t * Publishes the staged layout when it differs from the committed\n\t * prop. By default also exits edit mode; pass `{ exitEditMode: false }`\n\t * for inline auto-saves that keep the current mode.\n\t */\n\tcommit: ( options?: CommitOptions ) => void;\n\n\t/**\n\t * Reverts the staged layout and exits edit mode.\n\t */\n\tcancel: () => void;\n\n\t/**\n\t * Debounced auto-save for inline widget-instance edits. Controls call it\n\t * after staging a change; a single global timer publishes once the edits\n\t * settle. The settings drawer does not use it (it commits on Save).\n\t */\n\tscheduleAutoSave: () => void;\n\n\t/**\n\t * Publishes any pending auto-save immediately. Called when leaving the\n\t * inline surface (opening the drawer, entering customize) so staged inline\n\t * edits do not commingle with the drawer's explicit-save flow.\n\t */\n\tflushAutoSave: () => void;\n\n\thasUncommittedChanges: boolean;\n\teditMode: boolean;\n\tonEditChange?: ( next: boolean ) => void;\n\tresolveWidgetModule: ResolveWidgetModule;\n\n\t/**\n\t * Resolved policy. Every compound asks here, never the policy provider,\n\t * so further policy sources compose at this single point.\n\t */\n\tcanPerform: CanPerformDashboardOperation;\n}\n\nconst ALLOW_EVERY_OPERATION: CanPerformDashboardOperation = () => true;\n\ninterface CommitOptions {\n\texitEditMode?: boolean;\n}\n\nconst Context = createContext< InternalDashboardContextValue | null >( null );\n\n/**\n * Compound-internal hook — exposes the full provider state.\n * Not part of the public API; lives in the same module\n * so compound components can reach the state directly.\n */\nexport function useDashboardInternalContext(): InternalDashboardContextValue {\n\tconst ctx = useContext( Context );\n\tif ( ! ctx ) {\n\t\tthrow new Error(\n\t\t\t'Dashboard compound used outside a WidgetDashboard subtree.'\n\t\t);\n\t}\n\treturn ctx;\n}\n\ninterface ProviderProps {\n\twidgetTypes: WidgetType[];\n\tisResolvingWidgetTypes?: boolean;\n\tlayout: DashboardWidget[];\n\n\t/**\n\t * Fired on commit when the staged layout differs from `layout`.\n\t */\n\tonLayoutChange: ( layout: DashboardWidget[] ) => void;\n\n\t/**\n\t * Optional reset action surfaced by the bundled `Actions`.\n\t */\n\tonLayoutReset?: () => void;\n\n\teditMode?: boolean;\n\tonEditChange?: ( next: boolean ) => void;\n\n\t/**\n\t * Overrides the default `import()` resolution of\n\t * `WidgetType.renderModule`.\n\t */\n\tresolveWidgetModule?: ResolveWidgetModule;\n\n\tgridSettings?: WidgetGridSettings;\n\n\tchildren: ReactNode;\n}\n\n/**\n * Provider for the dashboard's staging layer. Owns the staging copy of\n * `layout`; `commit` publishes it when it differs from the committed\n * prop, `cancel` reverts it.\n *\n * Staging re-syncs from the committed prop on prop change. In-flight\n * edits are dropped silently when an external update (cross-tab commit,\n * reset, websocket push) lands. Consumers that cannot tolerate this\n * loss should mediate the prop updates before forwarding them here.\n *\n * @param {ProviderProps} props Component props.\n */\nexport function WidgetDashboardProvider( {\n\twidgetTypes,\n\tisResolvingWidgetTypes = false,\n\tlayout: committedLayout,\n\tonLayoutChange,\n\tonLayoutReset,\n\teditMode = false,\n\tonEditChange,\n\tresolveWidgetModule = DEFAULT_RESOLVE_WIDGET_MODULE,\n\tgridSettings: committedGridSettings = DEFAULT_GRID,\n\tchildren,\n}: ProviderProps ): React.ReactNode {\n\tconst [ stagingLayout, setStagingLayout ] =\n\t\tuseState< DashboardWidget[] >( committedLayout );\n\n\tconst policy = useDashboardPolicy();\n\n\t// Normalized once, so every surface reads a boolean: an `undefined`\n\t// answer denies everywhere instead of hiding the controls while the grid\n\t// still lets the tile drag and resize.\n\tconst canPerform = useMemo< CanPerformDashboardOperation >(\n\t\t() =>\n\t\t\tpolicy\n\t\t\t\t? ( request ) => !! policy( request )\n\t\t\t\t: ALLOW_EVERY_OPERATION,\n\t\t[ policy ]\n\t);\n\n\t// Every mutation stages through here, diffed against the current\n\t// staging so every change the policy denies is re-asserted before it\n\t// lands: what the interface hides, the staging layer rejects. Without\n\t// a policy there is nothing to enforce, and staging stays byte-equal\n\t// to what the trigger wrote.\n\tconst stageLayout = useCallback(\n\t\t( next: DashboardWidget[] ) => {\n\t\t\tsetStagingLayout( ( previous ) =>\n\t\t\t\tcanPerform === ALLOW_EVERY_OPERATION\n\t\t\t\t\t? next\n\t\t\t\t\t: enforceLayoutPolicy( {\n\t\t\t\t\t\t\tprevious,\n\t\t\t\t\t\t\tnext,\n\t\t\t\t\t\t\tcanPerform,\n\t\t\t\t\t\t\twidgetTypes,\n\t\t\t\t\t  } )\n\t\t\t);\n\t\t},\n\t\t[ canPerform, widgetTypes ]\n\t);\n\n\t// External change in `layout` (consumer-side reset, cross-tab sync,\n\t// websocket push, etc.) drops any in-flight staging edits without\n\t// surfacing a warning. See the provider JSDoc for the trade-off.\n\tuseEffect( () => {\n\t\tsetStagingLayout( committedLayout );\n\t}, [ committedLayout ] );\n\n\tconst gridSettings = useMemo(\n\t\t() => resolveGridSettings( committedGridSettings ),\n\t\t[ committedGridSettings ]\n\t);\n\n\tconst hasLayoutChanges = useMemo(\n\t\t() =>\n\t\t\t! fastDeepEqual(\n\t\t\t\tcanonicalizeLayout( committedLayout ),\n\t\t\t\tcanonicalizeLayout( stagingLayout )\n\t\t\t),\n\t\t[ committedLayout, stagingLayout ]\n\t);\n\n\tconst hasUncommittedChanges = hasLayoutChanges;\n\n\tconst commit = useCallback(\n\t\t( options?: CommitOptions ) => {\n\t\t\tif ( hasLayoutChanges ) {\n\t\t\t\tonLayoutChange( canonicalizeLayout( stagingLayout ) );\n\t\t\t}\n\n\t\t\tif ( options?.exitEditMode !== false ) {\n\t\t\t\tonEditChange?.( false );\n\t\t\t}\n\t\t},\n\t\t[ hasLayoutChanges, onLayoutChange, stagingLayout, onEditChange ]\n\t);\n\n\t// Auto-save for inline edits.\n\t// A single debounced timer publishes through `useEvent`, so it always\n\t// reads the latest `commit` (and so the current staging) without\n\t// resetting on staging re-renders.\n\tconst publishAutoSave = useEvent( () => commit( { exitEditMode: false } ) );\n\n\tconst scheduleAutoSave = useMemo(\n\t\t() => debounce( publishAutoSave, AUTO_SAVE_DELAY_MS ),\n\t\t[ publishAutoSave ]\n\t);\n\n\tconst flushAutoSave = useCallback(\n\t\t() => scheduleAutoSave.flush(),\n\t\t[ scheduleAutoSave ]\n\t);\n\n\t// Entering customize flushes any pending inline save first, so it does not\n\t// commingle with the layout edit flow.\n\tuseEffect( () => {\n\t\tif ( ! editMode ) {\n\t\t\treturn;\n\t\t}\n\n\t\tscheduleAutoSave.flush();\n\t}, [ editMode, scheduleAutoSave ] );\n\n\t// Flush, not cancel, on unmount: an edit still inside the debounce window\n\t// must persist when the user navigates away from the dashboard.\n\tuseEffect( () => () => scheduleAutoSave.flush(), [ scheduleAutoSave ] );\n\n\tconst cancel = useCallback( () => {\n\t\tsetStagingLayout( committedLayout );\n\t\tonEditChange?.( false );\n\t}, [ committedLayout, onEditChange ] );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\tstagingLayout.length === 0 &&\n\t\t\tcanPerform( { operation: 'customize' } )\n\t\t) {\n\t\t\tonEditChange?.( true );\n\t\t}\n\n\t\t// Only react to the layout count flipping to zero; firing on every\n\t\t// onEditChange identity change would also reopen edit mode after the\n\t\t// user explicitly closed it on a non-empty layout.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [ stagingLayout.length === 0 ] );\n\n\tconst value = useMemo< InternalDashboardContextValue >(\n\t\t() => ( {\n\t\t\twidgetTypes,\n\t\t\tisResolvingWidgetTypes,\n\t\t\tlayout: stagingLayout,\n\t\t\tonLayoutChange: stageLayout,\n\t\t\tonLayoutReset,\n\t\t\tgridSettings,\n\t\t\tcommit,\n\t\t\tcancel,\n\t\t\tscheduleAutoSave,\n\t\t\tflushAutoSave,\n\t\t\thasUncommittedChanges,\n\t\t\teditMode,\n\t\t\tonEditChange,\n\t\t\tresolveWidgetModule,\n\t\t\tcanPerform,\n\t\t} ),\n\t\t[\n\t\t\twidgetTypes,\n\t\t\tisResolvingWidgetTypes,\n\t\t\tstagingLayout,\n\t\t\tstageLayout,\n\t\t\tonLayoutReset,\n\t\t\tgridSettings,\n\t\t\tcommit,\n\t\t\tcancel,\n\t\t\tscheduleAutoSave,\n\t\t\tflushAutoSave,\n\t\t\thasUncommittedChanges,\n\t\t\teditMode,\n\t\t\tonEditChange,\n\t\t\tresolveWidgetModule,\n\t\t\tcanPerform,\n\t\t]\n\t);\n\n\treturn <Context.Provider value={ value }>{ children }</Context.Provider>;\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAA0B;AAE1B,qBAAmC;AACnC,qBAOO;AAKP,iCAAmC;AACnC,0BAA6B;AAC7B,mCAAoC;AACpC,qCAAsC;AACtC,4CAA0C;AAC1C,gCAAmC;AACnC,8BAAmC;AA6T3B;AApTR,SAAS,oBACR,UAC0B;AAC1B,QAAM,iBAAa,sDAAuB,UAAU,4CAAmB;AACvE,SAAO;AAAA,IACN,GAAG;AAAA,IACH,aAAS,iEAA2B,WAAW,OAAQ;AAAA,EACxD;AACD;AAEA,IAAM,gCAAqD,CAAE,aAC5D;AAAA;AAAA;AAAA,EAAqD;AAAA;AAMtD,IAAM,qBAAqB;AAwD3B,IAAM,wBAAsD,MAAM;AAMlE,IAAM,cAAU,8BAAuD,IAAK;AAOrE,SAAS,8BAA6D;AAC5E,QAAM,UAAM,2BAAY,OAAQ;AAChC,MAAK,CAAE,KAAM;AACZ,UAAM,IAAI;AAAA,MACT;AAAA,IACD;AAAA,EACD;AACA,SAAO;AACR;AA2CO,SAAS,wBAAyB;AAAA,EACxC;AAAA,EACA,yBAAyB;AAAA,EACzB,QAAQ;AAAA,EACR;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,EACA,sBAAsB;AAAA,EACtB,cAAc,wBAAwB;AAAA,EACtC;AACD,GAAoC;AACnC,QAAM,CAAE,eAAe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