var __awaiter = (this && this.__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());
    });
};
import { md5 } from '../../md5/md5';
export class DatabaseServerService {
    constructor(app) {
        this.app = app;
        this.Api = this.app.Api
            .extend()
            .setEndpoint(this.app.options.authEndpoint || 'database');
    }
    all(sheet, cacheTime = 0) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.app.Cache.getRefresh('database_' + sheet, cacheTime, () => __awaiter(this, void 0, void 0, function* () { return yield this.Api.get('/', { sheet }); }));
        });
    }
    query(sheet, query, cacheTime = 0) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.app.Cache.getRefresh('database_' + sheet + '_query_' + md5(JSON.stringify(query)), cacheTime, () => __awaiter(this, void 0, void 0, function* () { return yield this.Api.get('/', Object.assign({}, query, { sheet })); }));
        });
    }
    item(sheet, key, cacheTime = 0) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.app.Cache.getRefresh('database_' + sheet + '_' + key, cacheTime, () => __awaiter(this, void 0, void 0, function* () { return yield this.Api.get('/', { sheet, key }); }));
        });
    }
    content(urlOrDocId, styles = 'clean', cacheTime = 0) {
        return __awaiter(this, void 0, void 0, function* () {
            // process content source
            const docId = urlOrDocId
                .replace('https://docs.google.com/document/d/', '')
                .split('/')
                .shift();
            return yield this.app.Cache.getRefresh('content_' + docId + '_' + styles, cacheTime, () => __awaiter(this, void 0, void 0, function* () { return yield this.Api.get('/content', { docId, styles }); }));
        });
    }
    set(sheet, key, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.Api.post('/', {}, { sheet, key, data, clean: true });
        });
    }
    update(sheet, key, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.Api.post('/', {}, { sheet, key, data });
        });
    }
    add(sheet, key, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.update(sheet, key, data);
        });
    }
    remove(sheet, key) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.update(sheet, key, null);
        });
    }
    increase(sheet, key, increasing) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.Api.post('/', {}, { sheet, key, increasing });
        });
    }
}
