import { getProp } from "../../../objects/get-prop";
import { capabilityToFeatureMap } from "./capabilityToFeatureMap";
import { setProp } from "../../../objects/set-prop";
/**
 * 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; however, we must continue persisting updates to
 * these features in the legacy capabilities array until the
 * existing site capabilities in our application are plumbed
 * to work off of permissions
 *
 * This function is called within updateSite to ensure
 * the legacy capabilities array is kept up-to-date
 *
 * TODO: Remove once site capabilities use permissions
 *
 * @param modelToUpdate
 * @param currentModel
 */
export const convertFeaturesToLegacyCapabilities = (modelToUpdate, currentModel) => {
    let legacyCapabilityFeatureFlags = {};
    // 1. convert legacy capabilities to a feature flag hash
    (getProp(currentModel, "data.values.capabilities") || []).forEach((capability) => {
        legacyCapabilityFeatureFlags[capability] = true;
    });
    // 2. override legacy capabilities that are driven by features
    const features = getProp(modelToUpdate, "data.settings.features") || {};
    legacyCapabilityFeatureFlags = capabilityToFeatureMap.reduce((capabilities, map) => {
        // TODO: remove istanbul exception once we include a
        // legacy capability that satisfies the second condition
        const featureFlag = map.negate
            ? !features[map.feature]
            : /* istanbul ignore next */ features[map.feature];
        return Object.assign(Object.assign({}, capabilities), { [map.capability]: featureFlag });
    }, legacyCapabilityFeatureFlags);
    // 3. convert legacy capabilities back to an array and persist on model
    const updatedCapabilities = Object.entries(legacyCapabilityFeatureFlags).reduce((acc, [key, value]) => {
        // if you are updating this file, remove this comment and address the lint error
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        value && acc.push(key);
        return acc;
    }, []);
    setProp("data.values.capabilities", updatedCapabilities, modelToUpdate);
    return modelToUpdate;
};
//# sourceMappingURL=convertFeaturesToLegacyCapabilities.js.map