export function markPreparationDirty( form: HTMLFormElement, dirty: boolean, managed = false, ): void { if (managed) form.setAttribute('data-preparation-managed', 'true'); // Explicit query forms change consultation criteria, not a business preparation. form.setAttribute( 'data-preparation-dirty', dirty && form.getAttribute('data-query-form') !== 'true' ? 'true' : 'false', ); } /** A form that publishes its own dirty state owns clearing and restoring it. */ export function usesGenericPreparationTracking(form: HTMLFormElement): boolean { return ( form.getAttribute('data-query-form') !== 'true' && form.getAttribute('data-preparation-managed') !== 'true' ); } /** Recover restored preparations even when their effect ran before the guard subscribed. */ export function connectedDirtyForms( tracked: Iterable, root: Pick = document, ): HTMLFormElement[] { return [ ...new Set([ ...tracked, ...root.querySelectorAll( 'form[data-preparation-dirty="true"]', ), ]), ].filter( (form) => form.isConnected && form.getAttribute('data-query-form') !== 'true' && form.getAttribute('data-preparation-dirty') !== 'false', ); }