"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RopidGtfsMetadataRepository = exports.DatasetEnum = void 0;
const shared_1 = require("../shared");
const ropid_gtfs_1 = require("../../schema-definitions/ropid-gtfs");
const sequelize_1 = __importDefault(require("@golemio/core/dist/shared/sequelize"));
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
var DatasetEnum;
(function (DatasetEnum) {
    DatasetEnum["PID_GTFS"] = "PID_GTFS";
    DatasetEnum["CIS_STOPS"] = "CIS_STOPS";
    DatasetEnum["OIS_MAPPING"] = "OIS_MAPPING";
    DatasetEnum["RUN_NUMBERS"] = "RUN_NUMBERS";
    DatasetEnum["DEPARTURES_PRESETS"] = "DEPARTURES_PRESETS";
})(DatasetEnum || (exports.DatasetEnum = DatasetEnum = {}));
let RopidGtfsMetadataRepository = class RopidGtfsMetadataRepository extends shared_1.RopidMetadataModel {
    constructor() {
        super(ropid_gtfs_1.RopidGTFS);
        this.getAllSaved = async (dataset, version) => {
            const tables = await this.sequelizeModel.findAll({
                attributes: [["key", "tn"]],
                where: {
                    dataset,
                    type: shared_1.MetaTypeEnum.STATE,
                    value: shared_1.MetaStateEnum.SAVED,
                    version,
                },
            });
            return tables;
        };
        this.checkAllTablesHasSavedState = async (datasets) => {
            const notSaved = await this.sequelizeModel.count({
                where: {
                    [sequelize_1.default.Op.or]: datasets.map((item) => ({
                        dataset: item.dataset,
                        version: item.version,
                        type: shared_1.MetaTypeEnum.STATE,
                        value: { [sequelize_1.default.Op.ne]: shared_1.MetaStateEnum.SAVED },
                    })),
                },
            });
            return notSaved === 0;
        };
        this.checkIfNewVersionIsAlreadyDeployed = async (dataset, version) => {
            const alreadyDeployed = await this.sequelizeModel.count({
                where: {
                    dataset,
                    key: shared_1.MetaDatasetInfoKeyEnum.DEPLOYED,
                    type: shared_1.MetaTypeEnum.DATASET_INFO,
                    value: "true",
                    version,
                },
            });
            return alreadyDeployed !== 0;
        };
        this.updateState = async (dataset, name, state, version) => {
            return this.sequelizeModel.update({
                dataset,
                key: name,
                type: shared_1.MetaTypeEnum.STATE,
                value: state,
                version,
            }, {
                where: {
                    dataset,
                    key: name,
                    type: shared_1.MetaTypeEnum.STATE,
                    version,
                },
            });
        };
        this.getNumberOfDownloadRetries = async (dataset, version) => {
            const numberOfRetries = await this.sequelizeModel.findOne({
                attributes: [["value", "retries"]],
                where: {
                    dataset,
                    key: shared_1.MetaDatasetInfoKeyEnum.NUMBER_OF_RETRIES,
                    type: shared_1.MetaTypeEnum.DATASET_INFO,
                    version: version - 1,
                },
            });
            if (!numberOfRetries) {
                return 0;
            }
            return +numberOfRetries.dataValues.retries;
        };
        this.bulkCreate = async (data, options) => {
            await this.sequelizeModel.bulkCreate(data, options);
        };
        this.destroy = async (options) => {
            await this.sequelizeModel.destroy(options);
        };
    }
};
exports.RopidGtfsMetadataRepository = RopidGtfsMetadataRepository;
exports.RopidGtfsMetadataRepository = RopidGtfsMetadataRepository = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], RopidGtfsMetadataRepository);
//# sourceMappingURL=RopidGtfsMetadataRepository.js.map