"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const submitMeasurements_1 = __importDefault(require("./submitMeasurements"));
const auth_1 = __importDefault(require("./auth"));
const constants_1 = require("./constants");
const actions_core_1 = require("@segment/actions-core");
const destination = {
    name: 'm3ter',
    slug: 'actions-m3ter',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth2',
        fields: {
            access_key_id: {
                label: 'm3ter Access Key Id',
                description: 'Your service user Access Key Id. You can generate the service user and its Access Key Id in your m3ter console under "Settings" -> "Access" -> "Service Users" -> "Create Service User".' +
                    ' Step by step guide can be found in [m3ter Docs](https://www.m3ter.com/docs/guides/authenticating-with-the-platform/service-authentication#generating-an-api-key-and-secret-for-a-service-user)',
                type: 'password',
                required: true
            },
            api_secret: {
                label: 'm3ter Api Secret',
                description: 'Your service user Api Secret. You can generate the service user and its Api Secret in your m3ter console under "Settings" -> "Access" -> "Service Users" -> "Create Service User".' +
                    ' Step by step guide can be found in [m3ter Docs](https://www.m3ter.com/docs/guides/authenticating-with-the-platform/service-authentication#generating-an-api-key-and-secret-for-a-service-user)',
                type: 'password',
                required: true
            },
            org_id: {
                label: 'm3ter Organization Id',
                description: 'ID of your organization where your data will be submitted to',
                type: 'string',
                required: true
            }
        },
        testAuthentication: async (request, { settings }) => {
            const accessToken = await (0, auth_1.default)(request, settings.access_key_id, settings.api_secret);
            return { accessToken: accessToken };
        },
        refreshAccessToken: async (request, { settings }) => {
            const accessToken = await (0, auth_1.default)(request, settings.access_key_id, settings.api_secret);
            return { accessToken: accessToken };
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                Authorization: `Bearer ${auth?.accessToken}`,
                'User-Agent': constants_1.USER_AGENT_HEADER
            }
        };
    },
    actions: {
        submitMeasurements: submitMeasurements_1.default
    },
    presets: [
        {
            name: 'Submit usage data to m3ter',
            subscribe: submitMeasurements_1.default.defaultSubscription,
            partnerAction: 'submitMeasurements',
            mapping: (0, actions_core_1.defaultValues)(submitMeasurements_1.default.fields),
            type: 'automatic'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map