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 _LocalStorageBroker_localStorage, _LocalStorageBroker_counter, _LocalStorageBroker_observersByTopic;
import { Message, } from '@devvit/protos/types/devvit/plugin/streambroker/streambroker.js';
import { Empty } from '@devvit/protos/types/google/protobuf/empty.js';
import { Observable } from 'rxjs';
import { namespacedKey } from './utils/NamespacedKey.js';
/**
 * Observable KV storage
 */
class LocalStorageBroker {
    constructor(localStorage) {
        _LocalStorageBroker_localStorage.set(this, void 0);
        _LocalStorageBroker_counter.set(this, 0);
        _LocalStorageBroker_observersByTopic.set(this, {});
        __classPrivateFieldSet(this, _LocalStorageBroker_localStorage, localStorage, "f");
        try {
            __classPrivateFieldGet(this, _LocalStorageBroker_localStorage, "f").clear();
        }
        catch { } // eslint-disable-line no-empty
    }
    // RPC
    Get(cursor, metadata) {
        var _a, _b;
        const topic = 'broker-' + namespacedKey(metadata, cursor.topic);
        const id = (__classPrivateFieldSet(this, _LocalStorageBroker_counter, (_b = __classPrivateFieldGet(this, _LocalStorageBroker_counter, "f"), _a = _b++, _b), "f"), _a) + '';
        return new Observable((subscriber) => {
            __classPrivateFieldGet(this, _LocalStorageBroker_observersByTopic, "f")[topic] = __classPrivateFieldGet(this, _LocalStorageBroker_observersByTopic, "f")[topic] || {};
            __classPrivateFieldGet(this, _LocalStorageBroker_observersByTopic, "f")[topic][id] = subscriber;
            const count = parseInt(__classPrivateFieldGet(this, _LocalStorageBroker_localStorage, "f").getItem(topic) || '0');
            for (let i = cursor.offset; i < count; i++) {
                const item = __classPrivateFieldGet(this, _LocalStorageBroker_localStorage, "f").getItem(topic + '-' + i);
                if (item) {
                    subscriber.next(Message.fromPartial({
                        topic: cursor.topic,
                        value: item,
                    }));
                }
                else {
                    subscriber.error(new Error('Item not found'));
                }
            }
            if (cursor.closeAtEnd) {
                subscriber.complete();
            }
            return (() => {
                delete __classPrivateFieldGet(this, _LocalStorageBroker_observersByTopic, "f")[topic][id];
            });
        });
    }
    // RPC
    async Put(message, metadata) {
        const topic = 'broker-' + namespacedKey(metadata, message.topic);
        const next = parseInt(__classPrivateFieldGet(this, _LocalStorageBroker_localStorage, "f").getItem(topic) || '0');
        __classPrivateFieldGet(this, _LocalStorageBroker_localStorage, "f").setItem(topic + '-' + next, message.value);
        __classPrivateFieldGet(this, _LocalStorageBroker_localStorage, "f").setItem(topic, next + 1 + '');
        const listeners = Object.values(__classPrivateFieldGet(this, _LocalStorageBroker_observersByTopic, "f")[message.topic] || {});
        for (const listener of listeners) {
            listener.next(message);
        }
        return Empty.fromPartial({});
    }
}
_LocalStorageBroker_localStorage = new WeakMap(), _LocalStorageBroker_counter = new WeakMap(), _LocalStorageBroker_observersByTopic = new WeakMap();
// Export classes
export { LocalStorageBroker };
