"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntitlementApiImpl = void 0;
const ApiBase_1 = require("./ApiBase");
const EntitlementInternalApi_1 = require("../Internal/EntitlementInternalApi");
class EntitlementApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new EntitlementInternalApi_1.EntitlementInternalApi(_adaptable);
    }
    getEntitlementAccessLevelForModule(adaptableModule) {
        let entitlementOptions = this.getEntitlementOptions();
        if (entitlementOptions) {
            const defaultAccessLevelContext = this.getAdaptableInternalApi().buildBaseContext();
            const defaultAccessLevel = typeof entitlementOptions.defaultAccessLevel === 'function'
                ? entitlementOptions.defaultAccessLevel(defaultAccessLevelContext)
                : entitlementOptions.defaultAccessLevel;
            const moduleEntitlements = entitlementOptions.moduleEntitlements;
            if (moduleEntitlements) {
                if (typeof moduleEntitlements === 'function') {
                    const entitlementContext = {
                        adaptableModule,
                        ...this.getAdaptableInternalApi().buildBaseContext(),
                        defaultAccessLevel,
                    };
                    const dynamicModuleEntitlement = moduleEntitlements(entitlementContext);
                    return dynamicModuleEntitlement ?? defaultAccessLevel;
                }
                else {
                    const entitlement = moduleEntitlements.find((f) => f.adaptableModule === adaptableModule);
                    if (entitlement) {
                        return entitlement.accessLevel;
                    }
                }
            }
            // if still here then return their default Access Level
            return defaultAccessLevel;
        }
        return 'Full';
    }
    isObjectReadonly(object) {
        return object.IsReadOnly;
    }
    isModuleHiddenEntitlement(adaptableModule) {
        return this.getEntitlementAccessLevelForModule(adaptableModule) == 'Hidden';
    }
    isModuleFullEntitlement(adaptableModule) {
        return this.getEntitlementAccessLevelForModule(adaptableModule) == 'Full';
    }
    isModuleReadOnlyEntitlement(adaptableModule) {
        return this.getEntitlementAccessLevelForModule(adaptableModule) == 'ReadOnly';
    }
}
exports.EntitlementApiImpl = EntitlementApiImpl;
