"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.MetroRailtrackGPSRepository = void 0;
const const_1 = require("../../../../../../schema-definitions/const");
const MetroRailtrackGPSModel_1 = require("../../../../../../schema-definitions/ropid-gtfs/models/railtrack/MetroRailtrackGPSModel");
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const models_1 = require("@golemio/core/dist/integration-engine/models");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const sequelize_1 = __importDefault(require("@golemio/core/dist/shared/sequelize"));
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let MetroRailtrackGPSRepository = class MetroRailtrackGPSRepository extends models_1.PostgresModel {
    constructor() {
        super("MetroRailtrackGPSRepository", {
            pgTableName: MetroRailtrackGPSModel_1.MetroRailtrackGPSModel.TABLE_NAME,
            pgSchema: const_1.PG_SCHEMA,
            outputSequelizeAttributes: MetroRailtrackGPSModel_1.MetroRailtrackGPSModel.attributeModel,
            savingType: "insertOnly",
        }, new golemio_validator_1.JSONSchemaValidator("MetroRailtrackGPSRepository", MetroRailtrackGPSModel_1.MetroRailtrackGPSModel.arrayJsonSchema));
        this.findCoordinates = async (routeName, trackIds) => {
            try {
                return await this.sequelizeModel.findOne({
                    attributes: [
                        [
                            sequelize_1.default.fn("json_object_agg", sequelize_1.default.col("track_id"), sequelize_1.default.fn("json_build_object", "lon", sequelize_1.default.fn("ST_X", sequelize_1.default.col("coordinates")), "lat", sequelize_1.default.fn("ST_Y", sequelize_1.default.col("coordinates")), "gtfs_stop_id", sequelize_1.default.col("gtfs_stop_id"))),
                            "coordinates",
                        ],
                    ],
                    where: {
                        route_name: routeName,
                        track_id: {
                            [sequelize_1.default.Op.in]: trackIds,
                        },
                    },
                    raw: true,
                    plain: true,
                });
            }
            catch (err) {
                const exception = new golemio_errors_1.GeneralError(`[${this.constructor.name}] Could not find data: ${err.message}`, this.constructor.name, err);
                helpers_1.log.error(exception);
                throw exception;
            }
        };
        this.saveData = async (data, shouldReplace = true, isReturning = false) => {
            let transaction;
            try {
                await this.validate(data);
                transaction = await this.sequelizeModel.sequelize?.transaction();
                if (shouldReplace) {
                    await this.sequelizeModel.destroy({ where: {}, transaction });
                }
                const instances = await this.sequelizeModel.bulkCreate(data, {
                    ignoreDuplicates: true,
                    returning: isReturning,
                    transaction,
                });
                await transaction?.commit();
                return instances;
            }
            catch (err) {
                const exception = new golemio_errors_1.GeneralError(`[${this.constructor.name}] Could not save data: ${err.message}`, this.constructor.name, err);
                helpers_1.log.error(exception);
                await transaction?.rollback();
                throw exception;
            }
        };
    }
};
exports.MetroRailtrackGPSRepository = MetroRailtrackGPSRepository;
exports.MetroRailtrackGPSRepository = MetroRailtrackGPSRepository = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], MetroRailtrackGPSRepository);
//# sourceMappingURL=MetroRailtrackGPSRepository.js.map