"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDocumentInfo = void 0;
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const DOCUMO_API_BASE = 'https://api.documo.com';
const SettingsSchema = zod_1.z.object({
    apiKey: zod_1.z.string().min(1),
});
exports.getDocumentInfo = {
    key: 'getDocumentInfo',
    title: 'Get Document Info',
    description: 'Retrieve metadata about a specific document in a Documo workspace.',
    category: extensions_core_1.Category.DOCUMENT_MANAGEMENT,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        const { fields: { workspaceId, documentId }, settings: { apiKey }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsSchema,
                settings: SettingsSchema,
            }),
            payload,
        });
        const url = `${DOCUMO_API_BASE}/ws/v2/workspaces/${encodeURIComponent(workspaceId)}/documents/${encodeURIComponent(documentId)}`;
        const response = await fetch(url, {
            method: 'GET',
            headers: {
                Authorization: apiKey,
                'Content-Type': 'application/json',
            },
        });
        if (!response.ok) {
            const errorBody = await response.text();
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: `Documo API error (${response.status}): ${errorBody}`,
                        },
                        error: {
                            category: 'SERVER_ERROR',
                            message: `Documo API returned ${response.status}: ${errorBody}`,
                        },
                    },
                ],
            });
            return;
        }
        const data = await response.json();
        await onComplete({
            data_points: {
                id: data.id,
                name: data.name,
                workspaceId: data.workspaceId,
                createdAt: data.createdAt,
                addedAt: data.addedAt,
                isUploading: String(data.isUploading),
                pagesCount: data.pagesCount != null ? String(data.pagesCount) : '',
                from: (_a = data.from) !== null && _a !== void 0 ? _a : '',
                to: (_b = data.to) !== null && _b !== void 0 ? _b : '',
                sourceType: data.sourceType,
                sourceId: (_c = data.sourceId) !== null && _c !== void 0 ? _c : '',
                statusName: (_e = (_d = data.status) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : '',
                typeName: (_g = (_f = data.type) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '',
                workspaceName: (_j = (_h = data.workspace) === null || _h === void 0 ? void 0 : _h.name) !== null && _j !== void 0 ? _j : '',
                documentResponse: JSON.stringify(data),
            },
        });
    },
};
//# sourceMappingURL=getDocumentInfo.js.map