"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSegment = createSegment;
exports.getSegment = getSegment;
exports.getDomain = getDomain;
const actions_core_1 = require("@segment/actions-core");
const constants_1 = require("./constants");
async function createSegment(request, region, name) {
    const json = {
        name,
        visitors: ["empty-visitor"]
    };
    const response = await request(`${getDomain(region)}/${constants_1.SEGMENT_ENDPOINT}/upload`, {
        method: 'POST',
        json
    });
    return response.data.segmentId;
}
async function getSegment(request, region, segmentId) {
    const response = await request(`${getDomain(region)}/${constants_1.SEGMENT_ENDPOINT}/${segmentId}`, {
        method: 'GET'
    });
    if (response.data.id !== segmentId) {
        throw new actions_core_1.IntegrationError(`Pendo segment with ID ${segmentId} not found`, 'SEGMENT_NOT_FOUND', 404);
    }
    return segmentId;
}
function getDomain(regionName) {
    const region = Object.values(constants_1.REGIONS).find(r => r.name === regionName);
    if (!region) {
        throw new actions_core_1.IntegrationError(`Invalid region: ${regionName}`, actions_core_1.ErrorCodes.NOT_IMPLEMENTED, 400);
    }
    return region.domain;
}
//# sourceMappingURL=functions.js.map