1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6// This MUST be first to ensure that `fetch` is defined in the React Native environment.
7require("react-native/Libraries/Core/InitializeCore");
8const expo_constants_1 = __importDefault(require("expo-constants"));
9const url_parse_1 = __importDefault(require("url-parse"));
10const Location_1 = require("./Location");
11const getDevServer_1 = __importDefault(require("../getDevServer"));
12let hasWarned = false;
13const manifest = expo_constants_1.default.expoConfig;
14// Add a development warning for fetch requests with relative paths
15// to ensure developers are aware of the need to configure a production
16// base URL in the Expo config (app.json) under `expo.extra.router.origin`.
17function warnProductionOriginNotConfigured(requestUrl) {
18    if (hasWarned) {
19        return;
20    }
21    hasWarned = true;
22    if (!manifest?.extra?.router?.origin) {
23        console.warn(`The relative fetch request "${requestUrl}" will not work in production until the Expo Router Config Plugin (app.json) is configured with the \`origin\` prop set to the base URL of your web server, e.g. \`{ plugins: [["expo-router", { origin: "..." }]] }\`. [Learn more](https://expo.github.io/router/docs/lab/runtime-location)`);
24    }
25}
26// TODO: This would be better if native and tied as close to the JS engine as possible, i.e. it should
27// reflect the exact location of the JS file that was executed.
28function getBaseUrl() {
29    if (process.env.NODE_ENV !== 'production') {
30        // e.g. http://localhost:19006
31        return (0, getDevServer_1.default)().url?.replace(/\/$/, '');
32    }
33    // TODO: Make it official by moving out of `extra`
34    const productionBaseUrl = manifest?.extra?.router?.origin;
35    if (!productionBaseUrl) {
36        return null;
37    }
38    // Ensure no trailing slash
39    return productionBaseUrl?.replace(/\/$/, '');
40}
41function wrapFetchWithWindowLocation(fetch) {
42    if (fetch.__EXPO_BASE_URL_POLYFILLED) {
43        return fetch;
44    }
45    const _fetch = (...props) => {
46        if (props[0] && typeof props[0] === 'string' && props[0].startsWith('/')) {
47            if (process.env.NODE_ENV !== 'production') {
48                warnProductionOriginNotConfigured(props[0]);
49            }
50            props[0] = new url_parse_1.default(props[0], window.location?.origin).toString();
51        }
52        else if (props[0] && typeof props[0] === 'object') {
53            if (props[0].url && typeof props[0].url === 'string' && props[0].url.startsWith('/')) {
54                if (process.env.NODE_ENV !== 'production') {
55                    warnProductionOriginNotConfigured(props[0]);
56                }
57                props[0].url = new url_parse_1.default(props[0].url, window.location?.origin).toString();
58            }
59        }
60        return fetch(...props);
61    };
62    _fetch.__EXPO_BASE_URL_POLYFILLED = true;
63    return _fetch;
64}
65if (manifest?.extra?.router?.origin !== false) {
66    // Polyfill window.location in native runtimes.
67    if (typeof window !== 'undefined' && !window.location) {
68        const url = getBaseUrl();
69        if (url) {
70            (0, Location_1.setLocationHref)(url);
71            (0, Location_1.install)();
72        }
73    }
74    // Polyfill native fetch to support relative URLs
75    Object.defineProperty(global, 'fetch', {
76        value: wrapFetchWithWindowLocation(fetch),
77    });
78}
79//# sourceMappingURL=install.native.js.map