"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * A collection of keys and values.
 */
class Collection {
    /**
     * Opens a collection
     * @param name name of the collection to open
     */
    constructor(name) {
        this.name = name;
    }
    /**
     * Stores data in the collection associated key
     * @param key key for data
     * @param obj value to store
     */
    put(key, obj) {
        return new Promise((resolve, reject) => {
            try {
                bridge.dispatch("fly.Data.put", this.name, key, JSON.stringify(obj), (err, ok) => {
                    if (err) {
                        reject(new Error(err));
                        return;
                    }
                    resolve(ok);
                });
            }
            catch (err) {
                reject(err);
            }
        });
    }
    /**
     * Retrieves data from the collection store
     * @param key key to retrieve
     */
    get(key) {
        return new Promise((resolve, reject) => {
            bridge.dispatch("fly.Data.get", this.name, key, (err, res) => {
                if (err) {
                    reject(new Error(err));
                    return;
                }
                try {
                    resolve(JSON.parse(res));
                }
                catch (err) {
                    reject(err);
                }
            });
        });
    }
    /**
     * Deletes data from the collection store.
     * @param key key to delete
     */
    del(key) {
        return new Promise((resolve, reject) => {
            bridge.dispatch("fly.Data.del", this.name, key, (err, ok) => {
                if (err) {
                    reject(new Error(err));
                    return;
                }
                resolve(ok);
            });
        });
    }
}
exports.Collection = Collection;
const data = {
    collection(name) {
        return new Collection(name);
    },
    dropCollection(name) {
        return new Promise((resolve, reject) => {
            bridge.dispatch("fly.Data.dropCollection", name, (err, ok) => {
                if (err) {
                    reject(new Error(err));
                    return;
                }
                resolve(ok);
            });
        });
    }
};
exports.default = data;
//# sourceMappingURL=data.js.map