"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const datasource_1 = require("../definitions/datasource");
var AirtableModule;
(function (AirtableModule) {
    const Airtable = require("airtable");
    const SCHEMA = {
        docs: "https://airtable.com/api",
        description: "Airtable is a spreadsheet-database hybrid, with the features of a database but applied to a spreadsheet.",
        friendlyName: "Airtable",
        type: "Spreadsheet",
        datasource: {
            apiKey: {
                type: datasource_1.DatasourceFieldTypes.PASSWORD,
                default: "enter api key",
                required: true,
            },
            base: {
                type: datasource_1.DatasourceFieldTypes.STRING,
                default: "mybase",
                required: true,
            },
        },
        query: {
            create: {
                type: datasource_1.QueryTypes.FIELDS,
                customisable: true,
                fields: {
                    table: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                },
            },
            read: {
                type: datasource_1.QueryTypes.FIELDS,
                fields: {
                    table: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                    view: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                    numRecords: {
                        type: datasource_1.DatasourceFieldTypes.NUMBER,
                        default: 10,
                    },
                },
            },
            update: {
                type: datasource_1.QueryTypes.FIELDS,
                customisable: true,
                fields: {
                    id: {
                        type: datasource_1.DatasourceFieldTypes.STRING,
                        required: true,
                    },
                },
            },
            delete: {
                type: datasource_1.QueryTypes.JSON,
            },
        },
    };
    class AirtableIntegration {
        constructor(config) {
            this.config = config;
            this.client = new Airtable(config).base(config.base);
        }
        create(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const { table, json } = query;
                try {
                    return yield this.client(table).create([
                        {
                            fields: json,
                        },
                    ]);
                }
                catch (err) {
                    console.error("Error writing to airtable", err);
                    throw err;
                }
            });
        }
        read(query) {
            return __awaiter(this, void 0, void 0, function* () {
                try {
                    const records = yield this.client(query.table)
                        .select({ maxRecords: query.numRecords || 10, view: query.view })
                        .firstPage();
                    // @ts-ignore
                    return records.map(({ fields }) => fields);
                }
                catch (err) {
                    console.error("Error writing to airtable", err);
                    return [];
                }
            });
        }
        update(query) {
            return __awaiter(this, void 0, void 0, function* () {
                const { table, id, json } = query;
                try {
                    return yield this.client(table).update([
                        {
                            id,
                            fields: json,
                        },
                    ]);
                }
                catch (err) {
                    console.error("Error writing to airtable", err);
                    throw err;
                }
            });
        }
        delete(query) {
            return __awaiter(this, void 0, void 0, function* () {
                try {
                    return yield this.client(query.table).destroy(query.ids);
                }
                catch (err) {
                    console.error("Error writing to airtable", err);
                    throw err;
                }
            });
        }
    }
    module.exports = {
        schema: SCHEMA,
        integration: AirtableIntegration,
    };
})(AirtableModule || (AirtableModule = {}));
