"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 SavedFiltersRepository_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SavedFiltersRepository = void 0;
const common_1 = require("@nestjs/common");
const model_metadata_entity_1 = require("../entities/model-metadata.entity");
const saved_filters_entity_1 = require("../entities/saved-filters.entity");
const view_metadata_entity_1 = require("../entities/view-metadata.entity");
const typeorm_1 = require("typeorm");
const solid_base_repository_1 = require("./solid-base.repository");
let SavedFiltersRepository = SavedFiltersRepository_1 = class SavedFiltersRepository extends solid_base_repository_1.SolidBaseRepository {
    constructor(dataSource) {
        super(saved_filters_entity_1.SavedFilters, dataSource, null, null);
        this.dataSource = dataSource;
        this.logger = new common_1.Logger(SavedFiltersRepository_1.name);
    }
    async upsertWithDto(dto) {
        const modelRepo = this.dataSource.getRepository(model_metadata_entity_1.ModelMetadata);
        const viewRepo = this.dataSource.getRepository(view_metadata_entity_1.ViewMetadata);
        const [modelEntity, viewEntity] = await Promise.all([
            modelRepo.findOne({ where: { singularName: dto.modelUserKey } }),
            viewRepo.findOne({ where: { name: dto.viewUserKey } }),
        ]);
        if (!modelEntity || !viewEntity) {
            throw new Error(`Missing related entity for SavedFilter: model=${dto.modelUserKey}, view=${dto.viewUserKey}`);
        }
        const filterData = {
            ...dto,
            isPrivate: false,
            user: null,
            model: modelEntity,
            view: viewEntity,
        };
        const existing = await this.findOne({
            where: { name: dto.name },
        });
        if (existing) {
            const merged = this.merge(existing, filterData);
            this.logger.debug(`Updating saved filter: ${dto.name}`);
            return this.save(merged);
        }
        else {
            this.logger.debug(`Creating saved filter: ${dto.name}`);
            return this.save(this.create(filterData));
        }
    }
};
exports.SavedFiltersRepository = SavedFiltersRepository;
exports.SavedFiltersRepository = SavedFiltersRepository = SavedFiltersRepository_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [typeorm_1.DataSource])
], SavedFiltersRepository);
//# sourceMappingURL=saved-filters.repository.js.map