var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _LocalStorageKV_localStorage;
import { KeySet, MessageSet } from '@devvit/protos';
import { Empty } from '@devvit/protos/types/google/protobuf/empty.js';
import { namespacedKey } from './utils/NamespacedKey.js';
class LocalStorageKV {
    constructor(localStorage) {
        _LocalStorageKV_localStorage.set(this, void 0);
        __classPrivateFieldSet(this, _LocalStorageKV_localStorage, localStorage, "f");
    }
    // RPC
    async Get(request, metadata) {
        const response = MessageSet.fromPartial({ messages: {} });
        for (const key of request.keys) {
            const value = __classPrivateFieldGet(this, _LocalStorageKV_localStorage, "f").getItem(namespacedKey(metadata, key));
            if (value != null) {
                response.messages[key] = value;
            }
        }
        return response;
    }
    // RPC
    async Del(request, metadata) {
        for (const key of request.keys) {
            __classPrivateFieldGet(this, _LocalStorageKV_localStorage, "f").removeItem(namespacedKey(metadata, key));
        }
        return Empty.fromPartial({});
    }
    // RPC
    async Put(request, metadata) {
        for (const key in request.messages) {
            __classPrivateFieldGet(this, _LocalStorageKV_localStorage, "f").setItem(namespacedKey(metadata, key), request.messages[key]);
        }
        return Empty.fromPartial({});
    }
    // RPC
    async List(_request, metadata) {
        // TODO: use request.filter
        const result = KeySet.fromPartial({});
        const namespace = namespacedKey(metadata, '');
        for (let i = 0; i < __classPrivateFieldGet(this, _LocalStorageKV_localStorage, "f").length; ++i) {
            const key = __classPrivateFieldGet(this, _LocalStorageKV_localStorage, "f").key(i);
            if (key && key.startsWith(namespace)) {
                // un-namespace before pushing to avoid exposing namespace to client
                result.keys.push(key.split(':')[1]);
            }
        }
        return result;
    }
}
_LocalStorageKV_localStorage = new WeakMap();
// Export classes
export { LocalStorageKV };
