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