import DatabaseService from "./DatabaseService";
import Model from "../../Models/DatabaseModels/MetricSavedView";
import QueryHelper from "../Types/Database/QueryHelper";
import LIMIT_MAX from "../../Types/Database/LimitMax";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
    }
    async onBeforeCreate(createBy) {
        if (createBy.data.isDefault === undefined && createBy.data.projectId) {
            const existingDefaultView = await this.findOneBy({
                query: {
                    projectId: createBy.data.projectId,
                    isDefault: true,
                },
                select: {
                    _id: true,
                },
                props: {
                    isRoot: true,
                },
            });
            createBy.data.isDefault = !existingDefaultView;
        }
        if (createBy.data.projectId) {
            await this.unsetOtherDefaultsIfNeeded({
                projectId: createBy.data.projectId,
                isDefault: createBy.data.isDefault || false,
            });
        }
        return { createBy, carryForward: null };
    }
    async onBeforeUpdate(updateBy) {
        if (updateBy.data.isDefault !== true) {
            return { updateBy, carryForward: null };
        }
        const itemsToUpdate = await this.findBy({
            query: updateBy.query,
            select: {
                _id: true,
                projectId: true,
            },
            props: {
                isRoot: true,
            },
            limit: LIMIT_MAX,
            skip: 0,
        });
        for (const item of itemsToUpdate) {
            if (item.projectId) {
                await this.unsetOtherDefaultsIfNeeded({
                    projectId: item.projectId,
                    isDefault: true,
                    excludeIds: item._id ? [item._id] : [],
                });
            }
        }
        return { updateBy, carryForward: null };
    }
    async unsetOtherDefaultsIfNeeded(data) {
        if (!data.projectId || !data.isDefault) {
            return;
        }
        await this.updateBy({
            query: Object.assign({ projectId: data.projectId, isDefault: true }, (data.excludeIds && data.excludeIds.length > 0
                ? {
                    _id: QueryHelper.notInOrNull(data.excludeIds),
                }
                : {})),
            data: {
                isDefault: false,
            },
            props: {
                isRoot: true,
            },
            limit: LIMIT_MAX,
            skip: 0,
        });
    }
}
export default new Service();
//# sourceMappingURL=MetricSavedViewService.js.map