"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultGoalFields = void 0;
exports.sendGoal = sendGoal;
const unit_1 = require("./unit");
const actions_core_1 = require("@segment/actions-core");
const event_1 = require("./event");
const timestamp_1 = require("./timestamp");
exports.defaultGoalFields = {
    units: {
        label: 'Units',
        type: 'object',
        required: true,
        description: 'The units of the goal to track. Mapping of unit name to source property in the event payload. Create Units in the Settings > Units section of the ABsmartly Web Console',
        defaultObjectUI: 'keyvalue:only',
        default: {
            anonymousId: {
                '@path': '$.anonymousId'
            },
            userId: {
                '@path': '$.userId'
            }
        }
    },
    name: {
        label: 'Goal Name',
        type: 'string',
        required: true,
        description: 'The name of the goal to track',
        default: {
            '@path': '$.event'
        }
    },
    properties: {
        label: 'Goal Properties',
        type: 'object',
        required: true,
        description: 'Custom properties of the goal',
        default: {
            '@path': '$.properties'
        }
    },
    ...event_1.defaultEventFields
};
function sendGoal(request, timestamp, payload, settings) {
    if (typeof payload.name !== 'string' || payload.name.length == 0) {
        throw new actions_core_1.PayloadValidationError('Goal `name` is required to be a non-empty string');
    }
    if (payload.properties != null && typeof payload.properties != 'object') {
        throw new actions_core_1.PayloadValidationError('Goal `properties` if present is required to be an object');
    }
    const event = {
        historic: true,
        publishedAt: (0, timestamp_1.unixTimestampOf)(timestamp),
        units: (0, unit_1.mapUnits)(payload),
        goals: [
            {
                name: payload.name,
                achievedAt: (0, timestamp_1.unixTimestampOf)(timestamp),
                properties: payload.properties ?? null
            }
        ]
    };
    return (0, event_1.sendEvent)(request, settings, event, payload.agent, payload.application);
}
//# sourceMappingURL=goal.js.map