1import { EventEmitter, UnavailabilityError } from '@unimodules/core';
2import { Platform } from 'react-native';
3import { v4 as uuidv4 } from 'uuid';
4import ExponentFileSystem from './ExponentFileSystem';
5import { EncodingType, FileSystemSessionType, FileSystemUploadType, } from './FileSystem.types';
6if (!ExponentFileSystem) {
7    console.warn("No native ExponentFileSystem module found, are you sure the expo-file-system's module is linked properly?");
8}
9// Prevent webpack from pruning this.
10const _unused = new EventEmitter(ExponentFileSystem); // eslint-disable-line
11export { EncodingType, FileSystemSessionType, FileSystemUploadType, };
12function normalizeEndingSlash(p) {
13    if (p != null) {
14        return p.replace(/\/*$/, '') + '/';
15    }
16    return null;
17}
18export const documentDirectory = normalizeEndingSlash(ExponentFileSystem.documentDirectory);
19export const cacheDirectory = normalizeEndingSlash(ExponentFileSystem.cacheDirectory);
20export const { bundledAssets, bundleDirectory } = ExponentFileSystem;
21export async function getInfoAsync(fileUri, options = {}) {
22    if (!ExponentFileSystem.getInfoAsync) {
23        throw new UnavailabilityError('expo-file-system', 'getInfoAsync');
24    }
25    return await ExponentFileSystem.getInfoAsync(fileUri, options);
26}
27export async function readAsStringAsync(fileUri, options) {
28    if (!ExponentFileSystem.readAsStringAsync) {
29        throw new UnavailabilityError('expo-file-system', 'readAsStringAsync');
30    }
31    return await ExponentFileSystem.readAsStringAsync(fileUri, options || {});
32}
33export async function getContentUriAsync(fileUri) {
34    if (Platform.OS === 'android') {
35        if (!ExponentFileSystem.getContentUriAsync) {
36            throw new UnavailabilityError('expo-file-system', 'getContentUriAsync');
37        }
38        return await ExponentFileSystem.getContentUriAsync(fileUri);
39    }
40    else {
41        return new Promise(function (resolve, reject) {
42            resolve(fileUri);
43        });
44    }
45}
46export async function writeAsStringAsync(fileUri, contents, options = {}) {
47    if (!ExponentFileSystem.writeAsStringAsync) {
48        throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync');
49    }
50    return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options);
51}
52export async function deleteAsync(fileUri, options = {}) {
53    if (!ExponentFileSystem.deleteAsync) {
54        throw new UnavailabilityError('expo-file-system', 'deleteAsync');
55    }
56    return await ExponentFileSystem.deleteAsync(fileUri, options);
57}
58export async function deleteLegacyDocumentDirectoryAndroid() {
59    if (Platform.OS !== 'android' || documentDirectory == null) {
60        return;
61    }
62    const legacyDocumentDirectory = `${documentDirectory}ExperienceData/`;
63    return await deleteAsync(legacyDocumentDirectory, { idempotent: true });
64}
65export async function moveAsync(options) {
66    if (!ExponentFileSystem.moveAsync) {
67        throw new UnavailabilityError('expo-file-system', 'moveAsync');
68    }
69    return await ExponentFileSystem.moveAsync(options);
70}
71export async function copyAsync(options) {
72    if (!ExponentFileSystem.copyAsync) {
73        throw new UnavailabilityError('expo-file-system', 'copyAsync');
74    }
75    return await ExponentFileSystem.copyAsync(options);
76}
77export async function makeDirectoryAsync(fileUri, options = {}) {
78    if (!ExponentFileSystem.makeDirectoryAsync) {
79        throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync');
80    }
81    return await ExponentFileSystem.makeDirectoryAsync(fileUri, options);
82}
83export async function readDirectoryAsync(fileUri) {
84    if (!ExponentFileSystem.readDirectoryAsync) {
85        throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync');
86    }
87    return await ExponentFileSystem.readDirectoryAsync(fileUri, {});
88}
89export async function getFreeDiskStorageAsync() {
90    if (!ExponentFileSystem.getFreeDiskStorageAsync) {
91        throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync');
92    }
93    return await ExponentFileSystem.getFreeDiskStorageAsync();
94}
95export async function getTotalDiskCapacityAsync() {
96    if (!ExponentFileSystem.getTotalDiskCapacityAsync) {
97        throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync');
98    }
99    return await ExponentFileSystem.getTotalDiskCapacityAsync();
100}
101export async function downloadAsync(uri, fileUri, options = {}) {
102    if (!ExponentFileSystem.downloadAsync) {
103        throw new UnavailabilityError('expo-file-system', 'downloadAsync');
104    }
105    return await ExponentFileSystem.downloadAsync(uri, fileUri, {
106        sessionType: FileSystemSessionType.BACKGROUND,
107        ...options,
108    });
109}
110export async function uploadAsync(url, fileUri, options = {}) {
111    if (!ExponentFileSystem.uploadAsync) {
112        throw new UnavailabilityError('expo-file-system', 'uploadAsync');
113    }
114    return await ExponentFileSystem.uploadAsync(url, fileUri, {
115        sessionType: FileSystemSessionType.BACKGROUND,
116        uploadType: FileSystemUploadType.BINARY_CONTENT,
117        ...options,
118        httpMethod: (options.httpMethod || 'POST').toUpperCase(),
119    });
120}
121export function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
122    return new DownloadResumable(uri, fileUri, options, callback, resumeData);
123}
124export class DownloadResumable {
125    _uuid;
126    _url;
127    _fileUri;
128    _options;
129    _resumeData;
130    _callback;
131    _subscription;
132    _emitter;
133    constructor(url, fileUri, options = {}, callback, resumeData) {
134        this._uuid = uuidv4();
135        this._url = url;
136        this._fileUri = fileUri;
137        this._options = options;
138        this._resumeData = resumeData;
139        this._callback = callback;
140        this._subscription = null;
141        this._emitter = new EventEmitter(ExponentFileSystem);
142    }
143    async downloadAsync() {
144        if (!ExponentFileSystem.downloadResumableStartAsync) {
145            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
146        }
147        this._addSubscription();
148        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
149    }
150    async pauseAsync() {
151        if (!ExponentFileSystem.downloadResumablePauseAsync) {
152            throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
153        }
154        const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
155        this._removeSubscription();
156        if (pauseResult) {
157            this._resumeData = pauseResult.resumeData;
158            return this.savable();
159        }
160        else {
161            throw new Error('Unable to generate a savable pause state');
162        }
163    }
164    async resumeAsync() {
165        if (!ExponentFileSystem.downloadResumableStartAsync) {
166            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
167        }
168        this._addSubscription();
169        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
170    }
171    savable() {
172        return {
173            url: this._url,
174            fileUri: this._fileUri,
175            options: this._options,
176            resumeData: this._resumeData,
177        };
178    }
179    _addSubscription() {
180        if (this._subscription) {
181            return;
182        }
183        this._subscription = this._emitter.addListener('expo-file-system.downloadProgress', (event) => {
184            if (event.uuid === this._uuid) {
185                const callback = this._callback;
186                if (callback) {
187                    callback(event.data);
188                }
189            }
190        });
191    }
192    _removeSubscription() {
193        if (!this._subscription) {
194            return;
195        }
196        this._emitter.removeSubscription(this._subscription);
197        this._subscription = null;
198    }
199}
200const baseReadAsStringAsync = readAsStringAsync;
201const baseWriteAsStringAsync = writeAsStringAsync;
202const baseDeleteAsync = deleteAsync;
203const baseMoveAsync = moveAsync;
204const baseCopyAsync = copyAsync;
205/**
206 * Android only
207 */
208export var StorageAccessFramework;
209(function (StorageAccessFramework) {
210    function getUriForDirectoryInRoot(folderName) {
211        return `content://com.android.externalstorage.documents/tree/primary:${folderName}/document/primary:${folderName}`;
212    }
213    StorageAccessFramework.getUriForDirectoryInRoot = getUriForDirectoryInRoot;
214    async function requestDirectoryPermissionsAsync(initialFileUrl = null) {
215        if (!ExponentFileSystem.requestDirectoryPermissionsAsync) {
216            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.requestDirectoryPermissionsAsync');
217        }
218        return await ExponentFileSystem.requestDirectoryPermissionsAsync(initialFileUrl);
219    }
220    StorageAccessFramework.requestDirectoryPermissionsAsync = requestDirectoryPermissionsAsync;
221    async function readDirectoryAsync(dirUri) {
222        if (!ExponentFileSystem.readSAFDirectoryAsync) {
223            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.readDirectoryAsync');
224        }
225        return await ExponentFileSystem.readSAFDirectoryAsync(dirUri, {});
226    }
227    StorageAccessFramework.readDirectoryAsync = readDirectoryAsync;
228    async function makeDirectoryAsync(parentUri, dirName) {
229        if (!ExponentFileSystem.makeSAFDirectoryAsync) {
230            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.makeDirectoryAsync');
231        }
232        return await ExponentFileSystem.makeSAFDirectoryAsync(parentUri, dirName);
233    }
234    StorageAccessFramework.makeDirectoryAsync = makeDirectoryAsync;
235    async function createFileAsync(parentUri, fileName, mimeType) {
236        if (!ExponentFileSystem.createSAFFileAsync) {
237            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.createFileAsync');
238        }
239        return await ExponentFileSystem.createSAFFileAsync(parentUri, fileName, mimeType);
240    }
241    StorageAccessFramework.createFileAsync = createFileAsync;
242    StorageAccessFramework.writeAsStringAsync = baseWriteAsStringAsync;
243    StorageAccessFramework.readAsStringAsync = baseReadAsStringAsync;
244    StorageAccessFramework.deleteAsync = baseDeleteAsync;
245    StorageAccessFramework.moveAsync = baseMoveAsync;
246    StorageAccessFramework.copyAsync = baseCopyAsync;
247})(StorageAccessFramework || (StorageAccessFramework = {}));
248//# sourceMappingURL=FileSystem.js.map