1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.MATCH_INIT = void 0;
7exports.addGoogleMapsAppDelegateImport = addGoogleMapsAppDelegateImport;
8exports.addGoogleMapsAppDelegateInit = addGoogleMapsAppDelegateInit;
9exports.addMapsCocoaPods = addMapsCocoaPods;
10exports.getGoogleMapsApiKey = getGoogleMapsApiKey;
11exports.removeGoogleMapsAppDelegateImport = removeGoogleMapsAppDelegateImport;
12exports.removeGoogleMapsAppDelegateInit = removeGoogleMapsAppDelegateInit;
13exports.removeMapsCocoaPods = removeMapsCocoaPods;
14exports.setGoogleMapsApiKey = setGoogleMapsApiKey;
15exports.withMaps = void 0;
16
17function _fs() {
18  const data = _interopRequireDefault(require("fs"));
19
20  _fs = function () {
21    return data;
22  };
23
24  return data;
25}
26
27function _path() {
28  const data = _interopRequireDefault(require("path"));
29
30  _path = function () {
31    return data;
32  };
33
34  return data;
35}
36
37function _resolveFrom() {
38  const data = _interopRequireDefault(require("resolve-from"));
39
40  _resolveFrom = function () {
41    return data;
42  };
43
44  return data;
45}
46
47function _iosPlugins() {
48  const data = require("../plugins/ios-plugins");
49
50  _iosPlugins = function () {
51    return data;
52  };
53
54  return data;
55}
56
57function _withDangerousMod() {
58  const data = require("../plugins/withDangerousMod");
59
60  _withDangerousMod = function () {
61    return data;
62  };
63
64  return data;
65}
66
67function _generateCode() {
68  const data = require("../utils/generateCode");
69
70  _generateCode = function () {
71    return data;
72  };
73
74  return data;
75}
76
77function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
78
79const debug = require('debug')('expo:config-plugins:ios:maps'); // Match against `UMModuleRegistryAdapter` (unimodules), and React Native without unimodules (Expo Modules), and SDK +44 React AppDelegate subscriber.
80
81
82const MATCH_INIT = /(?:(self\.|_)(\w+)\s?=\s?\[\[UMModuleRegistryAdapter alloc\])|(?:RCTBridge\s?\*\s?(\w+)\s?=\s?\[\[RCTBridge alloc\])|(\[self\.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions\])/g;
83exports.MATCH_INIT = MATCH_INIT;
84const withGoogleMapsKey = (0, _iosPlugins().createInfoPlistPlugin)(setGoogleMapsApiKey, 'withGoogleMapsKey');
85
86const withMaps = config => {
87  config = withGoogleMapsKey(config);
88  const apiKey = getGoogleMapsApiKey(config); // Technically adds react-native-maps (Apple maps) and google maps.
89
90  debug('Google Maps API Key:', apiKey);
91  config = withMapsCocoaPods(config, {
92    useGoogleMaps: !!apiKey
93  }); // Adds/Removes AppDelegate setup for Google Maps API on iOS
94
95  config = withGoogleMapsAppDelegate(config, {
96    apiKey
97  });
98  return config;
99};
100
101exports.withMaps = withMaps;
102
103function getGoogleMapsApiKey(config) {
104  var _config$ios$config$go, _config$ios, _config$ios$config;
105
106  return (_config$ios$config$go = (_config$ios = config.ios) === null || _config$ios === void 0 ? void 0 : (_config$ios$config = _config$ios.config) === null || _config$ios$config === void 0 ? void 0 : _config$ios$config.googleMapsApiKey) !== null && _config$ios$config$go !== void 0 ? _config$ios$config$go : null;
107}
108
109function setGoogleMapsApiKey(config, {
110  GMSApiKey,
111  ...infoPlist
112}) {
113  const apiKey = getGoogleMapsApiKey(config);
114
115  if (apiKey === null) {
116    return infoPlist;
117  }
118
119  return { ...infoPlist,
120    GMSApiKey: apiKey
121  };
122}
123
124function addGoogleMapsAppDelegateImport(src) {
125  const newSrc = [];
126  newSrc.push('#if __has_include(<GoogleMaps/GoogleMaps.h>)', '#import <GoogleMaps/GoogleMaps.h>', '#endif');
127  return (0, _generateCode().mergeContents)({
128    tag: 'react-native-maps-import',
129    src,
130    newSrc: newSrc.join('\n'),
131    anchor: /#import "AppDelegate\.h"/,
132    offset: 1,
133    comment: '//'
134  });
135}
136
137function removeGoogleMapsAppDelegateImport(src) {
138  return (0, _generateCode().removeContents)({
139    tag: 'react-native-maps-import',
140    src
141  });
142}
143
144function addGoogleMapsAppDelegateInit(src, apiKey) {
145  const newSrc = [];
146  newSrc.push('#if __has_include(<GoogleMaps/GoogleMaps.h>)', `  [GMSServices provideAPIKey:@"${apiKey}"];`, '#endif');
147  return (0, _generateCode().mergeContents)({
148    tag: 'react-native-maps-init',
149    src,
150    newSrc: newSrc.join('\n'),
151    anchor: MATCH_INIT,
152    offset: 0,
153    comment: '//'
154  });
155}
156
157function removeGoogleMapsAppDelegateInit(src) {
158  return (0, _generateCode().removeContents)({
159    tag: 'react-native-maps-init',
160    src
161  });
162}
163/**
164 * @param src The contents of the Podfile.
165 * @returns Podfile with Google Maps added.
166 */
167
168
169function addMapsCocoaPods(src) {
170  return (0, _generateCode().mergeContents)({
171    tag: 'react-native-maps',
172    src,
173    newSrc: `  pod 'react-native-google-maps', path: File.dirname(\`node --print "require.resolve('react-native-maps/package.json')"\`)`,
174    anchor: /use_native_modules/,
175    offset: 0,
176    comment: '#'
177  });
178}
179
180function removeMapsCocoaPods(src) {
181  return (0, _generateCode().removeContents)({
182    tag: 'react-native-maps',
183    src
184  });
185}
186
187function isReactNativeMapsInstalled(projectRoot) {
188  const resolved = _resolveFrom().default.silent(projectRoot, 'react-native-maps/package.json');
189
190  return resolved ? _path().default.dirname(resolved) : null;
191}
192
193function isReactNativeMapsAutolinked(config) {
194  // Only add the native code changes if we know that the package is going to be linked natively.
195  // This is specifically for monorepo support where one app might have react-native-maps (adding it to the node_modules)
196  // but another app will not have it installed in the package.json, causing it to not be linked natively.
197  // This workaround only exists because react-native-maps doesn't have a config plugin vendored in the package.
198  // TODO: `react-native-maps` doesn't use Expo autolinking so we cannot safely disable the module.
199  return true; // return (
200  //   !config._internal?.autolinkedModules ||
201  //   config._internal.autolinkedModules.includes('react-native-maps')
202  // );
203}
204
205const withMapsCocoaPods = (config, {
206  useGoogleMaps
207}) => {
208  return (0, _withDangerousMod().withDangerousMod)(config, ['ios', async config => {
209    const filePath = _path().default.join(config.modRequest.platformProjectRoot, 'Podfile');
210
211    const contents = await _fs().default.promises.readFile(filePath, 'utf-8');
212    let results; // Only add the block if react-native-maps is installed in the project (best effort).
213    // Generally prebuild runs after a yarn install so this should always work as expected.
214
215    const googleMapsPath = isReactNativeMapsInstalled(config.modRequest.projectRoot);
216    const isLinked = isReactNativeMapsAutolinked(config);
217    debug('Is Expo Autolinked:', isLinked);
218    debug('react-native-maps path:', googleMapsPath);
219
220    if (isLinked && googleMapsPath && useGoogleMaps) {
221      try {
222        results = addMapsCocoaPods(contents);
223      } catch (error) {
224        if (error.code === 'ERR_NO_MATCH') {
225          throw new Error(`Cannot add react-native-maps to the project's ios/Podfile because it's malformed. Please report this with a copy of your project Podfile.`);
226        }
227
228        throw error;
229      }
230    } else {
231      // If the package is no longer installed, then remove the block.
232      results = removeMapsCocoaPods(contents);
233    }
234
235    if (results.didMerge || results.didClear) {
236      await _fs().default.promises.writeFile(filePath, results.contents);
237    }
238
239    return config;
240  }]);
241};
242
243const withGoogleMapsAppDelegate = (config, {
244  apiKey
245}) => {
246  return (0, _iosPlugins().withAppDelegate)(config, config => {
247    if (['objc', 'objcpp'].includes(config.modResults.language)) {
248      if (apiKey && isReactNativeMapsAutolinked(config) && isReactNativeMapsInstalled(config.modRequest.projectRoot)) {
249        try {
250          config.modResults.contents = addGoogleMapsAppDelegateImport(config.modResults.contents).contents;
251          config.modResults.contents = addGoogleMapsAppDelegateInit(config.modResults.contents, apiKey).contents;
252        } catch (error) {
253          if (error.code === 'ERR_NO_MATCH') {
254            throw new Error(`Cannot add Google Maps to the project's AppDelegate because it's malformed. Please report this with a copy of your project AppDelegate.`);
255          }
256
257          throw error;
258        }
259      } else {
260        config.modResults.contents = removeGoogleMapsAppDelegateImport(config.modResults.contents).contents;
261        config.modResults.contents = removeGoogleMapsAppDelegateInit(config.modResults.contents).contents;
262      }
263    } else {
264      throw new Error(`Cannot setup Google Maps because the project AppDelegate is not a supported language: ${config.modResults.language}`);
265    }
266
267    return config;
268  });
269};
270//# sourceMappingURL=Maps.js.map