import { definePlugin } from "../../../utils/plugin-utils";
import { createPluginMethod } from "../../../utils/plugin-utils";
import { GetTableRecordOptionsInputSchema, GetTableRecordApiResponseSchema, RecordItemSchema, } from "./schemas";
import { tableIdResolver, tableRecordIdResolver } from "../../../resolvers";
import { tableRecordFormatter } from "../../../formatters";
import { createFieldKeyTranslator, transformRecordItem } from "../utils";
import { tablesDefaults } from "../shared";
export const getTableRecordPlugin = definePlugin((sdk) => createPluginMethod(sdk, {
    ...tablesDefaults,
    name: "getTableRecord",
    type: "item",
    itemType: "Record",
    inputSchema: GetTableRecordOptionsInputSchema,
    outputSchema: RecordItemSchema,
    resolvers: { table: tableIdResolver, record: tableRecordIdResolver },
    formatter: tableRecordFormatter,
    handler: async ({ sdk, options }) => {
        const { api } = sdk.context;
        const tableId = "table" in options ? options.table : options.tableId;
        const recordId = "record" in options ? options.record : options.recordId;
        const rawResponse = await api.get(`/tables/api/v1/tables/${tableId}/records/${recordId}`, {
            authRequired: true,
            resource: { type: "record", id: recordId },
        });
        const response = GetTableRecordApiResponseSchema.parse(rawResponse);
        const translator = await createFieldKeyTranslator({
            api,
            tableId,
            keyMode: options.keyMode,
        });
        return {
            data: {
                ...transformRecordItem(response.data),
                data: translator.translateOutput(response.data.data),
            },
        };
    },
}));
