var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
class StorageService {
    constructor(app) {
        this.app = app;
        this.Api = this.app.Api
            .extend()
            .setEndpoint(this.app.options.authEndpoint || 'storage');
    }
    info(fileId) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.Api.get('/', { id: fileId });
        });
    }
    upload(fileResource, customFolder, rename) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!fileResource) {
                throw new Error('No file resource');
            }
            // request body
            const body = { fileResource };
            if (customFolder) {
                body.customFolder = customFolder;
            }
            if (rename) {
                body.rename = rename;
            }
            return yield this.Api.post('/', {}, body);
        });
    }
    load(file) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                if (!file) {
                    reject('No file.');
                }
                // read the file
                const reader = new FileReader();
                reader.onload = (e) => {
                    const base64Data = e.target.result;
                    const { name, size } = file;
                    resolve({ name, size, base64Data });
                };
                reader.readAsDataURL(file);
            });
        });
    }
}

function storage(app) {
    // get the default app
    if (!app) {
        if (!window['$$$SHEETBASE_APPS']) {
            throw new Error('No app for storage component.');
        }
        app = window['$$$SHEETBASE_APPS'].getApp();
    }
    // return the instance
    if (!!app.Storage) {
        return app.Storage;
    }
    else {
        return new StorageService(app);
    }
}
window['$$$SHEETBASE_COMPONENTS'].Storage = StorageService;

export { StorageService as Storage, storage };
//# sourceMappingURL=sheetbase-storage.js.map
