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 });
6exports.removeExpoSchemaFromVerifiedIntentFilters = exports.setGeneratedAndroidScheme = exports.withGeneratedAndroidScheme = void 0;
7const config_plugins_1 = require("@expo/config-plugins");
8const getDefaultScheme_1 = __importDefault(require("./getDefaultScheme"));
9const withGeneratedAndroidScheme = (config) => {
10    return (0, config_plugins_1.withAndroidManifest)(config, (config) => {
11        config.modResults = setGeneratedAndroidScheme(config, config.modResults);
12        config.modResults = removeExpoSchemaFromVerifiedIntentFilters(config, config.modResults);
13        return config;
14    });
15};
16exports.withGeneratedAndroidScheme = withGeneratedAndroidScheme;
17function setGeneratedAndroidScheme(config, androidManifest) {
18    // Generate a cross-platform scheme used to launch the dev client.
19    const scheme = (0, getDefaultScheme_1.default)(config);
20    if (!config_plugins_1.AndroidConfig.Scheme.hasScheme(scheme, androidManifest)) {
21        androidManifest = config_plugins_1.AndroidConfig.Scheme.appendScheme(scheme, androidManifest);
22    }
23    return androidManifest;
24}
25exports.setGeneratedAndroidScheme = setGeneratedAndroidScheme;
26/**
27 * Remove the custom Expo dev client scheme from intent filters, which are set to `autoVerify=true`.
28 * The custom scheme `<data android:scheme="exp+<slug>"/>` seems to block verification for these intent filters.
29 * This plugin makes sure there is no scheme in the autoVerify intent filters, that starts with `exp+`.
30
31 * Iterate over all `autoVerify=true` intent filters, and pull out schemes matching with `exp+<slug>`.
32 *
33 * @param {AndroidManifest} androidManifest
34 */
35function removeExpoSchemaFromVerifiedIntentFilters(config, androidManifest) {
36    // Generate a cross-platform scheme used to launch the dev client.
37    const defaultScheme = (0, getDefaultScheme_1.default)(config);
38    // see: https://github.com/expo/expo-cli/blob/f1624c75b52cc1c4f99354ec4021494e0eff74aa/packages/config-plugins/src/android/Scheme.ts#L164-L179
39    for (const application of androidManifest.manifest.application || []) {
40        for (const activity of application.activity || []) {
41            if (activityHasSingleTaskLaunchMode(activity)) {
42                for (const intentFilter of activity['intent-filter'] || []) {
43                    if (intentFilterHasAutoVerification(intentFilter) && (intentFilter === null || intentFilter === void 0 ? void 0 : intentFilter.data)) {
44                        intentFilter.data = intentFilterRemoveSchemeFromData(intentFilter, (scheme) => scheme === defaultScheme);
45                    }
46                }
47                break;
48            }
49        }
50    }
51    return androidManifest;
52}
53exports.removeExpoSchemaFromVerifiedIntentFilters = removeExpoSchemaFromVerifiedIntentFilters;
54/**
55 * Determine if the activity should contain the intent filters to clean.
56 *
57 * @see https://github.com/expo/expo-cli/blob/f1624c75b52cc1c4f99354ec4021494e0eff74aa/packages/config-plugins/src/android/Scheme.ts#L166
58 */
59function activityHasSingleTaskLaunchMode(activity) {
60    var _a;
61    return ((_a = activity === null || activity === void 0 ? void 0 : activity.$) === null || _a === void 0 ? void 0 : _a['android:launchMode']) === 'singleTask';
62}
63/**
64 * Determine if the intent filter has `autoVerify=true`.
65 */
66function intentFilterHasAutoVerification(intentFilter) {
67    var _a;
68    return ((_a = intentFilter === null || intentFilter === void 0 ? void 0 : intentFilter.$) === null || _a === void 0 ? void 0 : _a['android:autoVerify']) === 'true';
69}
70/**
71 * Remove schemes from the intent filter that matches the function.
72 */
73function intentFilterRemoveSchemeFromData(intentFilter, schemeMatcher) {
74    var _a;
75    return (_a = intentFilter === null || intentFilter === void 0 ? void 0 : intentFilter.data) === null || _a === void 0 ? void 0 : _a.filter((entry) => !schemeMatcher((entry === null || entry === void 0 ? void 0 : entry.$['android:scheme']) || ''));
76}
77