1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.appendScheme = appendScheme;
7exports.getScheme = getScheme;
8exports.getSchemesFromPlist = getSchemesFromPlist;
9exports.hasScheme = hasScheme;
10exports.removeScheme = removeScheme;
11exports.setScheme = setScheme;
12exports.withScheme = void 0;
13
14function _iosPlugins() {
15  const data = require("../plugins/ios-plugins");
16
17  _iosPlugins = function () {
18    return data;
19  };
20
21  return data;
22}
23
24const withScheme = (0, _iosPlugins().createInfoPlistPluginWithPropertyGuard)(setScheme, {
25  infoPlistProperty: 'CFBundleURLTypes',
26  expoConfigProperty: 'scheme'
27}, 'withScheme');
28exports.withScheme = withScheme;
29
30function getScheme(config) {
31  if (Array.isArray(config.scheme)) {
32    const validate = value => {
33      return typeof value === 'string';
34    };
35
36    return config.scheme.filter(validate);
37  } else if (typeof config.scheme === 'string') {
38    return [config.scheme];
39  }
40
41  return [];
42}
43
44function setScheme(config, infoPlist) {
45  var _config$ios, _config$ios2;
46
47  const scheme = [...getScheme(config), // @ts-ignore: TODO: ios.scheme is an unreleased -- harder to add to turtle v1.
48  ...getScheme((_config$ios = config.ios) !== null && _config$ios !== void 0 ? _config$ios : {})]; // Add the bundle identifier to the list of schemes for easier Google auth and parity with Turtle v1.
49
50  if ((_config$ios2 = config.ios) !== null && _config$ios2 !== void 0 && _config$ios2.bundleIdentifier) {
51    scheme.push(config.ios.bundleIdentifier);
52  }
53
54  if (scheme.length === 0) {
55    return infoPlist;
56  }
57
58  return { ...infoPlist,
59    CFBundleURLTypes: [{
60      CFBundleURLSchemes: scheme
61    }]
62  };
63}
64
65function appendScheme(scheme, infoPlist) {
66  var _infoPlist$CFBundleUR;
67
68  if (!scheme) {
69    return infoPlist;
70  }
71
72  const existingSchemes = (_infoPlist$CFBundleUR = infoPlist.CFBundleURLTypes) !== null && _infoPlist$CFBundleUR !== void 0 ? _infoPlist$CFBundleUR : [];
73
74  if (existingSchemes.some(({
75    CFBundleURLSchemes
76  }) => CFBundleURLSchemes.includes(scheme))) {
77    return infoPlist;
78  }
79
80  return { ...infoPlist,
81    CFBundleURLTypes: [...existingSchemes, {
82      CFBundleURLSchemes: [scheme]
83    }]
84  };
85}
86
87function removeScheme(scheme, infoPlist) {
88  if (!scheme) {
89    return infoPlist;
90  } // No need to remove if we don't have any
91
92
93  if (!infoPlist.CFBundleURLTypes) {
94    return infoPlist;
95  }
96
97  infoPlist.CFBundleURLTypes = infoPlist.CFBundleURLTypes.map(bundleUrlType => {
98    const index = bundleUrlType.CFBundleURLSchemes.indexOf(scheme);
99
100    if (index > -1) {
101      bundleUrlType.CFBundleURLSchemes.splice(index, 1);
102
103      if (bundleUrlType.CFBundleURLSchemes.length === 0) {
104        return undefined;
105      }
106    }
107
108    return bundleUrlType;
109  }).filter(Boolean);
110  return infoPlist;
111}
112
113function hasScheme(scheme, infoPlist) {
114  const existingSchemes = infoPlist.CFBundleURLTypes;
115  if (!Array.isArray(existingSchemes)) return false;
116  return existingSchemes.some(({
117    CFBundleURLSchemes: schemes
118  }) => Array.isArray(schemes) ? schemes.includes(scheme) : false);
119}
120
121function getSchemesFromPlist(infoPlist) {
122  if (Array.isArray(infoPlist.CFBundleURLTypes)) {
123    return infoPlist.CFBundleURLTypes.reduce((schemes, {
124      CFBundleURLSchemes
125    }) => {
126      if (Array.isArray(CFBundleURLSchemes)) {
127        return [...schemes, ...CFBundleURLSchemes];
128      }
129
130      return schemes;
131    }, []);
132  }
133
134  return [];
135}
136//# sourceMappingURL=Scheme.js.map