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