import { definePlugin } from "../../utils/plugin-utils";
import { createPluginMethod } from "../../utils/plugin-utils";
import { GetActionInputSchema } from "./schemas";
import { ZapierResourceNotFoundError } from "../../types/errors";
import { appKeyResolver, actionTypeResolver, actionKeyResolver, } from "../../resolvers";
import { ActionItemSchema } from "../../schemas/Action";
import { setMethodMetadata } from "../../utils/telemetry-context";
export const getActionPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    name: "getAction",
    categories: ["action"],
    type: "item",
    itemType: "Action",
    inputSchema: GetActionInputSchema,
    outputSchema: ActionItemSchema,
    resolvers: {
        app: appKeyResolver,
        actionType: actionTypeResolver,
        action: actionKeyResolver,
    },
    handler: async ({ sdk, options }) => {
        const appKey = "app" in options ? options.app : options.appKey;
        const actionKey = "action" in options ? options.action : options.actionKey;
        const { actionType } = options;
        // selectedApi is intentionally omitted — getAction doesn't resolve a
        // versioned implementation ID; it delegates to sdk.listActions, which
        // sets selectedApi in its own scope.
        setMethodMetadata({
            operationType: actionType,
            operationKey: actionKey,
        });
        for await (const action of sdk.listActions({ app: appKey }).items()) {
            if ((action.key === actionKey || action.id === actionKey) &&
                action.action_type === actionType) {
                return { data: action };
            }
        }
        throw new ZapierResourceNotFoundError(`Action not found: ${actionKey} with type ${actionType}`, { resourceType: "Action", resourceId: `${actionKey} (${actionType})` });
    },
}));
