import { definePlugin } from "../../../utils/plugin-utils";
import { createPluginMethod } from "../../../utils/plugin-utils";
import { DeleteTableFieldsOptionsInputSchema } from "./schemas";
import { tableIdResolver, tableFieldIdsResolver } from "../../../resolvers";
import { resolveFieldKeys } from "../utils";
import { tablesDefaults } from "../shared";
export const deleteTableFieldsPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    ...tablesDefaults,
    name: "deleteTableFields",
    type: "delete",
    itemType: "Field",
    inputSchema: DeleteTableFieldsOptionsInputSchema,
    resolvers: { table: tableIdResolver, fields: tableFieldIdsResolver },
    confirm: "delete",
    handler: async ({ sdk, options }) => {
        const { api } = sdk.context;
        const tableId = "table" in options ? options.table : options.tableId;
        const fieldKeys = "fields" in options ? options.fields : options.fieldKeys;
        const numericFieldIds = await resolveFieldKeys({
            api,
            tableId,
            fieldKeys,
        });
        await api.delete(`/tables/api/v1/tables/${tableId}/fields`, { field_ids: numericFieldIds }, {
            authRequired: true,
            resource: { type: "table", id: tableId },
        });
        return { success: true };
    },
}));
