"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../types");
const context_schema_1 = require("./context-schema");
const error_1 = require("../error");
const feature_schema_1 = require("../schema/feature-schema");
class ContextService {
    constructor({ projectStore, contextFieldStore, featureStrategiesStore, }, { getLogger, flagResolver, }, eventService, privateProjectChecker) {
        this.privateProjectChecker = privateProjectChecker;
        this.projectStore = projectStore;
        this.eventService = eventService;
        this.flagResolver = flagResolver;
        this.contextFieldStore = contextFieldStore;
        this.featureStrategiesStore = featureStrategiesStore;
        this.logger = getLogger('services/context-service.js');
    }
    async getAll() {
        return this.contextFieldStore.getAll();
    }
    async getContextField(name) {
        return this.contextFieldStore.get(name);
    }
    async getStrategiesByContextField(name, userId) {
        const strategies = await this.featureStrategiesStore.getStrategiesByContextField(name);
        const accessibleProjects = await this.privateProjectChecker.getUserAccessibleProjects(userId);
        if (accessibleProjects.mode === 'all') {
            return this.mapStrategies(strategies);
        }
        else {
            return this.mapStrategies(strategies.filter((strategy) => accessibleProjects.projects.includes(strategy.projectId)));
        }
    }
    mapStrategies(strategies) {
        return {
            strategies: strategies.map((strategy) => ({
                id: strategy.id,
                projectId: strategy.projectId,
                featureName: strategy.featureName,
                strategyName: strategy.strategyName,
                environment: strategy.environment,
            })),
        };
    }
    async createContextField(value, auditUser) {
        // validations
        await this.validateUniqueName(value);
        const contextField = await context_schema_1.contextSchema.validateAsync(value);
        // creations
        const createdField = await this.contextFieldStore.create(value);
        await this.eventService.storeEvent({
            type: types_1.CONTEXT_FIELD_CREATED,
            createdBy: auditUser.username,
            createdByUserId: auditUser.id,
            ip: auditUser.ip,
            data: contextField,
        });
        return createdField;
    }
    async updateContextField(updatedContextField, auditUser) {
        const contextField = await this.contextFieldStore.get(updatedContextField.name);
        const value = await context_schema_1.contextSchema.validateAsync(updatedContextField);
        // update
        await this.contextFieldStore.update(value);
        const { createdAt, sortOrder, ...previousContextField } = contextField;
        await this.eventService.storeEvent({
            type: types_1.CONTEXT_FIELD_UPDATED,
            createdBy: auditUser.username,
            createdByUserId: auditUser.id,
            ip: auditUser.ip,
            preData: previousContextField,
            data: value,
        });
    }
    async deleteContextField(name, auditUser) {
        const contextField = await this.contextFieldStore.get(name);
        // delete
        await this.contextFieldStore.delete(name);
        await this.eventService.storeEvent({
            type: types_1.CONTEXT_FIELD_DELETED,
            createdBy: auditUser.username,
            createdByUserId: auditUser.id,
            ip: auditUser.ip,
            preData: contextField,
        });
    }
    async validateUniqueName({ name, }) {
        let msg;
        try {
            await this.contextFieldStore.get(name);
            msg = 'A context field with that name already exist';
        }
        catch (error) {
            // No conflict, everything ok!
            return;
        }
        // Intentional throw here!
        throw new error_1.NameExistsError(msg);
    }
    async validateName(name) {
        await feature_schema_1.nameSchema.validateAsync({ name });
        await this.validateUniqueName({ name });
    }
}
exports.default = ContextService;
module.exports = ContextService;
//# sourceMappingURL=context-service.js.map