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 getFreeDiskStorageAsync() {
84    if (!ExponentFileSystem.getFreeDiskStorageAsync) {
85        throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync');
86    }
87    return await ExponentFileSystem.getFreeDiskStorageAsync();
88}
89export async function getTotalDiskCapacityAsync() {
90    if (!ExponentFileSystem.getTotalDiskCapacityAsync) {
91        throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync');
92    }
93    return await ExponentFileSystem.getTotalDiskCapacityAsync();
94}
95export async function downloadAsync(uri, fileUri, options = {}) {
96    if (!ExponentFileSystem.downloadAsync) {
97        throw new UnavailabilityError('expo-file-system', 'downloadAsync');
98    }
99    return await ExponentFileSystem.downloadAsync(uri, fileUri, options);
100}
101export function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
102    return new DownloadResumable(uri, fileUri, options, callback, resumeData);
103}
104export class DownloadResumable {
105    constructor(url, fileUri, options = {}, callback, resumeData) {
106        this._uuid = UUID.create(4).toString();
107        this._url = url;
108        this._fileUri = fileUri;
109        this._options = options;
110        this._resumeData = resumeData;
111        this._callback = callback;
112        this._subscription = null;
113        this._emitter = new EventEmitter(ExponentFileSystem);
114    }
115    async downloadAsync() {
116        if (!ExponentFileSystem.downloadResumableStartAsync) {
117            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
118        }
119        this._addSubscription();
120        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
121    }
122    async pauseAsync() {
123        if (!ExponentFileSystem.downloadResumablePauseAsync) {
124            throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
125        }
126        const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
127        this._removeSubscription();
128        if (pauseResult) {
129            this._resumeData = pauseResult.resumeData;
130            return this.savable();
131        }
132        else {
133            throw new Error('Unable to generate a savable pause state');
134        }
135    }
136    async resumeAsync() {
137        if (!ExponentFileSystem.downloadResumableStartAsync) {
138            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
139        }
140        this._addSubscription();
141        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
142    }
143    savable() {
144        return {
145            url: this._url,
146            fileUri: this._fileUri,
147            options: this._options,
148            resumeData: this._resumeData,
149        };
150    }
151    _addSubscription() {
152        if (this._subscription) {
153            return;
154        }
155        this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => {
156            if (event.uuid === this._uuid) {
157                const callback = this._callback;
158                if (callback) {
159                    callback(event.data);
160                }
161            }
162        });
163    }
164    _removeSubscription() {
165        if (!this._subscription) {
166            return;
167        }
168        this._emitter.removeSubscription(this._subscription);
169        this._subscription = null;
170    }
171}
172//# sourceMappingURL=FileSystem.js.map