"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TripConnectionsRepository = 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 TripConnectionsRepository extends models_1.PostgresModel {
    constructor() {
        super("TripConnectionsRepository", {
            outputSequelizeAttributes: ropid_gtfs_1.RopidGTFS.tripConnections.outputSequelizeAttributes,
            pgTableName: ropid_gtfs_1.RopidGTFS.tripConnections.pgTableName,
            pgSchema: const_1.PG_SCHEMA,
            savingType: "insertOnly",
            attributesToRemove: ["id", "created_at", "updated_at"],
        }, new golemio_validator_1.JSONSchemaValidator("TripConnectionsRepositoryValidator", ropid_gtfs_1.RopidGTFS.tripConnections.outputJsonSchema));
        this.populate = async (sourceTableSuffix) => {
            const sql = `
            SELECT
                to_stop_id,
                to_trip_id,
                JSONB_AGG(
                    JSON_BUILD_OBJECT(
                        'from_trip_id', from_trip_id,
                        'max_wait_sec', max_waiting_time
                    )
                ) AS "connections"
            FROM "${const_1.PG_SCHEMA}".ropidgtfs_transfers${sourceTableSuffix}
            WHERE "transfer_type" = '1'
            GROUP BY "to_stop_id", "to_trip_id"
        `;
            try {
                const tmpTable = ropid_gtfs_1.RopidGTFS.tripConnections.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.TripConnectionsRepository = TripConnectionsRepository;
//# sourceMappingURL=TripConnectionsRepository.js.map