import * as textModule from '../text';
import { getNativeApp } from '../application/helpers-common';
import { getFileExtension } from '../utils/utils-shared';
import { SDK_VERSION } from '../utils/constants';
function getOrSetHelper(path) {
    return org.nativescript.widgets.FileHelper.fromString(getNativeApp().getApplicationContext(), path);
}
function isContentUri(path) {
    if (typeof path === 'string' && path.startsWith('content:')) {
        return true;
    }
    return false;
}
export class FileSystemAccess {
    constructor() {
        this._pathSeparator = '/';
        this.copy = this.copySync.bind(this);
        this.readBuffer = this.readBufferSync.bind(this);
        this.read = this.readSync.bind(this);
        this.appendBuffer = this.appendBufferSync.bind(this);
        this.append = this.appendSync.bind(this);
        this.writeBuffer = this.writeBufferSync.bind(this);
        this.write = this.writeSync.bind(this);
        this.readText = this.readTextSync.bind(this);
        this.appendText = this.appendTextSync.bind(this);
        this.writeText = this.writeTextSync.bind(this);
    }
    getLastModified(path) {
        const javaFile = new java.io.File(path);
        return new Date(javaFile.lastModified());
    }
    getFileSize(path) {
        const javaFile = new java.io.File(path);
        return javaFile.length();
    }
    getParent(path, onError) {
        try {
            const javaFile = new java.io.File(path);
            const parent = javaFile.getParentFile();
            return { path: parent.getAbsolutePath(), name: parent.getName() };
        }
        catch (exception) {
            // TODO: unified approach for error messages
            if (onError) {
                onError(exception);
            }
            return undefined;
        }
    }
    getFile(path, onError) {
        return this.ensureFile(new java.io.File(path), false, onError);
    }
    getFolder(path, onError) {
        const javaFile = new java.io.File(path);
        const dirInfo = this.ensureFile(javaFile, true, onError);
        if (!dirInfo) {
            return undefined;
        }
        return { path: dirInfo.path, name: dirInfo.name };
    }
    eachEntity(path, onEntity, onError) {
        if (!onEntity) {
            return;
        }
        this.enumEntities(path, onEntity, onError);
    }
    getEntities(path, onError) {
        const fileInfos = new Array();
        const onEntity = function (entity) {
            fileInfos.push(entity);
            return true;
        };
        let errorOccurred;
        const localError = function (error) {
            if (onError) {
                onError(error);
            }
            errorOccurred = true;
        };
        this.enumEntities(path, onEntity, localError);
        if (!errorOccurred) {
            return fileInfos;
        }
        return null;
    }
    fileExists(path) {
        const file = new java.io.File(path);
        return file.exists();
    }
    folderExists(path) {
        const file = new java.io.File(path);
        return file.exists() && file.isDirectory();
    }
    deleteFile(path, onError) {
        try {
            const javaFile = new java.io.File(path);
            if (!javaFile.isFile()) {
                if (onError) {
                    onError({
                        message: 'The specified parameter is not a File entity.',
                    });
                }
                return;
            }
            if (!javaFile.delete()) {
                if (onError) {
                    onError({ message: 'File deletion failed' });
                }
            }
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    deleteFolder(path, onError) {
        try {
            const javaFile = new java.io.File(path);
            if (!javaFile.getCanonicalFile().isDirectory()) {
                if (onError) {
                    onError({
                        message: 'The specified parameter is not a Folder entity.',
                    });
                }
                return;
            }
            // TODO: Asynchronous
            this.deleteFolderContent(javaFile);
            if (!javaFile.delete()) {
                if (onError) {
                    onError({ message: 'Folder deletion failed.' });
                }
            }
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    emptyFolder(path, onError) {
        try {
            const javaFile = new java.io.File(path);
            if (!javaFile.getCanonicalFile().isDirectory()) {
                if (onError) {
                    onError({
                        message: 'The specified parameter is not a Folder entity.',
                    });
                }
                return;
            }
            // TODO: Asynchronous
            this.deleteFolderContent(javaFile);
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    rename(path, newPath, onError) {
        const javaFile = new java.io.File(path);
        if (!javaFile.exists()) {
            if (onError) {
                onError(new Error('The file to rename does not exist'));
            }
            return;
        }
        const newFile = new java.io.File(newPath);
        if (newFile.exists()) {
            if (onError) {
                onError(new Error('A file with the same name already exists.'));
            }
            return;
        }
        if (!javaFile.renameTo(newFile)) {
            if (onError) {
                onError(new Error("Failed to rename file '" + path + "' to '" + newPath + "'"));
            }
        }
    }
    getDocumentsFolderPath() {
        const dir = getNativeApp().getApplicationContext().getFilesDir();
        return dir.getAbsolutePath();
    }
    getExternalDocumentsFolderPath() {
        const dirs = getNativeApp().getApplicationContext().getExternalFilesDirs(null);
        let dir;
        if (dirs && dirs.length > 1) {
            dir = dirs[dirs.length - 1];
        }
        if (!dir) {
            dir = getNativeApp().getApplicationContext().getExternalFilesDir(null);
        }
        return dir.getAbsolutePath();
    }
    getLogicalRootPath() {
        const dir = getNativeApp().getApplicationContext().getFilesDir();
        return dir.getCanonicalPath();
    }
    getTempFolderPath() {
        const dir = getNativeApp().getApplicationContext().getCacheDir();
        return dir.getAbsolutePath();
    }
    getCurrentAppPath() {
        return this.getLogicalRootPath() + '/app';
    }
    copySync(src, dest, onError) {
        try {
            return org.nativescript.widgets.Async.File.copySync(src, dest, getNativeApp().getApplicationContext());
        }
        catch (error) {
            if (onError) {
                onError(error);
            }
        }
        return false;
    }
    copyAsync(src, dest) {
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.copy(src, dest, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: (result) => {
                        resolve(result);
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), getNativeApp().getApplicationContext());
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    readBufferAsync(path) {
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.readBuffer(path, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: (result) => {
                        resolve(ArrayBuffer.from(result));
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    readBufferSync(path, onError) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileInputStream(javaFile);
            const channel = stream.getChannel();
            const buffer = new ArrayBuffer(javaFile.length());
            channel.read(buffer);
            return buffer;
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    readAsync(path) {
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.read(path, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: (result) => {
                        resolve(result);
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    readSync(path, onError) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileInputStream(javaFile);
            const bytes = Array.create('byte', javaFile.length());
            const dataInputStream = new java.io.DataInputStream(stream);
            dataInputStream.readFully(bytes);
            return bytes;
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    static getBuffer(buffer) {
        if (buffer instanceof ArrayBuffer) {
            return buffer.nativeObject || buffer;
        }
        else {
            return buffer?.buffer?.nativeObject || buffer;
        }
    }
    appendBufferAsync(path, buffer) {
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.appendBuffer(path, FileSystemAccess.getBuffer(buffer), new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: () => {
                        resolve();
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    appendBufferSync(path, buffer, onError) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileOutputStream(javaFile);
            const channel = stream.getChannel();
            channel.write(FileSystemAccess.getBuffer(buffer));
            stream.close();
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    appendAsync(path, bytes) {
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.append(path, bytes, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: () => {
                        resolve();
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    appendSync(path, bytes, onError) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileOutputStream(javaFile, true);
            stream.write(bytes, 0, bytes.length);
            stream.close();
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    writeBufferAsync(path, buffer) {
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.writeBuffer(path, FileSystemAccess.getBuffer(buffer), new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: () => {
                        resolve();
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    writeBufferSync(path, buffer, onError) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileOutputStream(javaFile);
            const channel = stream.getChannel();
            channel.write(FileSystemAccess.getBuffer(buffer));
            stream.close();
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    writeAsync(path, bytes) {
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.write(path, bytes, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: () => {
                        resolve();
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    writeSync(path, bytes, onError) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileOutputStream(javaFile);
            stream.write(bytes, 0, bytes.length);
            stream.close();
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    readTextAsync(path, encoding) {
        let actualEncoding = encoding;
        if (!actualEncoding) {
            actualEncoding = textModule.encoding.UTF_8;
        }
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.readText(path, actualEncoding, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: (result) => {
                        if (actualEncoding === textModule.encoding.UTF_8) {
                            // Remove UTF8 BOM if present. http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html
                            result = FileSystemAccess._removeUtf8Bom(result);
                        }
                        resolve(result);
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    readTextSync(path, onError, encoding) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileInputStream(javaFile);
            let actualEncoding = encoding;
            if (!actualEncoding) {
                actualEncoding = textModule.encoding.UTF_8;
            }
            const reader = new java.io.InputStreamReader(stream, actualEncoding);
            const bufferedReader = new java.io.BufferedReader(reader);
            // TODO: We will need to read the entire file to a CharBuffer instead of reading it line by line
            // TODO: bufferedReader.read(CharBuffer) does not currently work
            let line = undefined;
            let result = '';
            while (line !== null) {
                line = bufferedReader.readLine();
                if (line === null) {
                    break;
                }
                if (result.length > 0) {
                    // add the new line manually to the result
                    // TODO: Try with CharBuffer at a later stage, when the Bridge allows it
                    result += '\n';
                }
                result += line;
            }
            if (actualEncoding === textModule.encoding.UTF_8) {
                // Remove UTF8 BOM if present. http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html
                result = FileSystemAccess._removeUtf8Bom(result);
            }
            bufferedReader.close();
            return result;
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    static _removeUtf8Bom(s) {
        if (s.charCodeAt(0) === 0xfeff) {
            s = s.slice(1);
            //console.log("Removed UTF8 BOM.");
        }
        return s;
    }
    appendTextAsync(path, content, encoding) {
        let actualEncoding = encoding;
        if (!actualEncoding) {
            actualEncoding = textModule.encoding.UTF_8;
        }
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.appendText(path, content, actualEncoding, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: () => {
                        resolve();
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    appendTextSync(path, content, onError, encoding) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileOutputStream(javaFile, true);
            let actualEncoding = encoding;
            if (!actualEncoding) {
                actualEncoding = textModule.encoding.UTF_8;
            }
            const writer = new java.io.OutputStreamWriter(stream, actualEncoding);
            writer.write(content);
            writer.close();
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    writeTextAsync(path, content, encoding) {
        let actualEncoding = encoding;
        if (!actualEncoding) {
            actualEncoding = textModule.encoding.UTF_8;
        }
        return new Promise((resolve, reject) => {
            try {
                org.nativescript.widgets.Async.File.writeText(path, content, actualEncoding, new org.nativescript.widgets.Async.CompleteCallback({
                    onComplete: () => {
                        resolve();
                    },
                    onError: (err) => {
                        reject(err);
                    },
                }), null);
            }
            catch (ex) {
                reject(ex);
            }
        });
    }
    writeTextSync(path, content, onError, encoding) {
        try {
            const javaFile = new java.io.File(path);
            const stream = new java.io.FileOutputStream(javaFile);
            let actualEncoding = encoding;
            if (!actualEncoding) {
                actualEncoding = textModule.encoding.UTF_8;
            }
            const writer = new java.io.OutputStreamWriter(stream, actualEncoding);
            writer.write(content);
            writer.close();
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    deleteFolderContent(file) {
        const filesList = file.listFiles();
        if (filesList.length === 0) {
            return true; // Nothing to delete, so success!
        }
        let childFile;
        let success = false;
        for (let i = 0; i < filesList.length; i++) {
            childFile = filesList[i];
            if (childFile.getCanonicalFile().isDirectory()) {
                success = this.deleteFolderContent(childFile);
                if (!success) {
                    break;
                }
            }
            success = childFile.delete();
        }
        return success;
    }
    ensureFile(javaFile, isFolder, onError) {
        try {
            if (!javaFile.exists()) {
                let created;
                if (isFolder) {
                    created = javaFile.mkdirs();
                }
                else {
                    javaFile.getParentFile().mkdirs();
                    created = javaFile.createNewFile();
                }
                if (!created) {
                    // TODO: unified approach for error messages
                    if (onError) {
                        onError('Failed to create new java File for path ' + javaFile.getAbsolutePath());
                    }
                    return undefined;
                }
                else {
                    javaFile.setReadable(true);
                    javaFile.setWritable(true);
                }
            }
            const path = javaFile.getAbsolutePath();
            return {
                path: path,
                name: javaFile.getName(),
                extension: this.getFileExtension(path),
            };
        }
        catch (exception) {
            // TODO: unified approach for error messages
            if (onError) {
                onError(exception);
            }
            return undefined;
        }
    }
    // TODO: This method is the same as in the iOS implementation.
    // Make it in a separate file / module so it can be reused from both implementations.
    getFileExtension(path) {
        return getFileExtension(path);
    }
    enumEntities(path, callback, onError) {
        try {
            let javaFile = new java.io.File(path);
            if (!javaFile.getCanonicalFile().isDirectory()) {
                if (onError) {
                    onError('There is no folder existing at path ' + path);
                }
                return;
            }
            const filesList = javaFile.listFiles();
            const length = filesList.length;
            let info;
            let retVal;
            for (let i = 0; i < length; i++) {
                javaFile = filesList[i];
                info = {
                    path: javaFile.getAbsolutePath(),
                    name: javaFile.getName(),
                };
                if (javaFile.isFile()) {
                    info.extension = this.getFileExtension(info.path);
                }
                retVal = callback(info);
                if (retVal === false) {
                    break;
                }
            }
        }
        catch (exception) {
            if (onError) {
                onError(exception);
            }
        }
    }
    getPathSeparator() {
        return this._pathSeparator;
    }
    normalizePath(path) {
        if (SDK_VERSION >= 26) {
            // the [''] is a trick to not have to create a android.net.URI
            // and use the method with string signature
            return java.nio.file.Paths.get(path, ['']).normalize().toString();
        }
        else {
            // for now it wont work on pre 26 as File does not normalize
            const file = new java.io.File(path);
            return file.getAbsolutePath();
        }
    }
    joinPath(left, right) {
        const file1 = new java.io.File(left);
        const file2 = new java.io.File(file1, right);
        return file2.getPath();
    }
    joinPaths(paths) {
        if (!paths || paths.length === 0) {
            return '';
        }
        if (paths.length === 1) {
            return paths[0];
        }
        let result = paths[0];
        for (let i = 1; i < paths.length; i++) {
            result = this.joinPath(result, paths[i]);
        }
        return result;
    }
}
export class FileSystemAccess29 extends FileSystemAccess {
    constructor() {
        super(...arguments);
        this.__skip = true;
        this.appendBuffer = this.appendBufferSync.bind(this);
        this.append = this.appendSync.bind(this);
        this.appendText = this.appendTextSync.bind(this);
        this.readText = this.readTextSync.bind(this);
        this.readBuffer = this.readBufferSync.bind(this);
        this.read = this.readSync.bind(this);
        this.writeText = this.writeTextSync.bind(this);
        this.writeBuffer = this.writeBufferSync.bind(this);
        this.write = this.writeSync.bind(this);
    }
    getLastModified(path) {
        if (isContentUri(path)) {
            return new Date(getOrSetHelper(path).getLastModified() * 1000);
        }
        return super.getLastModified(path);
    }
    getFileSize(path) {
        if (isContentUri(path)) {
            return getOrSetHelper(path).getSize();
        }
        return super.getFileSize(path);
    }
    getParent(path, onError) {
        if (isContentUri(path)) {
            return null;
        }
        return super.getParent(path, onError);
    }
    getFile(path, onError) {
        if (isContentUri(path)) {
            try {
                const file = getOrSetHelper(path);
                return {
                    path,
                    name: file.getName(),
                    extension: file.getExtension(),
                };
            }
            catch (e) {
                if (typeof onError === 'function') {
                    onError(e);
                }
                return;
            }
        }
        return super.getFile(path, onError);
    }
    getFolder(path, onError) {
        if (isContentUri(path)) {
            return null;
        }
        return super.getFolder(path, onError);
    }
    getEntities(path, onError) {
        if (isContentUri(path)) {
            return null;
        }
        return super.getEntities(path, onError);
    }
    eachEntity(path, onEntity, onError) {
        if (isContentUri(path)) {
            return null;
        }
        super.eachEntity(path, onEntity);
    }
    fileExists(path) {
        if (isContentUri(path)) {
            return org.nativescript.widgets.FileHelper.exists(getNativeApp().getApplicationContext(), path);
        }
        return super.fileExists(path);
    }
    folderExists(path) {
        if (isContentUri(path)) {
            return null;
        }
        return super.folderExists(path);
    }
    deleteFile(path, onError) {
        if (isContentUri(path)) {
            try {
                getOrSetHelper(path).delete(getNativeApp().getApplicationContext());
            }
            catch (e) {
                onError?.(e);
            }
        }
        else {
            super.deleteFile(path, onError);
        }
    }
    deleteFolder(path, onError) {
        if (!isContentUri(path)) {
            super.deleteFolder(path, onError);
        }
    }
    emptyFolder(path, onError) {
        if (!isContentUri(path)) {
            super.emptyFolder(path, onError);
        }
    }
    rename(path, newPath, onError) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            getOrSetHelper(path).renameSync(getNativeApp().getApplicationContext(), newPath, callback);
        }
        else {
            super.rename(path, newPath, onError);
        }
    }
    renameAsync(path, newPath) {
        return new Promise((resolve, reject) => {
            getOrSetHelper(path).renameSync(getNativeApp().getApplicationContext(), newPath, new org.nativescript.widgets.FileHelper.Callback({
                onSuccess(result) {
                    resolve();
                },
                onError(error) {
                    reject(error);
                },
            }));
        });
    }
    getDocumentsFolderPath() {
        return super.getDocumentsFolderPath();
    }
    getTempFolderPath() {
        return super.getTempFolderPath();
    }
    getLogicalRootPath() {
        return super.getLogicalRootPath();
    }
    getCurrentAppPath() {
        return super.getCurrentAppPath();
    }
    appendBufferAsync(path, content) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).appendBuffer(getNativeApp().getApplicationContext(), FileSystemAccess.getBuffer(content), new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve();
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.appendAsync(path, content);
    }
    appendBufferSync(path, content, onError) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            getOrSetHelper(path).appendSync(getNativeApp().getApplicationContext(), FileSystemAccess.getBuffer(content), callback);
        }
        else {
            super.appendSync(path, content, onError);
        }
    }
    appendAsync(path, content) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).append(getNativeApp().getApplicationContext(), content, new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve();
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.appendAsync(path, content);
    }
    appendSync(path, content, onError) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            getOrSetHelper(path).appendSync(getNativeApp().getApplicationContext(), content, callback);
        }
        else {
            super.appendSync(path, content, onError);
        }
    }
    appendTextAsync(path, content, encoding) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).appendText(getNativeApp().getApplicationContext(), content, encoding ?? null, new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve();
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.appendTextAsync(path, content, encoding);
    }
    appendTextSync(path, content, onError, encoding) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            getOrSetHelper(path).appendTextSync(getNativeApp().getApplicationContext(), content, encoding ?? null, callback);
        }
        else {
            super.appendTextSync(path, content, onError);
        }
    }
    readTextAsync(path, encoding) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).readText(getNativeApp().getApplicationContext(), encoding ?? null, new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve(result);
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.readTextAsync(path, encoding);
    }
    readTextSync(path, onError, encoding) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            return getOrSetHelper(path).readTextSync(getNativeApp().getApplicationContext(), encoding ?? null, callback);
        }
        else {
            return super.readTextSync(path, onError, encoding);
        }
    }
    readBufferAsync(path) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).readBuffer(getNativeApp().getApplicationContext(), new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve(result);
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.readBufferAsync(path);
    }
    readBufferSync(path, onError) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            const ret = getOrSetHelper(path).readBufferSync(getNativeApp().getApplicationContext(), callback);
            if (ret) {
                return null;
            }
            return ArrayBuffer.from(ret);
        }
        return super.readBufferSync(path, onError);
    }
    readAsync(path) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).read(getNativeApp().getApplicationContext(), new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve(result);
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.readAsync(path);
    }
    readSync(path, onError) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            return getOrSetHelper(path).readSync(getNativeApp().getApplicationContext(), callback);
        }
        return super.readSync(path, onError);
    }
    writeTextAsync(path, content, encoding) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).writeText(getNativeApp().getApplicationContext(), content, encoding ?? null, new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve();
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.writeTextAsync(path, content, encoding);
    }
    writeTextSync(path, content, onError, encoding) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            getOrSetHelper(path).writeTextSync(getNativeApp().getApplicationContext(), content, encoding ?? null, callback);
        }
        else {
            super.writeTextSync(path, content, onError);
        }
    }
    writeBufferAsync(path, content) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).writeBuffer(getNativeApp().getApplicationContext(), FileSystemAccess.getBuffer(content), new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve();
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.writeAsync(path, content);
    }
    writeBufferSync(path, content, onError) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            getOrSetHelper(path).writeSync(getNativeApp().getApplicationContext(), FileSystemAccess.getBuffer(content), callback);
        }
        else {
            super.writeSync(path, content, onError);
        }
    }
    writeAsync(path, content) {
        if (isContentUri(path)) {
            return new Promise((resolve, reject) => {
                getOrSetHelper(path).write(getNativeApp().getApplicationContext(), content, new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) {
                        resolve();
                    },
                    onError(error) {
                        reject(error);
                    },
                }));
            });
        }
        return super.writeAsync(path, content);
    }
    writeSync(path, content, onError) {
        if (isContentUri(path)) {
            let callback = null;
            if (typeof onError === 'function') {
                callback = new org.nativescript.widgets.FileHelper.Callback({
                    onSuccess(result) { },
                    onError(error) {
                        onError(error);
                    },
                });
            }
            getOrSetHelper(path).writeSync(getNativeApp().getApplicationContext(), content, callback);
        }
        else {
            super.writeSync(path, content, onError);
        }
    }
    getFileExtension(path) {
        if (isContentUri(path)) {
            return getOrSetHelper(path).getExtension();
        }
        return super.getFileExtension(path);
    }
    getPathSeparator() {
        return super.getPathSeparator();
    }
    normalizePath(path) {
        return super.normalizePath(path);
    }
    joinPath(left, right) {
        return super.joinPath(left, right);
    }
    joinPaths(paths) {
        return super.joinPaths(paths);
    }
}
//# sourceMappingURL=file-system-access.android.js.map