import { definePlugin } from "../../utils/plugin-utils";
import { createPluginMethod } from "../../utils/plugin-utils";
import { connectionIdGenericResolver } from "../../resolvers";
import { transformConnectionItem } from "../../utils/domain-utils";
import { ConnectionItemSchema } from "../../schemas/Connection";
import { GetConnectionParamSchema } from "./schemas";
export const getConnectionPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    name: "getConnection",
    categories: ["connection"],
    type: "item",
    itemType: "Connection",
    inputSchema: GetConnectionParamSchema,
    outputSchema: ConnectionItemSchema,
    resolvers: { connection: connectionIdGenericResolver },
    handler: async ({ sdk, options }) => {
        const { api } = sdk.context;
        const resolvedConnectionId = options.connection ??
            options.connectionId ??
            options.authenticationId;
        if (!resolvedConnectionId) {
            throw new Error("connection is required");
        }
        const response = await api.get(`/api/v0/connections/${encodeURIComponent(String(resolvedConnectionId))}`, {
            resource: {
                type: "connection",
                id: String(resolvedConnectionId),
            },
        });
        return { ...response, data: transformConnectionItem(response.data) };
    },
}));
