1import UUID from 'uuid-js';
2import { Platform } from 'react-native';
3import { EventEmitter, UnavailabilityError } from '@unimodules/core';
4import ExponentFileSystem from './ExponentFileSystem';
5import { EncodingType, } 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, };
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, options);
106}
107export function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
108    return new DownloadResumable(uri, fileUri, options, callback, resumeData);
109}
110export class DownloadResumable {
111    constructor(url, fileUri, options = {}, callback, resumeData) {
112        this._uuid = UUID.create(4).toString();
113        this._url = url;
114        this._fileUri = fileUri;
115        this._options = options;
116        this._resumeData = resumeData;
117        this._callback = callback;
118        this._subscription = null;
119        this._emitter = new EventEmitter(ExponentFileSystem);
120    }
121    async downloadAsync() {
122        if (!ExponentFileSystem.downloadResumableStartAsync) {
123            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
124        }
125        this._addSubscription();
126        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
127    }
128    async pauseAsync() {
129        if (!ExponentFileSystem.downloadResumablePauseAsync) {
130            throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
131        }
132        const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
133        this._removeSubscription();
134        if (pauseResult) {
135            this._resumeData = pauseResult.resumeData;
136            return this.savable();
137        }
138        else {
139            throw new Error('Unable to generate a savable pause state');
140        }
141    }
142    async resumeAsync() {
143        if (!ExponentFileSystem.downloadResumableStartAsync) {
144            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
145        }
146        this._addSubscription();
147        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
148    }
149    savable() {
150        return {
151            url: this._url,
152            fileUri: this._fileUri,
153            options: this._options,
154            resumeData: this._resumeData,
155        };
156    }
157    _addSubscription() {
158        if (this._subscription) {
159            return;
160        }
161        this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => {
162            if (event.uuid === this._uuid) {
163                const callback = this._callback;
164                if (callback) {
165                    callback(event.data);
166                }
167            }
168        });
169    }
170    _removeSubscription() {
171        if (!this._subscription) {
172            return;
173        }
174        this._emitter.removeSubscription(this._subscription);
175        this._subscription = null;
176    }
177}
178//# sourceMappingURL=FileSystem.js.map