import { definePlugin, createPluginMethod } from "../../../utils/plugin-utils";
import { AckTriggerInboxMessagesSchema, AckTriggerInboxMessagesItemSchema, } from "./schemas";
import { triggerInboxResolver } from "../../../resolvers";
import { resolveTriggerInboxId } from "../utils";
import { triggersDefaults } from "../shared";
export const ackTriggerInboxMessagesPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    ...triggersDefaults,
    name: "ackTriggerInboxMessages",
    type: "create",
    itemType: "TriggerInboxAck",
    inputSchema: AckTriggerInboxMessagesSchema,
    outputSchema: AckTriggerInboxMessagesItemSchema,
    resolvers: {
        inbox: triggerInboxResolver,
        // No way to look up a lease — leases are short-lived, only the
        // most recent leaseTriggerInboxMessages caller knows the ID.
        // Static resolver prompts for free-text input.
        lease: { type: "static", inputType: "text" },
    },
    handler: async ({ sdk, options }) => {
        const { inbox, lease, messages } = options;
        const inboxId = await resolveTriggerInboxId({
            api: sdk.context.api,
            inbox,
        });
        const requestBody = {
            lease_id: lease,
        };
        if (messages !== undefined) {
            requestBody.message_ids = messages;
        }
        const rawResponse = await sdk.context.api.post(`/trigger-inbox/api/v1/inboxes/${encodeURIComponent(inboxId)}/messages/ack`, requestBody, { authRequired: true });
        return { data: AckTriggerInboxMessagesItemSchema.parse(rawResponse) };
    },
}));
