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