Back

100% Statements 4/4
100% Branches 2/2
100% Functions 3/3
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                      26x       338x                       30x   30x    
/**
 * External dependencies
 */
import { MENU_SURFACE_PRESETS_WITH_ALL_TIME } from '@jetpack-premium-analytics/datetime';
import { resolveFieldTypes } from '@jetpack-premium-analytics/fields';
import { __ } from '@wordpress/i18n';
/**
 * Internal dependencies
 */
import type { SingleReportControlField, SingleReportControls } from './registry';
import type { Field } from '@jetpack-premium-analytics/externals';
 
/**
 * The item the page's controls edit: `reportParams` is the key the `jpa/report-params` field
 * reads and writes, `allTimeStart` the All time anchor it reads.
 */
export type ControlsItem = {
	reportParams: Record< string, unknown >;
	allTimeStart?: string;
};
 
/**
 * The control a record gets when it declares none: a date range over the detail presets.
 *
 * @return The field declaration.
 */
function defaultDateControl(): SingleReportControlField {
	return {
		id: 'reportParams',
		type: 'jpa/report-params',
		label: __( 'Date range', 'jetpack-premium-analytics-pkg' ),
		elements: MENU_SURFACE_PRESETS_WITH_ALL_TIME.map( value => ( { value, label: value } ) ),
	};
}
 
/**
 * The DataForm fields of a page's header controls: the fields the record declares, resolved
 * through the registered field types, or the default date control for a record without any.
 *
 * @param controls - The record's controls for the active section.
 * @return The resolved fields, empty for a page without controls.
 */
export function buildControlFields( controls: SingleReportControls ): Field< ControlsItem >[] {
	const declarations = controls ?? [ defaultDateControl() ];
 
	return resolveFieldTypes( declarations ) as unknown as Field< ControlsItem >[];
}