"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const integration_base_1 = require("@things-factory/integration-base");
const utils_1 = require("@things-factory/utils");
const env_1 = require("@things-factory/env");
const shell_1 = require("@things-factory/shell");
const equipment_analysis_1 = require("../../service/equipment-analysis/equipment-analysis");
const equipment_report_data_1 = require("./equipment-report-data");
const STEDCONFIG = env_1.config.get('sted', {});
/*
TODO: 모듈 이름과 파라미터 이름에 대한 자동화를 어떻게 헤야할지??

*/
async function EquipmentReport(step, context) {
    var _a, _b;
    var { params: { equipmentAnalysisAccessor } } = step;
    if (!equipmentAnalysisAccessor) {
        throw new Error('equipment-analysis-accessor is not defined');
    }
    var { data } = context;
    const equipmentAnalysisVariables = (0, utils_1.access)(equipmentAnalysisAccessor, data);
    const equipmentAnalysisVariablesKeyExists = ['equipmentState', 'processId'].every(key => key in equipmentAnalysisVariables);
    if (!equipmentAnalysisVariables || !equipmentAnalysisVariablesKeyExists) {
        throw new Error('equipment-analysis-variables is invalid');
    }
    const equipmentAnalysis = await (0, shell_1.getRepository)(equipment_analysis_1.EquipmentAnalysis).findOne({
        where: {
            type: (_a = equipmentAnalysisVariables.equipmentState) === null || _a === void 0 ? void 0 : _a.type,
            name: (_b = equipmentAnalysisVariables.equipmentState) === null || _b === void 0 ? void 0 : _b.refId,
            processId: equipmentAnalysisVariables.processId
        }
    });
    if (!equipmentAnalysis) {
        throw new Error('equipment-analysis not found');
    }
    let reportInput = {
        equipmentType: equipmentAnalysisVariables === null || equipmentAnalysisVariables === void 0 ? void 0 : equipmentAnalysisVariables.equipmentState.type,
        equipmentName: typeof (equipmentAnalysisVariables === null || equipmentAnalysisVariables === void 0 ? void 0 : equipmentAnalysisVariables.equipmentState.refid) !== 'string'
            ? equipmentAnalysisVariables === null || equipmentAnalysisVariables === void 0 ? void 0 : equipmentAnalysisVariables.equipmentState.refid.toString()
            : equipmentAnalysisVariables === null || equipmentAnalysisVariables === void 0 ? void 0 : equipmentAnalysisVariables.equipmentState.refid,
        equipmentInput: equipmentAnalysis.input,
        equipmentOutput: equipmentAnalysis.output
    };
    /*
    TODO: 레포트 입력 데이터를 기존 stedNodes 데이터를 구체화함으로서, 레포트의 질을 높이는 작업이 필요.
        
    */
    // translate equipment keys
    try {
        reportInput = (0, equipment_report_data_1.translateEquipmentData)(reportInput, reportInput.equipmentType);
    }
    catch (e) {
        return {
            data: e.message
        };
    }
    const response = await fetch(`${STEDCONFIG.url}/equipment-report`, {
        method: 'POST',
        headers: {
            Accept: '*/*',
            'Content-Type': 'application/json',
            output: 'keep-alive'
        },
        body: JSON.stringify(reportInput)
    });
    const equipmentResult = await response.json();
    return {
        data: equipmentResult === null || equipmentResult === void 0 ? void 0 : equipmentResult.result
    };
}
EquipmentReport.parameterSpec = [
    {
        type: 'string',
        name: 'equipmentAnalysisAccessor',
        label: 'equipment-analysis-accessor'
    }
];
EquipmentReport.help = 'integration/task/process-analysis';
EquipmentReport.connectorFree = true;
integration_base_1.TaskRegistry.registerTaskHandler('equipment-report', EquipmentReport);
//# sourceMappingURL=equipment-report.js.map