"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MinMaxStopSequencesRepository = void 0;
const const_1 = require("../../../../schema-definitions/const");
const ropid_gtfs_1 = require("../../../../schema-definitions/ropid-gtfs");
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const models_1 = require("@golemio/core/dist/integration-engine/models");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const SourceTableSuffixEnum_1 = require("../../helpers/SourceTableSuffixEnum");
class MinMaxStopSequencesRepository extends models_1.PostgresModel {
    constructor() {
        super("MinMaxStopSequencesRepository", {
            outputSequelizeAttributes: ropid_gtfs_1.RopidGTFS.minMaxStopSequences.outputSequelizeAttributes,
            pgTableName: ropid_gtfs_1.RopidGTFS.minMaxStopSequences.pgTableName,
            pgSchema: const_1.PG_SCHEMA,
            savingType: "insertOnly",
            attributesToRemove: ["id", "created_at", "updated_at"],
        }, new golemio_validator_1.JSONSchemaValidator("MinMaxStopSequencesRepositoryValidator", ropid_gtfs_1.RopidGTFS.minMaxStopSequences.outputJsonSchema));
        this.populate = async (sourceTableSuffix) => {
            const sql = `
            SELECT
                trip_id,
                max(stop_sequence) AS max_stop_sequence,
                min(stop_sequence) AS min_stop_sequence,
                GREATEST(
                    max(arrival_time::interval),
                    max(departure_time::interval)
                ) AS max_stop_time,
                LEAST(
                    min(arrival_time::interval),
                    min(departure_time::interval)
                ) AS min_stop_time
            FROM "${const_1.PG_SCHEMA}".ropidgtfs_stop_times${sourceTableSuffix}
            GROUP BY trip_id
        `;
            try {
                const tmpTable = ropid_gtfs_1.RopidGTFS.minMaxStopSequences.pgTableName + SourceTableSuffixEnum_1.SourceTableSuffixEnum.Tmp;
                await this.sequelizeModel.sequelize.query(`INSERT INTO "${const_1.PG_SCHEMA}"."${tmpTable}" ${sql};`);
            }
            catch (err) {
                helpers_1.log.error(err);
                throw err;
            }
        };
    }
}
exports.MinMaxStopSequencesRepository = MinMaxStopSequencesRepository;
//# sourceMappingURL=MinMaxStopSequencesRepository.js.map