import { definePlugin, createPluginMethod } from "../../../utils/plugin-utils";
import { UpdateTriggerInboxSchema, TriggerInboxItemSchema } from "./schemas";
import { triggerInboxResolver } from "../../../resolvers";
import { resolveTriggerInboxId } from "../utils";
import { triggersDefaults } from "../shared";
export const updateTriggerInboxPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    ...triggersDefaults,
    name: "updateTriggerInbox",
    type: "update",
    itemType: "TriggerInbox",
    inputSchema: UpdateTriggerInboxSchema,
    outputSchema: TriggerInboxItemSchema,
    resolvers: { inbox: triggerInboxResolver },
    handler: async ({ sdk, options }) => {
        const { inbox, notificationUrl } = options;
        const inboxId = await resolveTriggerInboxId({
            api: sdk.context.api,
            inbox,
        });
        const requestBody = {};
        if (notificationUrl !== undefined) {
            requestBody.notification_url = notificationUrl;
        }
        const rawResponse = await sdk.context.api.patch(`/trigger-inbox/api/v1/inboxes/${encodeURIComponent(inboxId)}`, requestBody, {
            authRequired: true,
            resource: { type: "trigger_inbox", id: inboxId },
        });
        return { data: TriggerInboxItemSchema.parse(rawResponse) };
    },
}));
