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 _RemoteApp_instances, _RemoteApp_contextAction, _RemoteApp_customPost, _RemoteApp_meta, _RemoteApp_uiEventHandler, _RemoteApp_newNiceMeta;
import { ContextActionDefinition, CustomPostDefinition, UIEventHandlerDefinition, } from '@devvit/protos';
import { Header } from '@devvit/shared-types/Header.js';
import { createChannel, createClient, FetchTransport, Metadata as NiceMeta, } from 'nice-grpc-web';
export class RemoteApp {
    static new(gatewayOrigin, meta) {
        const channel = createChannel(gatewayOrigin, FetchTransport({ credentials: 'include' }));
        const contextAction = createClient(ContextActionDefinition, channel);
        const customPost = createClient(CustomPostDefinition, channel);
        const uiEventHandler = createClient(UIEventHandlerDefinition, channel);
        return new RemoteApp(contextAction, customPost, uiEventHandler, meta);
    }
    constructor(contextAction, customPost, uiEventHandler, meta) {
        _RemoteApp_instances.add(this);
        _RemoteApp_contextAction.set(this, void 0);
        _RemoteApp_customPost.set(this, void 0);
        _RemoteApp_meta.set(this, void 0);
        _RemoteApp_uiEventHandler.set(this, void 0);
        __classPrivateFieldSet(this, _RemoteApp_contextAction, contextAction, "f");
        __classPrivateFieldSet(this, _RemoteApp_customPost, customPost, "f");
        __classPrivateFieldSet(this, _RemoteApp_meta, {
            [Header.Installation]: { values: [meta.installation] },
            [Header.Actor]: { values: [meta.actor] },
        }, "f");
        __classPrivateFieldSet(this, _RemoteApp_uiEventHandler, uiEventHandler, "f");
    }
    async GetActions(req, meta) {
        return __classPrivateFieldGet(this, _RemoteApp_contextAction, "f").getActions(req, {
            metadata: __classPrivateFieldGet(this, _RemoteApp_instances, "m", _RemoteApp_newNiceMeta).call(this, meta),
        });
    }
    async HandleUIEvent(req, meta) {
        return __classPrivateFieldGet(this, _RemoteApp_uiEventHandler, "f").handleUIEvent(req, {
            metadata: __classPrivateFieldGet(this, _RemoteApp_instances, "m", _RemoteApp_newNiceMeta).call(this, meta),
        });
    }
    async OnAction(req, meta) {
        return __classPrivateFieldGet(this, _RemoteApp_contextAction, "f").onAction(req, {
            metadata: __classPrivateFieldGet(this, _RemoteApp_instances, "m", _RemoteApp_newNiceMeta).call(this, meta),
        });
    }
    async RenderPost(req, meta) {
        return __classPrivateFieldGet(this, _RemoteApp_customPost, "f").renderPost(req, {
            metadata: __classPrivateFieldGet(this, _RemoteApp_instances, "m", _RemoteApp_newNiceMeta).call(this, meta),
        });
    }
    async RenderPostContent(req, meta) {
        return __classPrivateFieldGet(this, _RemoteApp_customPost, "f").renderPostContent(req, {
            metadata: __classPrivateFieldGet(this, _RemoteApp_instances, "m", _RemoteApp_newNiceMeta).call(this, meta),
        });
    }
    async RenderPostComposer(req, meta) {
        return __classPrivateFieldGet(this, _RemoteApp_customPost, "f").renderPostComposer(req, {
            metadata: __classPrivateFieldGet(this, _RemoteApp_instances, "m", _RemoteApp_newNiceMeta).call(this, meta),
        });
    }
}
_RemoteApp_contextAction = new WeakMap(), _RemoteApp_customPost = new WeakMap(), _RemoteApp_meta = new WeakMap(), _RemoteApp_uiEventHandler = new WeakMap(), _RemoteApp_instances = new WeakSet(), _RemoteApp_newNiceMeta = function _RemoteApp_newNiceMeta(meta) {
    return new NiceMeta(Object.entries({ ...__classPrivateFieldGet(this, _RemoteApp_meta, "f"), ...meta }).map(([key, { values }]) => [key, values.join()]));
};
//# sourceMappingURL=remote-app.js.map