"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const ropid_vymi_1 = require("../../../../../schema-definitions/ropid-vymi");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const config_1 = require("@golemio/core/dist/integration-engine/config");
const HTTPFetchProtocolStrategy_1 = require("@golemio/core/dist/integration-engine/datasources/protocol-strategy/HTTPFetchProtocolStrategy");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const crypto_1 = __importDefault(require("crypto"));
const RopidVYMIApiOutputLevel_1 = require("./RopidVYMIApiOutputLevel");
class RopidVYMIApiHelper {
    constructor() {
        this.getAllEvents = async () => {
            try {
                const datasource = this.createDatasource();
                const events = await datasource.getAll();
                const digest = this.createDigest(JSON.stringify(events));
                return {
                    data: events,
                    digest,
                };
            }
            catch (err) {
                integration_engine_1.log.error(err);
                if (err instanceof golemio_errors_1.AbstractGolemioError) {
                    throw err;
                }
                else {
                    throw new golemio_errors_1.GeneralError("Error while getting data", this.constructor.name, err);
                }
            }
        };
        this.createDigest = (data) => {
            return crypto_1.default.createHash("sha1").update(data).digest("hex");
        };
    }
    createDatasource() {
        const createdFrom = this.computeCreatedFrom();
        return new integration_engine_1.DataSource(ropid_vymi_1.RopidVYMI.events.name + "DataSource", new HTTPFetchProtocolStrategy_1.HTTPFetchProtocolStrategy({
            headers: config_1.config.datasources.RopidVYMIApiHeaders,
            method: "GET",
            timeoutInSeconds: 20,
            url: `${config_1.config.datasources.RopidVYMIApiUrl}` +
                `?level=${RopidVYMIApiOutputLevel_1.RopidVYMIApiOutputLevel.Standard}` +
                `&status=1` +
                `&recordtype=1` +
                `&createdfrom=${createdFrom}`,
        }), new integration_engine_1.JSONDataTypeStrategy({ resultsPath: "vymi-report.vymi-list" }), new golemio_validator_1.JSONSchemaValidator(ropid_vymi_1.RopidVYMI.events.name + "DataSource", ropid_vymi_1.RopidVYMI.events.datasourceJsonSchema));
    }
    computeCreatedFrom() {
        const d = new Date();
        d.setDate(d.getDate() - 100);
        const pad = (n) => String(n).padStart(2, "0");
        // Format: YYYYMMDDHHMMSS
        return (d.getFullYear().toString() +
            pad(d.getMonth() + 1) +
            pad(d.getDate()) +
            pad(d.getHours()) +
            pad(d.getMinutes()) +
            pad(d.getSeconds()));
    }
}
_a = RopidVYMIApiHelper;
RopidVYMIApiHelper.getInstance = () => {
    if (!_a._instance) {
        _a._instance = new _a();
    }
    return _a._instance;
};
exports.default = RopidVYMIApiHelper;
//# sourceMappingURL=RopidVYMIApiHelper.js.map