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 });
6const config_plugins_1 = require("@expo/config-plugins");
7const fs_1 = __importDefault(require("fs"));
8const path_1 = __importDefault(require("path"));
9const withDevMenuAppDelegate_1 = require("./withDevMenuAppDelegate");
10const pkg = require('expo-dev-menu/package.json');
11const DEV_MENU_ANDROID_IMPORT = 'expo.modules.devmenu.react.DevMenuAwareReactActivity';
12const DEV_MENU_ACTIVITY_CLASS = 'public class MainActivity extends DevMenuAwareReactActivity {';
13const DEV_MENU_POD_IMPORT = "pod 'expo-dev-menu', path: '../node_modules/expo-dev-menu', :configurations => :debug";
14async function readFileAsync(path) {
15    return fs_1.default.promises.readFile(path, 'utf8');
16}
17async function saveFileAsync(path, content) {
18    return fs_1.default.promises.writeFile(path, content, 'utf8');
19}
20function addJavaImports(javaSource, javaImports) {
21    const lines = javaSource.split('\n');
22    const lineIndexWithPackageDeclaration = lines.findIndex(line => line.match(/^package .*;$/));
23    for (const javaImport of javaImports) {
24        if (!javaSource.includes(javaImport)) {
25            const importStatement = `import ${javaImport};`;
26            lines.splice(lineIndexWithPackageDeclaration + 1, 0, importStatement);
27        }
28    }
29    return lines.join('\n');
30}
31function addLines(content, find, offset, toAdd) {
32    const lines = content.split('\n');
33    let lineIndex = lines.findIndex(line => line.match(find));
34    for (const newLine of toAdd) {
35        if (!content.includes(newLine)) {
36            lines.splice(lineIndex + offset, 0, newLine);
37            lineIndex++;
38        }
39    }
40    return lines.join('\n');
41}
42async function editPodfile(config, action) {
43    const podfilePath = path_1.default.join(config.modRequest.platformProjectRoot, 'Podfile');
44    try {
45        const podfile = action(await readFileAsync(podfilePath));
46        return await saveFileAsync(podfilePath, podfile);
47    }
48    catch (e) {
49        config_plugins_1.WarningAggregator.addWarningIOS('expo-dev-menu', `Couldn't modified AppDelegate.m - ${e}.`);
50    }
51}
52const withDevMenuActivity = config => {
53    return config_plugins_1.withMainActivity(config, config => {
54        if (config.modResults.language === 'java') {
55            let content = config.modResults.contents;
56            content = addJavaImports(content, [DEV_MENU_ANDROID_IMPORT]);
57            content = content.replace('public class MainActivity extends ReactActivity {', DEV_MENU_ACTIVITY_CLASS);
58            config.modResults.contents = content;
59        }
60        else {
61            config_plugins_1.WarningAggregator.addWarningAndroid('expo-dev-menu', `Cannot automatically configure MainActivity if it's not java`);
62        }
63        return config;
64    });
65};
66const withDevMenuPodfile = config => {
67    return config_plugins_1.withDangerousMod(config, [
68        'ios',
69        async (config) => {
70            await editPodfile(config, podfile => {
71                podfile = podfile.replace("platform :ios, '10.0'", "platform :ios, '11.0'");
72                // Match both variations of Ruby config:
73                // unknown: pod 'expo-dev-menu', path: '../node_modules/expo-dev-menu', :configurations => :debug
74                // Rubocop: pod 'expo-dev-menu', path: '../node_modules/expo-dev-menu', configurations: :debug
75                if (!podfile.match(/pod ['"]expo-dev-menu['"],\s?path: ['"]\.\.\/node_modules\/expo-dev-menu['"],\s?:?configurations:?\s(?:=>\s)?:debug/)) {
76                    podfile = addLines(podfile, 'use_react_native', 0, [`  ${DEV_MENU_POD_IMPORT}`]);
77                }
78                return podfile;
79            });
80            return config;
81        },
82    ]);
83};
84const withDevMenu = (config) => {
85    config = withDevMenuActivity(config);
86    config = withDevMenuPodfile(config);
87    config = withDevMenuAppDelegate_1.withDevMenuAppDelegate(config);
88    return config;
89};
90exports.default = config_plugins_1.createRunOncePlugin(withDevMenu, pkg.name, pkg.version);
91