"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractFetchJISResourceTask = void 0;
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const ResourceNotModified_1 = require("@golemio/core/dist/integration-engine/helpers/ResourceNotModified");
const JISMetadataRepository_1 = require("../../repositories/JISMetadataRepository");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
let AbstractFetchJISResourceTask = class AbstractFetchJISResourceTask extends integration_engine_1.AbstractEmptyTask {
    constructor(metadataRepository, logger, queuePrefix) {
        super(queuePrefix);
        this.metadataRepository = metadataRepository;
        this.logger = logger;
    }
    async execute() {
        try {
            const lastEtag = await this.metadataGetter();
            const dataSource = this.getDataSource(lastEtag);
            const { data, metadata } = await dataSource.getAllWithMetadata();
            if (data instanceof ResourceNotModified_1.ResourceNotModified) {
                this.logger.info(`${this.constructor.name}.execute: No update (304 Not Modified). ETag: ${lastEtag}`);
                return;
            }
            const responseEtag = this.getResponseEtag(metadata);
            if (typeof responseEtag === "string") {
                await this.metadataSetter(responseEtag);
            }
            else {
                this.logger.warn(`${this.constructor.name}.execute: Response has no ETag.`);
            }
            await this.sendMessageToExchange(data);
        }
        catch (err) {
            if (err instanceof golemio_errors_1.AbstractGolemioError) {
                throw err;
            }
            else {
                throw new golemio_errors_1.GeneralError(`Error while fetching JIS resource: ${this.queueRoutingKey}`, this.constructor.name, err);
            }
        }
    }
    getResponseEtag(metadata) {
        if (typeof metadata === "object" &&
            metadata !== null &&
            "headers" in metadata &&
            typeof metadata.headers === "object" &&
            metadata.headers !== null &&
            "etag" in metadata.headers &&
            typeof metadata.headers["etag"] === "string") {
            return metadata.headers["etag"];
        }
    }
};
exports.AbstractFetchJISResourceTask = AbstractFetchJISResourceTask;
exports.AbstractFetchJISResourceTask = AbstractFetchJISResourceTask = __decorate([
    __param(1, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)),
    __metadata("design:paramtypes", [JISMetadataRepository_1.JISMetadataRepository, Object, String])
], AbstractFetchJISResourceTask);
//# sourceMappingURL=AbstractFetchJISResourceTask.js.map