import { definePlugin } from "../../../utils/plugin-utils";
import { createPluginMethod } from "../../../utils/plugin-utils";
import { GetTableApiResponseSchema, GetTableOptionsInputSchema, TableItemSchema, } from "./schemas";
import { tableIdResolver } from "../../../resolvers";
import { transformTableItem } from "../utils";
import { tablesDefaults } from "../shared";
export const getTablePlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    ...tablesDefaults,
    name: "getTable",
    type: "item",
    itemType: "Table",
    inputSchema: GetTableOptionsInputSchema,
    outputSchema: TableItemSchema,
    resolvers: { table: tableIdResolver },
    handler: async ({ sdk, options }) => {
        const tableId = "table" in options ? options.table : options.tableId;
        const rawResponse = await sdk.context.api.get(`/tables/api/v1/tables/${tableId}`, {
            authRequired: true,
            resource: { type: "table", id: tableId },
        });
        const response = GetTableApiResponseSchema.parse(rawResponse);
        return { data: transformTableItem(response.data) };
    },
}));
