import { addCallback } from 'meteor/vulcan:core';
import { Posts } from '../../lib/collections/posts';
import { Comments } from '../../lib/collections/comments'
import Users from 'meteor/vulcan:users';
import Sequences from '../../lib/collections/sequences/collection.js';
import { algoliaDocumentExport } from './utils.js';

function newCommentAlgoliaIndex(comment) {
  algoliaDocumentExport({
    documents: [comment],
    collection: Comments,
    indexName: 'test_comments',
    exportFunction: Comments.toAlgolia,
    updateFunction: (comment) => Comments.update(comment._id, {$set: {algoliaIndexAt: new Date()}})
  })
}
addCallback("comments.new.async", newCommentAlgoliaIndex)

function editCommentAlgoliaIndex(comment) {
  algoliaDocumentExport({
    documents: [comment],
    collection: Comments,
    indexName: 'test_comments',
    exportFunction: Comments.toAlgolia,
    updateFunction: (comment) => Comments.update(comment._id, {$set: {algoliaIndexAt: new Date()}})
  })
}
addCallback("comments.edit.async", editCommentAlgoliaIndex)

function newPostAlgoliaIndex(post) {
  if (!post.draft) {
    algoliaDocumentExport({
      documents: [post],
      collection: Posts,
      indexName: 'test_posts',
      exportFunction: Posts.toAlgolia,
      updateFunction: (post) => Posts.update(post._id, {$set: {algoliaIndexAt: new Date()}})
    })
  }
}
addCallback("posts.new.async", newPostAlgoliaIndex)

function editPostAlgoliaIndex(post) {
  if (!post.draft) {
    algoliaDocumentExport({
      documents: [post],
      collection: Posts,
      indexName: 'test_posts',
      exportFunction: Posts.toAlgolia,
      updateFunction: (post) => Posts.update(post._id, {$set: {algoliaIndexAt: new Date()}})
    })
  }
}
addCallback("posts.edit.async", editPostAlgoliaIndex)

function newUserAlgoliaIndex(user) {
  algoliaDocumentExport({
    documents: [user],
    collection: Users,
    indexName: 'test_users',
    exportFunction: Users.toAlgolia,
    updateFunction: (user) => Users.update(user._id, {$set: {algoliaIndexAt: new Date()}})
  })
}
addCallback("users.new.async", newUserAlgoliaIndex)

function editUserAlgoliaIndex(user) {
  algoliaDocumentExport({
    documents: [user],
    collection: Users,
    indexName: 'test_users',
    exportFunction: Users.toAlgolia,
    updateFunction: (user) => Users.update(user._id, {$set: {algoliaIndexAt: new Date()}})
  })
}
addCallback("users.edit.async", editUserAlgoliaIndex)

function newSequenceAlgoliaIndex(sequence) {
  algoliaDocumentExport({
    documents: [sequence],
    collection: Sequences,
    indexName: 'test_sequences',
    exportFunction: Sequences.toAlgolia,
    updateFunction: (sequence) => Sequences.update(sequence._id, {$set: {algoliaIndexAt: new Date()}})
  })
}
addCallback("sequences.new.async", newSequenceAlgoliaIndex)

function editSequenceAlgoliaIndex(sequence) {
  algoliaDocumentExport({
    documents: [sequence],
    collection: Sequences,
    indexName: 'test_sequences',
    exportFunction: Sequences.toAlgolia,
    updateFunction: (sequence) => Sequences.update(sequence._id, {$set: {algoliaIndexAt: new Date()}})
  })
}
addCallback("sequences.edit.async", editSequenceAlgoliaIndex);

@@ -319,16 +319,175 @@
 s.js';%0A%0A
+const commentIndexName = 'test_comments';%0Aconst postIndexName = 'test_posts';%0Aconst userIndexName = 'test_users';%0Aconst sequenceIndexName = 'test_sequences';%0A%0A
 function
@@ -610,39 +610,40 @@
  indexName: 
-'test_
 comment
-s'
+IndexName
 ,%0A    export
@@ -975,31 +975,32 @@
 exName: 
-'test_
 comment
-s'
+IndexName
 ,%0A    ex
@@ -1350,36 +1350,37 @@
  indexName: 
-'test_posts'
+postIndexName
 ,%0A      expo
@@ -1721,28 +1721,29 @@
 exName: 
-'test_posts'
+postIndexName
 ,%0A      
@@ -2056,36 +2056,37 @@
  indexName: 
-'test_users'
+userIndexName
 ,%0A    export
@@ -2388,28 +2388,29 @@
 exName: 
-'test_users'
+userIndexName
 ,%0A    ex
@@ -2729,38 +2729,32 @@
  indexName: 
-'test_
 sequence
 s',%0A    expo
@@ -2733,34 +2733,41 @@
 exName: sequence
-s'
+IndexName
 ,%0A    exportFunc
@@ -3105,30 +3105,24 @@
 exName: 
-'test_
 sequence
 s',%0A    
@@ -3113,18 +3113,25 @@
 sequence
-s'
+IndexName
 ,%0A    ex

Factor out Algolia index names