import Permission from "../Permission";
export var PermissionType;
(function (PermissionType) {
    PermissionType["Allow"] = "Allow";
    PermissionType["Block"] = "Block";
})(PermissionType || (PermissionType = {}));
export default class DatabaseCommonInteractionPropsUtil {
    static getUserPermissions(props, permissionType) {
        var _a;
        /*
         * Check first if the user has Global Permissions.
         * Global permissions includes all the tenantId user has access to.
         * and it includes all the global permissions that applies to all the tenant, like PUBLIC.
         */
        if (!props.userGlobalAccessPermission) {
            props.userGlobalAccessPermission = {
                globalPermissions: [Permission.Public],
                projectIds: [],
                _type: "UserGlobalAccessPermission",
            };
        }
        // If the PUBLIC Permission is not found in global permissions, include it.
        if (props.userGlobalAccessPermission &&
            !props.userGlobalAccessPermission.globalPermissions.includes(Permission.Public)) {
            props.userGlobalAccessPermission.globalPermissions.push(Permission.Public); // add public permission if not already.
        }
        // If the CurrentUser Permission is not found in global permissions, include it.
        if (props.userId &&
            props.userGlobalAccessPermission &&
            !props.userGlobalAccessPermission.globalPermissions.includes(Permission.CurrentUser)) {
            props.userGlobalAccessPermission.globalPermissions.push(Permission.CurrentUser);
        }
        let userPermissions = [];
        // Include global permission in userPermissions.
        if (props.userGlobalAccessPermission &&
            permissionType === PermissionType.Allow) {
            /// take global permissions.
            userPermissions = props.userGlobalAccessPermission.globalPermissions.map((permission) => {
                return {
                    permission: permission,
                    labelIds: [],
                    isBlockPermission: false,
                    _type: "UserPermission",
                };
            });
        }
        if (props.tenantId && props.userTenantAccessPermission) {
            // Include Tenant Permission in userPermissions.
            userPermissions = [
                ...userPermissions,
                ...(((_a = props.userTenantAccessPermission[props.tenantId.toString()]) === null || _a === void 0 ? void 0 : _a.permissions.filter((userPermission) => {
                    return (userPermission.isBlockPermission ===
                        (permissionType === PermissionType.Block));
                })) || []),
            ];
        }
        return userPermissions;
    }
}
//# sourceMappingURL=DatabaseCommonInteractionPropsUtil.js.map