/** @flow */
import type {Manager} from '../../../src/types'

function randomString() {
  return Math.random().toString(36).substring(7)
}

function range(from, to) {
  return Array.from(new Uint32Array(to - from + 1).map((val, index) => index + from))
}

export function createFixtureManager(manager: Manager) {

  let usersCount = 0

  function createUser() {
    return manager.getRepository('users')
      .create({
        login: 'user' + (++usersCount),
        password: randomString()
      })
      .save()
  }

  function createUsers(count: number = 1) {
    return Promise.all(range(0, count).map(createUser))
  }

  function createPostToUser(id: number) {
    return manager.getRepository('posts')
      .create({
        title: 'post ' + randomString(),
        body: randomString(),
        creator_id: id
      })
      .save()
  }

  async function createPost() {
    const user = await createUser()
    return createPostToUser(user.id)
  }

  function createPostsToUser(count: number = 1, id: number) {
    return Promise.all(range(0, count).map(createPostsToUser.bind(id)))
  }

  async function createPosts(userCount?: number = 1, postsPerUser?: number = 1) {
    const users = await createUsers(usersCount)
    return Promise.all(users.map(user => createPostsToUser(postsPerUser, user.id)))
  }

  function createPostComment(postId: number, userId: number) {
    return manager.getRepository('comments')
      .create({
        creator_id: userId,
        post_id: postId,
        body: randomString()
      })
      .save()
  }

  async function createPostCommentWithUser(postId: number) {
    const user = await createUser()
    return createPostComment(postId, user.id)
  }

  async function createPostWithComments(count: number = 1) {
    const post = await createPost()
    return Promise.all(range(0, count).map(createPostCommentWithUser.bind(post.id)))
  }

  return {
    createUser,
    createUsers,

    createPost,
    createPostsToUser,
    createPosts,

    createPostComment,
    createPostCommentWithUser,
    createPostWithComments
  }
}

@@ -157,16 +157,31 @@
 rom, to)
+: Array%3Cnumber%3E
  %7B%0A  ret
@@ -367,16 +367,33 @@
 teUser()
+: Promise%3CObject%3E
  %7B%0A    r
@@ -583,24 +583,48 @@
  number = 1)
+: Promise%3CArray%3CObject%3E%3E
  %7B%0A    retur
@@ -706,32 +706,49 @@
 User(id: number)
+: Promise%3CObject%3E
  %7B%0A    return ma
@@ -943,16 +943,33 @@
 tePost()
+: Promise%3CObject%3E
  %7B%0A    c
@@ -1092,32 +1092,56 @@
 = 1, id: number)
+: Promise%3CArray%3CObject%3E%3E
  %7B%0A    return Pr
@@ -1275,32 +1275,63 @@
 er?: number = 1)
+: Promise%3CArray%3CArray%3CObject%3E%3E%3E
  %7B%0A    const use
@@ -1510,24 +1510,41 @@
 rId: number)
+: Promise%3CObject%3E
  %7B%0A    retur
@@ -1763,16 +1763,33 @@
  number)
+: Promise%3CObject%3E
  %7B%0A    c
@@ -1928,16 +1928,40 @@
 ber = 1)
+: Promise%3CArray%3CObject%3E%3E
  %7B%0A    c

Fix for flow strange error