<commit_before>/* global _ */

Template.editor.helpers({
  owner: function () {
    return (Meteor.userId() && (Meteor.userId() === Session.get('gistOwnerId')))
  },

  fileId: function () {
    return Session.get('fileId')
  },
  filename: function () {
    return Files.findOne({ _id: Session.get('fileId') }).filename
  },
  config: function () {
    return function (cm) {
      const file = Files.findOne({ _id: Session.get('fileId') })

      cm.setOption('lineNumbers', true)

      if (!Meteor.userId()) {
        cm.setOption('readOnly', true)
      } else {
        const collaboratorIds = Session.get('gistCollaborators').map(collaborator => collaborator.githubId)
        const collaboratorIndex = collaboratorIds.indexOf(Meteor.user().services.github.id)
        if ((collaboratorIndex < 0) && (Meteor.userId() !== Session.get('gistOwnerId'))) {
          cm.setOption('readOnly', true)
        }
      }

      // periodically update file object when there is a change in the editor
      cm.doc.on('change', _.debounce(function (editor) {
        Meteor.call('updateFile', file._id, editor.getValue())
      }, 500))
    }
  },
  setContent: function () {
    return function (cm) {
      const file = Files.findOne({ _id: Session.get('fileId') })
      cm.doc.setValue(file.content)
    }
  }
})
//
// Template.editingUsers.helpers({
//   users: function () {
//     var doc, eusers, users
//     doc = Documents.findOne()
//     if (!doc) {
//       return
//     }
//     eusers = EditingUsers.findOne()
//     if (!eusers) {
//       return
//     }
//<commit_msg>fix bug - gistCollaborators undefined<commit_after>/* global _ */

Template.editor.helpers({
  owner: function () {
    return (Meteor.userId() && (Meteor.userId() === Session.get('gistOwnerId')))
  },

  fileId: function () {
    return Session.get('fileId')
  },
  filename: function () {
    return Files.findOne({ _id: Session.get('fileId') }).filename
  },
  config: function () {
    return function (cm) {
      const file = Files.findOne({ _id: Session.get('fileId') })

      cm.setOption('lineNumbers', true)

      if (!Meteor.userId()) {
        cm.setOption('readOnly', true)
      } else {
        const collaborators = Session.get('gistCollaborators')
        if (collaborators) {
          const collaboratorIds = collaborators.map(collaborator => collaborator.githubId)
          const collaboratorIndex = collaboratorIds.indexOf(Meteor.user().services.github.id)
          if ((collaboratorIndex < 0) && (Meteor.userId() !== Session.get('gistOwnerId'))) {
            cm.setOption('readOnly', true)
          }
        }
      }

      // periodically update file object when there is a change in the editor
      cm.doc.on('change', _.debounce(function (editor) {
        Meteor.call('updateFile', file._id, editor.getValue())
      }, 500))
    }
  },
  setContent: function () {
    return function (cm) {
      const file = Files.findOne({ _id: Session.get('fileId') })
      cm.doc.setValue(file.content)
    }
  }
})
//
// Template.editingUsers.helpers({
//   users: function () {
//     var doc, eusers, users
//     doc = Documents.findOne()
//     if (!doc) {
//       return
//     }
//     eusers = EditingUsers.findOne()
//     if (!eusers) {
//       return
//     }
//
{'fraction_non_alphanumeric': 0.16631130063965885, 'fraction_numerical': 0.0024367956137678953, 'mean_word_length': 2.5085470085470085, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7559393', 'n_tokens_mistral': 1050, 'n_tokens_neox': 1025, 'n_words': 204}