"use strict";
/**
 * An API for accessing a regional, volatile cache. Data stored in `@fly/cache` can have an associated per-key time to live (TTL), and we will evict key data automatically after the elapsed TTL. We will also evict unused data when we need to reclaim space.
 *
 * ```javascript
 * import cache from "@fly/cache"
 *
 * await cache.set("test-key", "test-value")
 *
 * const s = await cache.getString("test-key")
 * ```
 *
 * See {@link fly/cache/response} for caching HTTP Response objects.
 *
 * See {@link fly/cache/global} for global cache del/purge
 *
 * @preferred
 * @module fly/cache
 */
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Get an ArrayBuffer value (or null) from the cache
 * @param key The key to get
 * @return Raw bytes stored for provided key or null if empty.
 */
function get(key) {
    return new Promise(function cacheGetPromise(resolve, reject) {
        bridge.dispatch("flyCacheGet", key, function cacheGetCallback(err, value) {
            if (err != null) {
                reject(err);
                return;
            }
            resolve(value);
        });
    });
}
exports.get = get;
/**
 * Get a string value (or null) from the cache
 *
 * @param key The key to get
 * @returns Data stored at the key, or null if none exists
 */
async function getString(key) {
    const buf = await get(key);
    if (!buf) {
        return null;
    }
    try {
        return new TextDecoder("utf-8").decode(buf);
    }
    catch (err) {
        return null;
    }
}
exports.getString = getString;
/**
 * Get multiple values from the cache.
 * @param keys list of keys to retrieve
 * @returns List of results in the same order as the provided keys
 */
function getMulti(keys) {
    return new Promise(function cacheGetMultiPromise(resolve, reject) {
        bridge.dispatch("flyCacheGetMulti", JSON.stringify(keys), function cacheGetMultiCallback(err, ...values) {
            if (err != null) {
                reject(err);
                return;
            }
            resolve(values);
        });
    });
}
exports.getMulti = getMulti;
/**
 * Get multiple string values from the cache
 * @param keys list of keys to retrieve
 * @returns list of results in the same order as the provided keys
 */
async function getMultiString(keys) {
    const raw = await getMulti(keys);
    return raw.map((b) => b ? new TextDecoder("utf-8").decode(b) : null);
}
exports.getMultiString = getMultiString;
/**
 * Sets a value at the specified key, with an optional ttl
 * @param key The key to add or overwrite
 * @param value Data to store at the specified key, up to 2MB
 * @param ttl Time to live (in seconds)
 * @returns true if the set was successful
 */
function set(key, value, options) {
    if (typeof value !== "string" && !(value instanceof ArrayBuffer)) {
        throw new Error("Cache values must be either a string or array buffer");
    }
    return new Promise(function cacheSetPromise(resolve, reject) {
        bridge.dispatch("flyCacheSet", key, value, options && JSON.stringify(options), function cacheSetCallback(err, ok) {
            if (err != null) {
                reject(err);
                return;
            }
            resolve(ok);
        });
    });
}
exports.set = set;
/**
 * Add or overwrite a key's  time to live
 * @param key The key to modify
 * @param ttl Expiration time remaining in seconds
 * @returns true if ttl was successfully updated
 */
function expire(key, ttl) {
    return new Promise(function cacheSetPromise(resolve, reject) {
        bridge.dispatch("flyCacheExpire", key, ttl, function cacheSetCallback(err, ok) {
            if (err != null) {
                reject(err);
                return;
            }
            resolve(ok);
        });
    });
}
exports.expire = expire;
/**
 * Replace tags for a given cache key
 * @param key The key to modify
 * @param tags Tags to apply to key
 * @returns true if tags were successfully updated
 */
function setTags(key, tags) {
    return new Promise(function cacheSetTagsPromise(resolve, reject) {
        bridge.dispatch("flyCacheSetTags", key, tags, function cacheSetTagsCallback(err, ok) {
            if (err != null) {
                reject(err);
                return;
            }
            resolve(ok);
        });
    });
}
exports.setTags = setTags;
/**
 * Purges all cache entries with the given tag
 * @param tag Tag to purge
 */
function purgeTag(tag) {
    return new Promise(function cachePurgeTagsPromise(resolve, reject) {
        bridge.dispatch("flyCachePurgeTags", tag, function cachePurgeTagsCallback(err, keys) {
            if (err != null || !keys) {
                reject(err || "weird result");
                return;
            }
            const result = JSON.parse(keys);
            if (result instanceof Array) {
                resolve(result);
                return;
            }
            else {
                reject("got back gibberish");
            }
        });
    });
}
exports.purgeTag = purgeTag;
/**
 * Deletes the value (if any) at the specified key
 * @param key Key to delete
 * @returns true if delete was successful
 */
function del(key) {
    return new Promise(function cacheDelPromise(resolve, reject) {
        bridge.dispatch("flyCacheDel", key, function cacheDelCallback(err, ok) {
            if (err != null) {
                reject(err);
                return;
            }
            resolve(ok);
        });
    });
}
exports.del = del;
/**
 * A library for caching/retrieving Response objects
 *
 * See {@link fly/cache/response}
 */
var response_1 = require("./response");
exports.responseCache = response_1.default;
/**
 * API for sending global cache notifications
 *
 * See {@link fly/cache/global}
 */
const global_1 = require("./global");
const cache = {
    get,
    getString,
    getMulti,
    getMultiString,
    set,
    expire,
    del,
    setTags,
    purgeTag,
    global: global_1.default
};
exports.default = cache;
//# sourceMappingURL=index.js.map