import { Utils } from '@nativescript/core';
class FileUploadResponse {
    static fromNative(value) {
        if (!value)
            return null;
        const ret = new FileUploadResponse();
        ret.native_ = value;
        return ret;
    }
    get native() {
        return this.native_;
    }
    get fullPath() {
        return this.native.fullPath;
    }
    get id() {
        return this.native.id;
    }
    get path() {
        return this.native.path;
    }
}
class SignedURLUploadResponse {
    static fromNative(value) {
        if (!value)
            return null;
        const ret = new SignedURLUploadResponse();
        ret.native_ = value;
        return ret;
    }
    get native() {
        return this.native_;
    }
    get fullPath() {
        return this.native.fullPath;
    }
    get path() {
        return this.native.path;
    }
}
class SignedUploadURL {
    static fromNative(value) {
        if (!value)
            return null;
        const ret = new SignedUploadURL();
        ret.native_ = value;
        return ret;
    }
    get native() {
        return this.native_;
    }
    get path() {
        return this.native.path;
    }
    get signedURL() {
        return this.native.signedURL;
    }
    get token() {
        return this.native.token;
    }
}
class FileObject {
    static fromNative(value) {
        if (!value)
            return null;
        const ret = new FileObject();
        ret.native_ = value;
        return ret;
    }
    get native() {
        return this.native_;
    }
    get bucketId() {
        return this.native.bucketId;
    }
    get buckets() {
        return StorageBucket.fromNative(this.native.buckets);
    }
    get createdAt() {
        return this.native.createdAt;
    }
    get id() {
        return this.native.id;
    }
    get lastAccessedAt() {
        return this.native.lastAccessedAt;
    }
    get metadata() {
        return Utils.dataDeserialize(this.native.metadata);
    }
    get name() {
        return this.native.name;
    }
    get owner() {
        return this.native.owner;
    }
    get updatedAt() {
        return this.native.updatedAt;
    }
}
function parseTransformOptions(options) {
    if (!options) {
        return null;
    }
    const ret = NSCSupabseStorageTransformOptions.new();
    if ('format' in options) {
        ret.format = options.format;
    }
    if ('height' in options) {
        ret.height = options.height;
    }
    if ('quality' in options) {
        ret.quality = options.quality;
    }
    if ('resize' in options) {
        ret.resize = options.resize;
    }
    if ('width' in options) {
        ret.width = options.width;
    }
    return ret;
}
function parseDownload(download) {
    switch (typeof download) {
        case 'string':
            return download;
        case 'boolean':
            return download ? '' : null;
        default:
            return null;
    }
}
function parseSearch(options) {
    if (!options) {
        return null;
    }
    const ret = NSCSupabseStorageSearchOptions.new();
    if ('limit' in options) {
        ret.limit = options.limit;
    }
    if ('offset' in options) {
        ret.offset = options.offset;
    }
    if ('search' in options) {
        ret.search = options.search;
    }
    if ('sortBy' in options) {
        if ('column' in options.sortBy || 'order' in options.sortBy) {
            ret.sortBy = NSCSupabseStorageSortBy.new();
            ret.sortBy.column = options.sortBy.column;
            ret.sortBy.order = options.sortBy.order;
        }
    }
    return ret;
}
function parseFileOptions(options) {
    if (!options) {
        return null;
    }
    const ret = NSCSupabseStorageFileOptions.new();
    if ('cacheControl' in options) {
        ret.cacheControl = options.cacheControl;
    }
    if ('contentType' in options) {
        ret.contentType = options.contentType;
    }
    if ('duplex' in options) {
        ret.duplex = options.duplex;
    }
    if ('headers' in options) {
        ret.headers = options.headers;
    }
    if ('metadata' in options) {
        ret.metadata = options.metadata;
    }
    if ('upsert' in options) {
        ret.upsert = options.upsert;
    }
    return ret;
}
export class StorageBucket {
    static fromNative(value) {
        if (!value)
            return null;
        const ret = new StorageBucket();
        ret.native_ = value;
        return ret;
    }
    get native() {
        return this.native_;
    }
    get allowedMimeTypes() {
        return Utils.dataDeserialize(this.native.allowedMimeTypes);
    }
    get createdAt() {
        return this.native.createdAt;
    }
    get id() {
        return this.native.id;
    }
    get isPublic() {
        return this.native.isPublic;
    }
    get name() {
        return this.native.name;
    }
    get owner() {
        return this.native.owner;
    }
    get updatedAt() {
        return this.native.updatedAt;
    }
}
export class StorageFileApi {
    static fromNative(value) {
        if (!value)
            return null;
        const ret = new StorageFileApi();
        ret.native_ = value;
        return ret;
    }
    get native() {
        return this.native_;
    }
    copy(fromPath, toPath) {
        return new Promise((resolve, reject) => {
            this.native.copy(fromPath, toPath, (path, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(path);
                }
            });
        });
    }
    createSignedUploadUrl(path) {
        return new Promise((resolve, reject) => {
            this.native.createSignedUploadUrl(path, (url, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(SignedUploadURL.fromNative(url));
                }
            });
        });
    }
    createSignedUrl(path, expiresIn, options) {
        return new Promise((resolve, reject) => {
            this.native.createSignedUrl(path, expiresIn, parseDownload(options?.download), parseTransformOptions(options?.transform), (url, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(url);
                }
            });
        });
    }
    createSignedUrls(paths, expiresIn, options) {
        return new Promise((resolve, reject) => {
            this.native.createSignedUrls(paths, expiresIn, parseDownload(options?.download), (urls, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(Utils.dataDeserialize(urls));
                }
            });
        });
    }
    download(path, options) {
        return new Promise((resolve, reject) => {
            this.native.download(path, parseTransformOptions(options), (data, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(interop.bufferFromData(data));
                }
            });
        });
    }
    getPublicUrl(path, options) {
        try {
            return this.native.getPublicUrlError(path, parseDownload(options?.download ?? null), parseTransformOptions(options?.transform ?? null));
        }
        catch (error) {
            throw new Error(error.message);
        }
    }
    list(path, options) {
        return new Promise((resolve, reject) => {
            this.native.list(path ?? null, parseSearch(options), (files, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    const count = files.count;
                    const ret = new Array(count);
                    for (let i = 0; i < count; i++) {
                        ret[i] = FileObject.fromNative(files.objectAtIndex(i));
                    }
                    resolve(ret);
                }
            });
        });
    }
    move(fromPath, toPath) {
        return new Promise((resolve, reject) => {
            this.native.move(fromPath, toPath, (error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    remove(paths) {
        return new Promise((resolve, reject) => {
            this.native.remove(paths, (files, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    const count = files.count;
                    const ret = new Array(count);
                    for (let i = 0; i < count; i++) {
                        ret[i] = FileObject.fromNative(files.objectAtIndex(i));
                    }
                    resolve(ret);
                }
            });
        });
    }
    update(path, data, options) {
        return new Promise((resolve, reject) => {
            if (typeof data === 'string') {
                this.native.updateFile(path, NSURL.URLWithString('file://' + data), parseFileOptions(options), (response, error) => {
                    if (error) {
                        reject(error);
                    }
                    else {
                        resolve(FileUploadResponse.fromNative(response));
                    }
                });
            }
            else {
                this.native.updateData(path, NSData.dataWithData(data), parseFileOptions(options), (response, error) => {
                    if (error) {
                        reject(error);
                    }
                    else {
                        resolve(FileUploadResponse.fromNative(response));
                    }
                });
            }
        });
    }
    upload(path, data, options) {
        return new Promise((resolve, reject) => {
            if (typeof data === 'string') {
                this.native.uploadFile(path, NSURL.URLWithString('file://' + data), parseFileOptions(options), (response, error) => {
                    if (error) {
                        reject(error);
                    }
                    else {
                        resolve(FileUploadResponse.fromNative(response));
                    }
                });
            }
            else {
                this.native.updateData(path, NSData.dataWithData(data), parseFileOptions(options), (response, error) => {
                    if (error) {
                        reject(error);
                    }
                    else {
                        resolve(FileUploadResponse.fromNative(response));
                    }
                });
            }
        });
    }
    uploadToSignedUrl(path, token, data, options) {
        return new Promise((resolve, reject) => {
            this.native.uploadToSignedUrlData(path, token, NSData.dataWithData(data), parseFileOptions(options), (response, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(SignedURLUploadResponse.fromNative(response));
                }
            });
        });
    }
}
export class SupabaseStorageClient {
    constructor(client) {
        this.client = client;
    }
    get native() {
        return this.client.native.storage;
    }
    createBucket(id, options) {
        return new Promise((resolve, reject) => {
            let opt = null;
            if (options) {
                opt = NSCSupabseStorageBucketOptions.new();
                if ('allowedMimeTypes' in opt) {
                    opt.allowedMimeTypes = options.allowedMimeTypes;
                }
                if ('isPublic' in options) {
                    opt.public_ = options.isPublic;
                }
                if ('fileSizeLimit' in options) {
                    opt.fileSizeLimit = `${options.fileSizeLimit}`;
                }
            }
            this.native.createBucket(id, opt, (error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    getBucket(id) {
        return new Promise((reject, resolve) => {
            this.native.getBucket(id, (bucket, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(StorageBucket.fromNative(bucket));
                }
            });
        });
    }
    listBuckets(id) {
        return new Promise((resolve, reject) => {
            this.native.listBuckets(id, (buckets, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    const count = buckets.count;
                    const ret = new Array(count);
                    for (let i = 0; i < count; i++) {
                        ret[i] = StorageBucket.fromNative(buckets.objectAtIndex(i));
                    }
                    resolve(ret);
                }
            });
        });
    }
    updateBucket(id, options) {
        return new Promise((resolve, reject) => {
            let opt = null;
            if (options) {
                opt = NSCSupabseStorageBucketOptions.new();
                if ('allowedMimeTypes' in opt) {
                    opt.allowedMimeTypes = options.allowedMimeTypes;
                }
                if ('isPublic' in options) {
                    opt.public_ = options.isPublic;
                }
                if ('fileSizeLimit' in options) {
                    opt.fileSizeLimit = `${options.fileSizeLimit}`;
                }
            }
            this.native.updateBucket(id, opt, (error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    deleteBucket(id) {
        return new Promise((resolve, reject) => {
            this.native.deleteBucket(id, (error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    emptyBucket(id) {
        return new Promise((resolve, reject) => {
            this.native.emptyBucket(id, (error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    from(id) {
        return StorageFileApi.fromNative(this.native.from(id));
    }
}
//# sourceMappingURL=index.ios.js.map