"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.startCareFlowAndSession = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const addEventLog_1 = require("../../../../../src/lib/awell/addEventLog");
exports.startCareFlowAndSession = {
    key: 'startCareFlowAndSession',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Start Care flow and session',
    description: 'Start a new care flow and immediately create a session',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    supports_automated_retries: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        const { fields: { careFlowDefinitionId, stakeholderDefinitionId, baselineInfo }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsValidationSchema,
            }),
            payload,
        });
        const sdk = await helpers.awellSdk();
        const resp = await sdk.orchestration.mutation({
            startHostedPathwaySession: {
                __args: {
                    input: {
                        patient_id: payload.patient.id,
                        pathway_definition_id: careFlowDefinitionId,
                        // If the stakeholder is the patient, we don't need to specify a stakeholder definition id
                        stakeholder_definition_id: stakeholderDefinitionId === 'patient'
                            ? undefined
                            : stakeholderDefinitionId,
                        data_points: baselineInfo,
                    },
                },
                success: true,
                pathway_id: true,
                session_url: true,
            },
        });
        const { startHostedPathwaySession: { pathway_id, session_url }, } = resp;
        await onComplete({
            data_points: {
                careFlowId: pathway_id,
                sessionUrl: session_url,
            },
            events: [
                (0, addEventLog_1.addActivityEventLog)({
                    message: `Care flow started with instance id ${pathway_id}. Session URL is ${session_url}.`,
                }),
            ],
        });
    },
};
//# sourceMappingURL=startCareFlowAndSession.js.map