import { definePlugin, createPluginMethod } from "../../utils/plugin-utils";
import { GetAppInputSchema } from "./schemas";
import { AppItemSchema } from "../../schemas/App";
import { ZapierAppNotFoundError } from "../../types/errors";
import { appKeyResolver } from "../../resolvers";
export const getAppPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    name: "getApp",
    categories: ["app"],
    type: "item",
    itemType: "App",
    inputSchema: GetAppInputSchema,
    outputSchema: AppItemSchema,
    resolvers: { app: appKeyResolver },
    handler: async ({ sdk, options }) => {
        const appKey = "app" in options ? options.app : options.appKey;
        const appsIterable = sdk.listApps({ apps: [appKey] }).items();
        for await (const app of appsIterable) {
            return { data: app };
        }
        throw new ZapierAppNotFoundError("App not found", { appKey });
    },
}));
