1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.setNotificationSounds = exports.withNotificationSounds = exports.withNotificationsIOS = void 0;
4const config_plugins_1 = require("@expo/config-plugins");
5const fs_1 = require("fs");
6const path_1 = require("path");
7const ERROR_MSG_PREFIX = 'An error occurred while configuring iOS notifications. ';
8exports.withNotificationsIOS = (config, { mode = 'development', sounds = [] }) => {
9    config = config_plugins_1.withEntitlementsPlist(config, config => {
10        config.modResults['aps-environment'] = mode;
11        return config;
12    });
13    config = exports.withNotificationSounds(config, { sounds });
14    return config;
15};
16exports.withNotificationSounds = (config, { sounds }) => {
17    return config_plugins_1.withXcodeProject(config, config => {
18        setNotificationSounds(config.modRequest.projectRoot, {
19            sounds,
20            project: config.modResults,
21            projectName: config.modRequest.projectName,
22        });
23        return config;
24    });
25};
26/**
27 * Save sound files to the Xcode project root and add them to the Xcode project.
28 */
29function setNotificationSounds(projectRoot, { sounds, project, projectName, }) {
30    if (!projectName) {
31        throw new Error(ERROR_MSG_PREFIX + `Unable to find iOS project name.`);
32    }
33    if (!Array.isArray(sounds)) {
34        throw new Error(ERROR_MSG_PREFIX +
35            `Must provide an array of sound files in your app config, found ${typeof sounds}.`);
36    }
37    const sourceRoot = config_plugins_1.IOSConfig.Paths.getSourceRoot(projectRoot);
38    for (const soundFileRelativePath of sounds) {
39        const fileName = path_1.basename(soundFileRelativePath);
40        const sourceFilepath = path_1.resolve(projectRoot, soundFileRelativePath);
41        const destinationFilepath = path_1.resolve(sourceRoot, fileName);
42        // Since it's possible that the filename is the same, but the
43        // file itself id different, let's copy it regardless
44        fs_1.copyFileSync(sourceFilepath, destinationFilepath);
45        if (!project.hasFile(`${projectName}/${fileName}`)) {
46            project = config_plugins_1.IOSConfig.XcodeUtils.addResourceFileToGroup({
47                filepath: `${projectName}/${fileName}`,
48                groupName: projectName,
49                isBuildFile: true,
50                project,
51            });
52        }
53    }
54    return project;
55}
56exports.setNotificationSounds = setNotificationSounds;
57