"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sendAssetData_1 = __importDefault(require("./sendAssetData"));
const destination = {
    name: '1Plusx Asset Api',
    slug: 'actions-1plusx-asset-api',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth2',
        fields: {
            client_id: {
                label: 'Client ID',
                description: 'Your 1plusX Client ID. Please refer to your 1PlusX representative to obtain it.',
                type: 'string',
                required: true
            },
            key_id: {
                label: 'Key ID',
                description: 'Your 1plusX Key ID. Available in 1plusX UI in the "API Keys" section.',
                type: 'password',
                required: true
            },
            client_secret: {
                label: 'Secret',
                description: 'Your 1plusX Secret. Available in 1plusX UI in the "API Keys" section.',
                type: 'password',
                required: true
            }
        },
        testAuthentication: async (request, { settings }) => {
            const res = await request('https://us.1plusx.io/api/auth/token', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Basic ${Buffer.from(settings.key_id + ':' + settings.client_secret).toString('base64')}`
                },
                body: '{"grant_type":"client_credentials"}'
            });
            if (res.status == 200) {
                return { accessToken: res.data.access_token };
            }
            else {
                throw new Error(res.status + res.statusText);
            }
        },
        refreshAccessToken: async (request, { settings }) => {
            const res = await request('https://us.1plusx.io/api/auth/token', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Basic ${Buffer.from(settings.key_id + ':' + settings.client_secret).toString('base64')}`
                },
                body: '{"grant_type":"client_credentials"}'
            });
            return { accessToken: res.data.access_token };
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`
            }
        };
    },
    actions: {
        sendAssetData: sendAssetData_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map