import { definePlugin } from "../../utils/plugin-utils";
import { createPluginMethod } from "../../utils/plugin-utils";
import { FindUniqueConnectionSchema } from "./schemas";
import { ZapierResourceNotFoundError, ZapierValidationError, } from "../../types/errors";
import { ConnectionItemSchema } from "../../schemas/Connection";
export const findUniqueConnectionPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    name: "findUniqueConnection",
    categories: ["connection"],
    type: "item",
    itemType: "Connection",
    inputSchema: FindUniqueConnectionSchema,
    outputSchema: ConnectionItemSchema,
    handler: async ({ sdk, options = {} }) => {
        const connectionsResponse = await sdk.listConnections({
            ...options,
            maxItems: 2, // Get up to 2 to check for uniqueness
        });
        if (connectionsResponse.data.length === 0) {
            throw new ZapierResourceNotFoundError("No connection found matching the specified criteria", { resourceType: "Connection" });
        }
        if (connectionsResponse.data.length > 1) {
            throw new ZapierValidationError("Multiple connections found matching the specified criteria. Expected exactly one.");
        }
        return { data: connectionsResponse.data[0] };
    },
}));
