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