/** * External dependencies */ import type { Maybe, SiteId, Url } from '@nab/types'; /** * Internal dependencies */ import { parseJavaScript } from '../internal/parse-javascript'; import type { Settings } from '../../../types'; export const getSettings = (): Maybe< Required< Settings > > => { const win = window as unknown; if ( ! hasSettings( win ) ) { return; } const settings = win.nabSettings; return { ajaxUrl: '' as Url, alternativeChecksum: '', alternativeUrls: undefined, api: { mode: 'native', url: '' as Url }, cookieTesting: false, debugGui: false, excludeBots: true, forceECommerceSessionSync: false, gdprCookie: { name: '', value: '' }, heatmaps: [], hideQueryArgs: false, homeUrl: '' as Url, ignoreTrailingSlash: true, isGA4Integrated: false, ga4TrackingMethod: false, isStagingSite: false, isTestedPostRequest: false, maxCombinations: 24, nabPosition: 'last', optimizeXPath: true, participationChance: 100, postId: 0, preloadQueryArgUrls: [], segmentMatching: 'all', singleConvPerView: true, site: '' as SiteId, timezone: '', useControlUrl: false, useSendBeacon: true, version: 'unknown', ...settings, experiments: parseJavaScript( settings.experiments ), throttle: { global: fixThrottleWait( settings.throttle?.global ), woocommerce: fixThrottleWait( settings.throttle?.woocommerce ), }, }; }; // ======== // INTERNAL // ======== const hasSettings = ( win: unknown ): win is { nabSettings: Partial< Settings > } => !! win && 'object' === typeof win && 'nabSettings' in win; function fixThrottleWait( wait = 0 ): number { return Math.max( 0, Math.min( 10, wait ) ); }