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