"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const send_1 = __importDefault(require("./send"));
const btoa_lite_1 = __importDefault(require("btoa-lite"));
const destination = {
    name: 'SingleStore',
    slug: 'actions-singlestore',
    mode: 'cloud',
    authentication: {
        scheme: 'custom',
        fields: {
            host: {
                label: 'Host',
                description: 'The host of the SingleStore database.',
                type: 'string',
                required: true
            },
            port: {
                label: 'Port',
                description: 'The port of the SingleStore Data API. Defaults to 443.',
                type: 'string',
                default: '443'
            },
            username: {
                label: 'Username',
                description: 'The username of the SingleStore database.',
                type: 'string',
                required: true
            },
            password: {
                label: 'Password',
                description: 'The password of the SingleStore database.',
                type: 'password',
                required: true
            },
            dbName: {
                label: 'Database Name',
                description: 'The name of the database.',
                type: 'string',
                required: true
            },
            tableName: {
                label: 'Table Name',
                description: 'The name of the table. Defaults to "segment_data".',
                type: 'string',
                required: true,
                default: 'segment_data'
            }
        },
        testAuthentication: async (request, { settings }) => {
            const { host, port, username, password, dbName, tableName } = settings;
            const url = `https://${host}:${port}/api/v2/exec`;
            const encodedCredentials = (0, btoa_lite_1.default)(`${username}:${password}`);
            const sql = `
        CREATE TABLE IF NOT EXISTS \`${tableName.replace(/`/g, '``')}\` (
          messageId TEXT NOT NULL,
          timestamp DATETIME(6) NOT NULL,
          type TEXT NOT NULL,
          event TEXT,
          name TEXT,
          properties JSON,
          userId TEXT,
          anonymousId TEXT,
          groupId TEXT,
          traits JSON,
          context JSON,
          SHARD KEY ()
        ) AUTOSTATS_CARDINALITY_MODE=PERIODIC AUTOSTATS_HISTOGRAM_MODE=CREATE SQL_MODE='STRICT_ALL_TABLES';
      `;
            const requestData = {
                sql,
                database: dbName
            };
            const response = await request(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Basic ${encodedCredentials}`
                },
                json: requestData,
                throwHttpErrors: false
            });
            if (response.status !== 200 || response.ok === false) {
                throw new actions_core_1.IntegrationError(`Failed to create table: ${(await response.text()) || 'Unknown error'}`, response.statusText, response.status);
            }
            return response;
        }
    },
    actions: {
        send: send_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map