var _a, _b, _c, _d, _e, _f;
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
apiFetch.fetchAllMiddleware = null;
if ((_a = window === null || window === void 0 ? void 0 : window.scData) === null || _a === void 0 ? void 0 : _a.root_url) {
  apiFetch.use(apiFetch.createRootURLMiddleware((_b = window === null || window === void 0 ? void 0 : window.scData) === null || _b === void 0 ? void 0 : _b.root_url));
}
if ((_c = window === null || window === void 0 ? void 0 : window.scData) === null || _c === void 0 ? void 0 : _c.nonce) {
  // @ts-ignore
  apiFetch.nonceMiddleware = apiFetch.createNonceMiddleware((_d = window === null || window === void 0 ? void 0 : window.scData) === null || _d === void 0 ? void 0 : _d.nonce);
  // @ts-ignore
  apiFetch.use(apiFetch.nonceMiddleware);
}
if ((_e = window === null || window === void 0 ? void 0 : window.scData) === null || _e === void 0 ? void 0 : _e.nonce_endpoint) {
  // @ts-ignore
  apiFetch.nonceEndpoint = (_f = window === null || window === void 0 ? void 0 : window.scData) === null || _f === void 0 ? void 0 : _f.nonce_endpoint;
}
// Add a timestamp so it can bypass cache rest api
apiFetch.use((options, next) => {
  options.path = addQueryArgs(options.path, { t: Date.now() });
  return next(options);
});
export default apiFetch;
