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