1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.getLocales = getLocales;
7exports.getResolvedLocalesAsync = getResolvedLocalesAsync;
8exports.setLocalesAsync = setLocalesAsync;
9exports.withLocales = void 0;
10function _jsonFile() {
11  const data = _interopRequireDefault(require("@expo/json-file"));
12  _jsonFile = function () {
13    return data;
14  };
15  return data;
16}
17function _fs() {
18  const data = _interopRequireDefault(require("fs"));
19  _fs = function () {
20    return data;
21  };
22  return data;
23}
24function _path() {
25  const data = require("path");
26  _path = function () {
27    return data;
28  };
29  return data;
30}
31function _Xcodeproj() {
32  const data = require("./utils/Xcodeproj");
33  _Xcodeproj = function () {
34    return data;
35  };
36  return data;
37}
38function _iosPlugins() {
39  const data = require("../plugins/ios-plugins");
40  _iosPlugins = function () {
41    return data;
42  };
43  return data;
44}
45function _warnings() {
46  const data = require("../utils/warnings");
47  _warnings = function () {
48    return data;
49  };
50  return data;
51}
52function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
53const withLocales = config => {
54  return (0, _iosPlugins().withXcodeProject)(config, async config => {
55    config.modResults = await setLocalesAsync(config, {
56      projectRoot: config.modRequest.projectRoot,
57      project: config.modResults
58    });
59    return config;
60  });
61};
62exports.withLocales = withLocales;
63function getLocales(config) {
64  var _config$locales;
65  return (_config$locales = config.locales) !== null && _config$locales !== void 0 ? _config$locales : null;
66}
67async function setLocalesAsync(config, {
68  projectRoot,
69  project
70}) {
71  const locales = getLocales(config);
72  if (!locales) {
73    return project;
74  }
75  // possibly validate CFBundleAllowMixedLocalizations is enabled
76  const localesMap = await getResolvedLocalesAsync(projectRoot, locales);
77  const projectName = (0, _Xcodeproj().getProjectName)(projectRoot);
78  const supportingDirectory = (0, _path().join)(projectRoot, 'ios', projectName, 'Supporting');
79
80  // TODO: Should we delete all before running? Revisit after we land on a lock file.
81  const stringName = 'InfoPlist.strings';
82  for (const [lang, localizationObj] of Object.entries(localesMap)) {
83    const dir = (0, _path().join)(supportingDirectory, `${lang}.lproj`);
84    // await fs.ensureDir(dir);
85    await _fs().default.promises.mkdir(dir, {
86      recursive: true
87    });
88    const strings = (0, _path().join)(dir, stringName);
89    const buffer = [];
90    for (const [plistKey, localVersion] of Object.entries(localizationObj)) {
91      buffer.push(`${plistKey} = "${localVersion}";`);
92    }
93    // Write the file to the file system.
94    await _fs().default.promises.writeFile(strings, buffer.join('\n'));
95    const groupName = `${projectName}/Supporting/${lang}.lproj`;
96    // deep find the correct folder
97    const group = (0, _Xcodeproj().ensureGroupRecursively)(project, groupName);
98
99    // Ensure the file doesn't already exist
100    if (!(group !== null && group !== void 0 && group.children.some(({
101      comment
102    }) => comment === stringName))) {
103      // Only write the file if it doesn't already exist.
104      project = (0, _Xcodeproj().addResourceFileToGroup)({
105        filepath: (0, _path().relative)(supportingDirectory, strings),
106        groupName,
107        project,
108        isBuildFile: true,
109        verbose: true
110      });
111    }
112  }
113  return project;
114}
115async function getResolvedLocalesAsync(projectRoot, input) {
116  const locales = {};
117  for (const [lang, localeJsonPath] of Object.entries(input)) {
118    if (typeof localeJsonPath === 'string') {
119      try {
120        locales[lang] = await _jsonFile().default.readAsync((0, _path().join)(projectRoot, localeJsonPath));
121      } catch {
122        // Add a warning when a json file cannot be parsed.
123        (0, _warnings().addWarningIOS)(`locales.${lang}`, `Failed to parse JSON of locale file for language: ${lang}`, 'https://docs.expo.dev/distribution/app-stores/#localizing-your-ios-app');
124      }
125    } else {
126      // In the off chance that someone defined the locales json in the config, pass it directly to the object.
127      // We do this to make the types more elegant.
128      locales[lang] = localeJsonPath;
129    }
130  }
131  return locales;
132}
133//# sourceMappingURL=Locales.js.map