import { definePlugin } from "../../../utils/plugin-utils";
import { createPluginMethod } from "../../../utils/plugin-utils";
import { CreateTableFieldsOptionsInputSchema, CreateTableFieldsApiResponseSchema, FieldItemSchema, } from "./schemas";
import { tableIdResolver, tableFieldsResolver } from "../../../resolvers";
import { transformFieldItem } from "../utils";
import { tablesDefaults } from "../shared";
export const createTableFieldsPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    ...tablesDefaults,
    name: "createTableFields",
    type: "create",
    itemType: "Field",
    returnType: "FieldItem[]",
    inputSchema: CreateTableFieldsOptionsInputSchema,
    outputSchema: FieldItemSchema,
    resolvers: { table: tableIdResolver, fields: tableFieldsResolver },
    handler: async ({ sdk, options }) => {
        const tableId = "table" in options ? options.table : options.tableId;
        const rawResponse = await sdk.context.api.post(`/tables/api/v1/tables/${tableId}/fields`, { new_fields: options.fields }, {
            authRequired: true,
            resource: { type: "table", id: tableId },
        });
        const response = CreateTableFieldsApiResponseSchema.parse(rawResponse);
        return {
            data: response.data.map((changeset) => transformFieldItem(changeset.new)),
        };
    },
}));
