"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateLegacyCapabilitiesToFeatures = void 0;
const get_prop_1 = require("../../../objects/get-prop");
const set_prop_1 = require("../../../objects/set-prop");
const capabilityToFeatureMap_1 = require("./capabilityToFeatureMap");
/**
 * Site capabilities are currently saved as an array on the
 * site.data.values.capabilities. We want to migragte these
 * legacy capabilities over to features in the new permissions
 * system and expose them on the entity
 *
 * This function is called within fetchSite to ensure all
 * sites have features that are kept up-to-date with the
 * legacy capabilities array
 */
const migrateLegacyCapabilitiesToFeatures = (model) => {
    const legacyCapabilityFeatureFlags = {};
    let updatedFeatures = {};
    // 1. convert legacy capabilities to a feature flag hash
    ((0, get_prop_1.getProp)(model, "data.values.capabilities") || []).forEach((capability) => {
        legacyCapabilityFeatureFlags[capability] = true;
    });
    // 2. update/add features based on the legacy capabilities array
    const currentFeatures = (0, get_prop_1.getProp)(model, "data.settings.features") || {};
    updatedFeatures = capabilityToFeatureMap_1.capabilityToFeatureMap.reduce((features, map) => {
        // TODO: remove istanbul exception once we include a
        // legacy capability that satisfies the second condition
        const capabilityFlag = map.negate
            ? !legacyCapabilityFeatureFlags[map.capability]
            : /* istanbul ignore next */ legacyCapabilityFeatureFlags[map.capability];
        return Object.assign(Object.assign({}, features), { [map.feature]: capabilityFlag });
    }, currentFeatures);
    (0, set_prop_1.setProp)("data.settings.features", updatedFeatures, model);
    return model;
};
exports.migrateLegacyCapabilitiesToFeatures = migrateLegacyCapabilitiesToFeatures;
//# sourceMappingURL=migrateLegacyCapabilitiesToFeatures.js.map