import { definePlugin } from "../../utils/plugin-utils";
import { createPluginMethod } from "../../utils/plugin-utils";
import { FindFirstConnectionSchema } from "./schemas";
import { ConnectionItemSchema } from "../../schemas/Connection";
import { ZapierResourceNotFoundError } from "../../types/errors";
export const findFirstConnectionPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    name: "findFirstConnection",
    categories: ["connection"],
    type: "item",
    itemType: "Connection",
    inputSchema: FindFirstConnectionSchema,
    outputSchema: ConnectionItemSchema,
    handler: async ({ sdk, options = {} }) => {
        const connectionsResponse = await sdk.listConnections({
            ...options,
            maxItems: 1,
        });
        if (connectionsResponse.data.length === 0) {
            throw new ZapierResourceNotFoundError("No connection found matching the specified criteria", { resourceType: "Connection" });
        }
        return { data: connectionsResponse.data[0] };
    },
}));
