1import { UnavailabilityError } from 'expo-errors';
2import { EventEmitter } from 'expo-core';
3import UUID from 'uuid-js';
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}
9export { EncodingType, };
10function normalizeEndingSlash(p) {
11    if (p != null) {
12        return p.replace(/\/*$/, '') + '/';
13    }
14    return null;
15}
16export const documentDirectory = normalizeEndingSlash(ExponentFileSystem.documentDirectory);
17export const cacheDirectory = normalizeEndingSlash(ExponentFileSystem.cacheDirectory);
18export const { bundledAssets, bundleDirectory } = ExponentFileSystem;
19export async function getInfoAsync(fileUri, options = {}) {
20    if (!ExponentFileSystem.getInfoAsync) {
21        throw new UnavailabilityError('expo-file-system', 'getInfoAsync');
22    }
23    return await ExponentFileSystem.getInfoAsync(fileUri, options);
24}
25export async function readAsStringAsync(fileUri, options) {
26    if (!ExponentFileSystem.readAsStringAsync) {
27        throw new UnavailabilityError('expo-file-system', 'readAsStringAsync');
28    }
29    return await ExponentFileSystem.readAsStringAsync(fileUri, options || {});
30}
31export async function writeAsStringAsync(fileUri, contents, options = {}) {
32    if (!ExponentFileSystem.writeAsStringAsync) {
33        throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync');
34    }
35    return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options);
36}
37export async function deleteAsync(fileUri, options = {}) {
38    if (!ExponentFileSystem.deleteAsync) {
39        throw new UnavailabilityError('expo-file-system', 'deleteAsync');
40    }
41    return await ExponentFileSystem.deleteAsync(fileUri, options);
42}
43export async function moveAsync(options) {
44    if (!ExponentFileSystem.moveAsync) {
45        throw new UnavailabilityError('expo-file-system', 'moveAsync');
46    }
47    return await ExponentFileSystem.moveAsync(options);
48}
49export async function copyAsync(options) {
50    if (!ExponentFileSystem.copyAsync) {
51        throw new UnavailabilityError('expo-file-system', 'copyAsync');
52    }
53    return await ExponentFileSystem.copyAsync(options);
54}
55export async function makeDirectoryAsync(fileUri, options = {}) {
56    if (!ExponentFileSystem.makeDirectoryAsync) {
57        throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync');
58    }
59    return await ExponentFileSystem.makeDirectoryAsync(fileUri, options);
60}
61export async function readDirectoryAsync(fileUri) {
62    if (!ExponentFileSystem.readDirectoryAsync) {
63        throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync');
64    }
65    return await ExponentFileSystem.readDirectoryAsync(fileUri, {});
66}
67export async function downloadAsync(uri, fileUri, options = {}) {
68    if (!ExponentFileSystem.downloadAsync) {
69        throw new UnavailabilityError('expo-file-system', 'downloadAsync');
70    }
71    return await ExponentFileSystem.downloadAsync(uri, fileUri, options);
72}
73export function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
74    return new DownloadResumable(uri, fileUri, options, callback, resumeData);
75}
76export class DownloadResumable {
77    constructor(url, fileUri, options = {}, callback, resumeData) {
78        this._uuid = UUID.create(4).toString();
79        this._url = url;
80        this._fileUri = fileUri;
81        this._options = options;
82        this._resumeData = resumeData;
83        this._callback = callback;
84        this._subscription = null;
85        this._emitter = new EventEmitter(ExponentFileSystem);
86    }
87    async downloadAsync() {
88        if (!ExponentFileSystem.downloadResumableStartAsync) {
89            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
90        }
91        this._addSubscription();
92        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
93    }
94    async pauseAsync() {
95        if (!ExponentFileSystem.downloadResumablePauseAsync) {
96            throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
97        }
98        const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
99        this._removeSubscription();
100        if (pauseResult) {
101            this._resumeData = pauseResult.resumeData;
102            return this.savable();
103        }
104        else {
105            throw new Error('Unable to generate a savable pause state');
106        }
107    }
108    async resumeAsync() {
109        if (!ExponentFileSystem.downloadResumableStartAsync) {
110            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
111        }
112        this._addSubscription();
113        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
114    }
115    savable() {
116        return {
117            url: this._url,
118            fileUri: this._fileUri,
119            options: this._options,
120            resumeData: this._resumeData,
121        };
122    }
123    _addSubscription() {
124        if (this._subscription) {
125            return;
126        }
127        this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => {
128            if (event.uuid === this._uuid) {
129                const callback = this._callback;
130                if (callback) {
131                    callback(event.data);
132                }
133            }
134        });
135    }
136    _removeSubscription() {
137        if (!this._subscription) {
138            return;
139        }
140        this._emitter.removeSubscription(this._subscription);
141        this._subscription = null;
142    }
143}
144//# sourceMappingURL=FileSystem.js.map