"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.JISInfotextsDataService = void 0;
const JISContainerToken_1 = require("../ioc/JISContainerToken");
const JISInfotextsRepository_1 = require("../repositories/JISInfotextsRepository");
const JISInfotextsRopidGTFSStopsRepository_1 = require("../repositories/JISInfotextsRopidGTFSStopsRepository");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let JISInfotextsDataService = class JISInfotextsDataService {
    constructor(infotextsRepository, infotextsRopidGTFSStopsRepository, databaseConnector, logger) {
        this.infotextsRepository = infotextsRepository;
        this.infotextsRopidGTFSStopsRepository = infotextsRopidGTFSStopsRepository;
        this.databaseConnector = databaseConnector;
        this.logger = logger;
        // do nothing
    }
    /**
     * Insert or update all given infotexts and infotextsRopidGTFSStops data. In case of an update, all attributes except for
     * `created_at` shall be overwritten. Delete all non-updated data from before the refresh.
     *
     * @param infotexts The infotexts data to be upserted
     * @param infotextsRopidGTFSStops The infotextsRopidGTFSStops data to be upserted
     */
    async refreshData(infotexts, infotextsRopidGTFSStops) {
        const connection = this.databaseConnector.getConnection();
        const transaction = await connection.transaction();
        const saveTime = new Date();
        try {
            if (infotexts.length === 0) {
                await this.infotextsRopidGTFSStopsRepository.deleteAll({ transaction });
                await this.infotextsRepository.deleteAll({ transaction });
                await transaction.commit();
                this.logger.info(`${this.constructor.name}.refreshData: all JIS infotexts were deleted because 0 infotexts were imported`);
            }
            else {
                await this.infotextsRepository.refreshData(infotexts, { transaction });
                await this.infotextsRopidGTFSStopsRepository.upsertAll(infotextsRopidGTFSStops, { transaction });
                await this.infotextsRopidGTFSStopsRepository.deleteAllLastUpdatedBefore(saveTime, { transaction });
                await transaction.commit();
                this.logger.info(`${this.constructor.name}.refreshData: ${infotexts.length} JIS infotexts and ` +
                    `${infotextsRopidGTFSStops.length} JIS infotextsRopidGTFSStops were saved`);
            }
        }
        catch (err) {
            await transaction.rollback();
            if (err instanceof golemio_errors_1.GeneralError)
                throw err;
            throw new golemio_errors_1.GeneralError("Refreshing JIS Infotexts in database failed", this.constructor.name, err);
        }
    }
};
exports.JISInfotextsDataService = JISInfotextsDataService;
exports.JISInfotextsDataService = JISInfotextsDataService = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(JISContainerToken_1.JISContainerToken.JISInfotextsRepository)),
    __param(1, (0, tsyringe_1.inject)(JISContainerToken_1.JISContainerToken.JISInfotextsRopidGTFSStopsRepository)),
    __param(2, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.PostgresConnector)),
    __param(3, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)),
    __metadata("design:paramtypes", [JISInfotextsRepository_1.JISInfotextsRepository,
        JISInfotextsRopidGTFSStopsRepository_1.JISInfotextsRopidGTFSStopsRepository, Object, Object])
], JISInfotextsDataService);
//# sourceMappingURL=JISInfotextsDataService.js.map