/**
 * TrustLens — Blocks checkout integration.
 *
 * Wraps window.fetch so Store API requests to /wc/store/v1/* carry an
 * X-TL-FP header with the fingerprint hash computed by fingerprint.js.
 *
 * Safe to load globally on checkout/cart pages. Only rewrites Store API
 * requests; all other fetches pass through unchanged.
 *
 * @since 1.3.0
 */
( function () {
	'use strict';

	if ( ! window.fetch ) { return; }
	var origFetch = window.fetch;

	function isStoreApi( input ) {
		try {
			var url = ( typeof input === 'string' ) ? input : input.url;
			return url && url.indexOf( '/wc/store/v' ) !== -1;
		} catch ( e ) { return false; }
	}

	window.fetch = function ( input, init ) {
		if ( ! isStoreApi( input ) ) {
			return origFetch.call( this, input, init );
		}
		var fp = window.__tlFingerprint || '';
		if ( ! fp ) {
			return origFetch.call( this, input, init );
		}
		init = init || {};
		var headers = new Headers( init.headers || ( typeof input !== 'string' ? input.headers : undefined ) );
		headers.set( 'X-TL-FP', fp );
		init.headers = headers;
		return origFetch.call( this, input, init );
	};
} )();
