1{"version":3,"file":"Maps.js","names":["debug","require","MATCH_INIT","withGoogleMapsKey","createInfoPlistPlugin","setGoogleMapsApiKey","withMaps","config","apiKey","getGoogleMapsApiKey","withMapsCocoaPods","useGoogleMaps","withGoogleMapsAppDelegate","ios","googleMapsApiKey","GMSApiKey","infoPlist","addGoogleMapsAppDelegateImport","src","newSrc","push","mergeContents","tag","join","anchor","offset","comment","removeGoogleMapsAppDelegateImport","removeContents","addGoogleMapsAppDelegateInit","removeGoogleMapsAppDelegateInit","addMapsCocoaPods","removeMapsCocoaPods","isReactNativeMapsInstalled","projectRoot","resolved","resolveFrom","silent","path","dirname","isReactNativeMapsAutolinked","withDangerousMod","filePath","modRequest","platformProjectRoot","contents","fs","promises","readFile","results","googleMapsPath","isLinked","error","code","Error","didMerge","didClear","writeFile","withAppDelegate","includes","modResults","language"],"sources":["../../src/ios/Maps.ts"],"sourcesContent":["import { ExpoConfig } from '@expo/config-types';\nimport fs from 'fs';\nimport path from 'path';\nimport resolveFrom from 'resolve-from';\n\nimport { ConfigPlugin, InfoPlist } from '../Plugin.types';\nimport { createInfoPlistPlugin, withAppDelegate } from '../plugins/ios-plugins';\nimport { withDangerousMod } from '../plugins/withDangerousMod';\nimport { mergeContents, MergeResults, removeContents } from '../utils/generateCode';\n\nconst debug = require('debug')('expo:config-plugins:ios:maps') as typeof console.log;\n\n// Match against `UMModuleRegistryAdapter` (unimodules), and React Native without unimodules (Expo Modules), and SDK +44 React AppDelegate subscriber.\nexport const MATCH_INIT =\n  /(?:(self\\.|_)(\\w+)\\s?=\\s?\\[\\[UMModuleRegistryAdapter alloc\\])|(?:RCTBridge\\s?\\*\\s?(\\w+)\\s?=\\s?\\[\\[RCTBridge alloc\\])|(\\[self\\.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions\\])/g;\n\nconst withGoogleMapsKey = createInfoPlistPlugin(setGoogleMapsApiKey, 'withGoogleMapsKey');\n\nexport const withMaps: ConfigPlugin = (config) => {\n  config = withGoogleMapsKey(config);\n\n  const apiKey = getGoogleMapsApiKey(config);\n  // Technically adds react-native-maps (Apple maps) and google maps.\n\n  debug('Google Maps API Key:', apiKey);\n  config = withMapsCocoaPods(config, { useGoogleMaps: !!apiKey });\n\n  // Adds/Removes AppDelegate setup for Google Maps API on iOS\n  config = withGoogleMapsAppDelegate(config, { apiKey });\n\n  return config;\n};\n\nexport function getGoogleMapsApiKey(config: Pick<ExpoConfig, 'ios'>) {\n  return config.ios?.config?.googleMapsApiKey ?? null;\n}\n\nexport function setGoogleMapsApiKey(\n  config: Pick<ExpoConfig, 'ios'>,\n  { GMSApiKey, ...infoPlist }: InfoPlist\n): InfoPlist {\n  const apiKey = getGoogleMapsApiKey(config);\n\n  if (apiKey === null) {\n    return infoPlist;\n  }\n\n  return {\n    ...infoPlist,\n    GMSApiKey: apiKey,\n  };\n}\n\nexport function addGoogleMapsAppDelegateImport(src: string): MergeResults {\n  const newSrc = [];\n  newSrc.push(\n    '#if __has_include(<GoogleMaps/GoogleMaps.h>)',\n    '#import <GoogleMaps/GoogleMaps.h>',\n    '#endif'\n  );\n\n  return mergeContents({\n    tag: 'react-native-maps-import',\n    src,\n    newSrc: newSrc.join('\\n'),\n    anchor: /#import \"AppDelegate\\.h\"/,\n    offset: 1,\n    comment: '//',\n  });\n}\n\nexport function removeGoogleMapsAppDelegateImport(src: string): MergeResults {\n  return removeContents({\n    tag: 'react-native-maps-import',\n    src,\n  });\n}\n\nexport function addGoogleMapsAppDelegateInit(src: string, apiKey: string): MergeResults {\n  const newSrc = [];\n  newSrc.push(\n    '#if __has_include(<GoogleMaps/GoogleMaps.h>)',\n    `  [GMSServices provideAPIKey:@\"${apiKey}\"];`,\n    '#endif'\n  );\n\n  return mergeContents({\n    tag: 'react-native-maps-init',\n    src,\n    newSrc: newSrc.join('\\n'),\n    anchor: MATCH_INIT,\n    offset: 0,\n    comment: '//',\n  });\n}\n\nexport function removeGoogleMapsAppDelegateInit(src: string): MergeResults {\n  return removeContents({\n    tag: 'react-native-maps-init',\n    src,\n  });\n}\n\n/**\n * @param src The contents of the Podfile.\n * @returns Podfile with Google Maps added.\n */\nexport function addMapsCocoaPods(src: string): MergeResults {\n  return mergeContents({\n    tag: 'react-native-maps',\n    src,\n    newSrc: `  pod 'react-native-google-maps', path: File.dirname(\\`node --print \"require.resolve('react-native-maps/package.json')\"\\`)`,\n    anchor: /use_native_modules/,\n    offset: 0,\n    comment: '#',\n  });\n}\n\nexport function removeMapsCocoaPods(src: string): MergeResults {\n  return removeContents({\n    tag: 'react-native-maps',\n    src,\n  });\n}\n\nfunction isReactNativeMapsInstalled(projectRoot: string): string | null {\n  const resolved = resolveFrom.silent(projectRoot, 'react-native-maps/package.json');\n  return resolved ? path.dirname(resolved) : null;\n}\n\nfunction isReactNativeMapsAutolinked(config: Pick<ExpoConfig, '_internal'>): boolean {\n  // Only add the native code changes if we know that the package is going to be linked natively.\n  // This is specifically for monorepo support where one app might have react-native-maps (adding it to the node_modules)\n  // but another app will not have it installed in the package.json, causing it to not be linked natively.\n  // This workaround only exists because react-native-maps doesn't have a config plugin vendored in the package.\n\n  // TODO: `react-native-maps` doesn't use Expo autolinking so we cannot safely disable the module.\n  return true;\n\n  // return (\n  //   !config._internal?.autolinkedModules ||\n  //   config._internal.autolinkedModules.includes('react-native-maps')\n  // );\n}\n\nconst withMapsCocoaPods: ConfigPlugin<{ useGoogleMaps: boolean }> = (config, { useGoogleMaps }) => {\n  return withDangerousMod(config, [\n    'ios',\n    async (config) => {\n      const filePath = path.join(config.modRequest.platformProjectRoot, 'Podfile');\n      const contents = await fs.promises.readFile(filePath, 'utf-8');\n      let results: MergeResults;\n      // Only add the block if react-native-maps is installed in the project (best effort).\n      // Generally prebuild runs after a yarn install so this should always work as expected.\n      const googleMapsPath = isReactNativeMapsInstalled(config.modRequest.projectRoot);\n      const isLinked = isReactNativeMapsAutolinked(config);\n      debug('Is Expo Autolinked:', isLinked);\n      debug('react-native-maps path:', googleMapsPath);\n      if (isLinked && googleMapsPath && useGoogleMaps) {\n        try {\n          results = addMapsCocoaPods(contents);\n        } catch (error: any) {\n          if (error.code === 'ERR_NO_MATCH') {\n            throw new Error(\n              `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.`\n            );\n          }\n          throw error;\n        }\n      } else {\n        // If the package is no longer installed, then remove the block.\n        results = removeMapsCocoaPods(contents);\n      }\n      if (results.didMerge || results.didClear) {\n        await fs.promises.writeFile(filePath, results.contents);\n      }\n      return config;\n    },\n  ]);\n};\n\nconst withGoogleMapsAppDelegate: ConfigPlugin<{ apiKey: string | null }> = (config, { apiKey }) => {\n  return withAppDelegate(config, (config) => {\n    if (['objc', 'objcpp'].includes(config.modResults.language)) {\n      if (\n        apiKey &&\n        isReactNativeMapsAutolinked(config) &&\n        isReactNativeMapsInstalled(config.modRequest.projectRoot)\n      ) {\n        try {\n          config.modResults.contents = addGoogleMapsAppDelegateImport(\n            config.modResults.contents\n          ).contents;\n          config.modResults.contents = addGoogleMapsAppDelegateInit(\n            config.modResults.contents,\n            apiKey\n          ).contents;\n        } catch (error: any) {\n          if (error.code === 'ERR_NO_MATCH') {\n            throw new Error(\n              `Cannot add Google Maps to the project's AppDelegate because it's malformed. Please report this with a copy of your project AppDelegate.`\n            );\n          }\n          throw error;\n        }\n      } else {\n        config.modResults.contents = removeGoogleMapsAppDelegateImport(\n          config.modResults.contents\n        ).contents;\n        config.modResults.contents = removeGoogleMapsAppDelegateInit(\n          config.modResults.contents\n        ).contents;\n      }\n    } else {\n      throw new Error(\n        `Cannot setup Google Maps because the project AppDelegate is not a supported language: ${config.modResults.language}`\n      );\n    }\n    return config;\n  });\n};\n"],"mappings":";;;;;;;;;;;;;;;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAGA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA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