"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAndCreateClient = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const zod_1 = __importDefault(require("zod"));
const client_1 = require("./api/client");
const settings_1 = require("../settings");
const lodash_1 = require("lodash");
const validateAndCreateClient = async ({ fieldsSchema, payload, requiresAppId = true, }) => {
    const { settings: { appId: settingsAppId, apiKey, baseUrl }, fields, settings, } = (0, extensions_core_1.validate)({
        schema: zod_1.default.object({
            fields: fieldsSchema,
            settings: settings_1.SettingsValidationSchema,
            pathway: zod_1.default.object({ id: zod_1.default.string() }),
            activity: zod_1.default.object({ id: zod_1.default.string() }),
        }),
        payload,
    });
    const { patient, pathway, activity } = payload;
    const { appId: fieldsAppId } = payload.fields;
    // App ID via fields takes precedence over app ID via settings
    const appId = (0, lodash_1.isNil)(fieldsAppId) || (0, lodash_1.isEmpty)(fieldsAppId) ? settingsAppId : fieldsAppId;
    if (requiresAppId && ((0, lodash_1.isNil)(appId) || (0, lodash_1.isEmpty)(appId))) {
        throw new Error('App ID is required. Either provide it via the extension settings or the action field.');
    }
    const brazeClient = new client_1.BrazeClient({
        apiKey,
        baseUrl,
    });
    return {
        brazeClient,
        appId,
        fields,
        settings,
        patient,
        pathway,
        activity,
    };
};
exports.validateAndCreateClient = validateAndCreateClient;
//# sourceMappingURL=validateAndCreateClient.js.map