"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptableHelper = exports.isAdaptableObject = exports.removeUuidAndSource = exports.addUuidAndSource = exports.getAccessLevelForObject = exports.initPredefinedConfigWithUuids = void 0;
const tslib_1 = require("tslib");
const cloneDeepWith_1 = tslib_1.__importDefault(require("lodash/cloneDeepWith"));
const isPlainObject_1 = tslib_1.__importDefault(require("lodash/isPlainObject"));
const Uuid_1 = require("../../PredefinedConfig/Uuid");
function initPredefinedConfigWithUuids(predefinedConfig) {
    const customizer = (value) => {
        // so whenever we clone a plain object,
        // we add a Uuid property
        // jw added 9/8/20: unless there is on there already
        if ((0, isPlainObject_1.default)(value) && value != predefinedConfig) {
            if (value.Uuid == null || value.Uuid == undefined) {
                value.Uuid = (0, Uuid_1.createUuid)();
            }
        }
    };
    return (0, cloneDeepWith_1.default)(predefinedConfig, customizer);
}
exports.initPredefinedConfigWithUuids = initPredefinedConfigWithUuids;
function getAccessLevelForObject(adaptableObject, moduleAccessLevel) {
    // we always return the Module Access Level unless the following condition is met:
    // the object prpoerty has been set and set to true and the Module entitlement is full
    if (moduleAccessLevel == 'Full' &&
        adaptableObject &&
        adaptableObject.IsReadOnly &&
        adaptableObject.IsReadOnly == true) {
        return 'ReadOnly';
    }
    return moduleAccessLevel;
}
exports.getAccessLevelForObject = getAccessLevelForObject;
function addUuidAndSource(adaptableObject) {
    if (!adaptableObject.Uuid) {
        adaptableObject.Uuid = (0, Uuid_1.createUuid)();
    }
    adaptableObject.Source = 'User';
    return adaptableObject;
}
exports.addUuidAndSource = addUuidAndSource;
function removeUuidAndSource(adaptableObject) {
    const clonedObject = structuredClone(adaptableObject);
    const sanitiseObject = (object) => {
        for (const key in object) {
            if (key === 'Source' || key === 'Uuid') {
                delete object[key];
            }
            else if (Array.isArray(object[key])) {
                object[key].forEach((item) => sanitiseObject(item));
            }
            else if (typeof object[key] === 'object') {
                sanitiseObject(object[key]);
            }
        }
    };
    sanitiseObject(clonedObject);
    return clonedObject;
}
exports.removeUuidAndSource = removeUuidAndSource;
// pretty primitive type guard for AdaptableObject, but currently there is no better/safer way
function isAdaptableObject(object) {
    return object != undefined && typeof object['Uuid'] === 'string';
}
exports.isAdaptableObject = isAdaptableObject;
exports.AdaptableHelper = {
    initPredefinedConfigWithUuids,
    getAccessLevelForObject,
    addUuidAndSource,
    removeUuidAndSource,
    isAdaptableObject,
};
exports.default = exports.AdaptableHelper;
