"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MedicationExtractorApi = exports.FetchError = void 0;
const crypto_1 = require("crypto");
const lodash_1 = require("lodash");
class FetchError extends Error {
    constructor(statusCode, statusText, responseBody) {
        super(`${statusCode} ${statusText}`);
        this.statusCode = statusCode;
        this.statusText = statusText;
        this.responseBody = (0, lodash_1.isEmpty)(responseBody)
            ? ''
            : JSON.stringify(responseBody);
    }
}
exports.FetchError = FetchError;
class MedicationExtractorApi {
    constructor() {
        this.baseUrl = 'https://medication-extractor-390819626634.us-central1.run.app';
    }
    constructUrl(url) {
        return `${this.baseUrl}${url}`;
    }
    async extractMedicationFromImage({ imageUrl, context, }) {
        const url = this.constructUrl(`/medication_extractor/`);
        const request_id = (0, crypto_1.randomUUID)();
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'X-Request-ID': request_id,
            },
            body: JSON.stringify({
                image_url: imageUrl,
                context: {
                    pathway_id: context === null || context === void 0 ? void 0 : context.pathwayId,
                    activity_id: context === null || context === void 0 ? void 0 : context.activityId,
                    request_id,
                },
            }),
        });
        // Promise resolved and HTTP status is successful
        if (response.ok) {
            const res = (await response.json());
            // API-specific error handling
            if (res.status !== 'OK') {
                throw new FetchError(500, res.status, res);
            }
            return res;
        }
        else {
            // Promise resolved but HTTP status failed (handle server errors like 404 or 500)
            throw new FetchError(response.status, response.statusText, response.body);
        }
    }
}
exports.MedicationExtractorApi = MedicationExtractorApi;
exports.default = MedicationExtractorApi;
//# sourceMappingURL=medicationExtractorApi.js.map