import { definePlugin, createPluginMethod } from "../../../utils/plugin-utils";
import { ReleaseTriggerInboxMessagesSchema, ReleaseTriggerInboxMessagesItemSchema, } from "./schemas";
import { triggerInboxResolver, triggerMessagesResolver, } from "../../../resolvers";
import { resolveTriggerInboxId } from "../utils";
import { triggersDefaults } from "../shared";
export const releaseTriggerInboxMessagesPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    ...triggersDefaults,
    name: "releaseTriggerInboxMessages",
    type: "create",
    itemType: "TriggerInboxRelease",
    inputSchema: ReleaseTriggerInboxMessagesSchema,
    outputSchema: ReleaseTriggerInboxMessagesItemSchema,
    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" },
        messages: triggerMessagesResolver,
    },
    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/release`, requestBody, { authRequired: true });
        return {
            data: ReleaseTriggerInboxMessagesItemSchema.parse(rawResponse),
        };
    },
}));
