<commit_before>import checkError from '../check-error.js'

export default function (itemId) {
  var Item = {
    /**
    * Gets current username
    * @returns {Promise} On resolution will return current users name. You need that.
    */
    getUsername: function() {
      return this.request(`portals/self`)
      .then(function (results) {
        return results.user.username
      })
    },
    /**
    * Creats a rando id for publishing items, which need to be at a unique url endpoint.
    * @returns {String} A rando UUID that uses _ instead of - because of reasons
    */
    generateUUID: function() {
      var d = new Date().getTime();
      if(window.performance && typeof window.performance.now === "function"){
        d += performance.now();; //use high-precision timer if available
      }
      var uuid = 'xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
      });
      return uuid;
    },

    /**
    * Gets an item
    * @returns {Promise} On resolution will return Item Object
    */
    get: function() {
      var item = this
      // Step One:
      // Promise.all to return:
      //  - Fav GroupID
      //  - All Groups item is in
      //  - Item Details
      return Promise.all([
        item.request(`portals/self`).then(function (results) {
          checkError(results)
          return results.user.favGroupId
        }),
        item.groups(),
        item.request(`content/items/${itemId}`),
      ])
      .catch(function (err) {
        console.error(err)
      })
      .then(function (results) {
        checkError(results)
        // Step Two:
        //  - Add all groups to item.groups = []
        var itemDetails = results[2]
        //  - Add all groups to item.groups = []
        itemDetails.groups = results [1]
        //  - Is Fav Group ID in the All Groups List? item.favorite = bool
        var groupIds = itemDetails.groups.map(function(object) {
          return object.id
        })
        itemDetails.favorite = groupIds.indexOf(results[0]) > -1
        // Step Three:
        //  - Return the item details with new information.
        console.log(itemDetails)
        return itemDetails
      })
    },
    /**
    * If the item has hosted geographic data, returns all of that data.
    * @returns {Promise} On resolution will return a big pile of data
    */
    data: function () {
      var _item = this
      return _item.get()
      .then(function (results) {
        console.log(`${results.url}/0`)
        var dataOptions = {
          where: '1=1',
          returnGeometry: false,
          outFields: '*'
        }
        return _item.request(`/query`, dataOptions, false, `${results.url}/0`)
      })
      .then(function (results) {
        console.log(results)
        return results
      })
    },
    /**
    * Updates the Item Details Stuff
    * @param {Object} Key value pair of the stuff that needs to be updated.
    * @returns {Promise} On resolution will return the new item object, with the stuff and things updated.
    */
    update: function(options) {
      var item = this
      item.getUsername()
      .then(function (username) {
        return item.request(`content/users/${username}/items/${itemId}/update`, options, true)
        .then(function (results) {
          return item.get(item)
        })
      })
    },
    /**
    * Rates an Item
    * @param {Number} An integer between 0 and 5. Pass a value of 'null' to delete the authenticated users rating.<commit_msg>item.update needs to return stuff that usually helps<commit_after>import checkError from '../check-error.js'

export default function (itemId) {
  var Item = {
    /**
    * Gets current username
    * @returns {Promise} On resolution will return current users name. You need that.
    */
    getUsername: function() {
      return this.request(`portals/self`)
      .then(function (results) {
        return results.user.username
      })
    },
    /**
    * Creats a rando id for publishing items, which need to be at a unique url endpoint.
    * @returns {String} A rando UUID that uses _ instead of - because of reasons
    */
    generateUUID: function() {
      var d = new Date().getTime();
      if(window.performance && typeof window.performance.now === "function"){
        d += performance.now();; //use high-precision timer if available
      }
      var uuid = 'xxxxxxxx_xxxx_4xxx_yxxx_xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
      });
      return uuid;
    },

    /**
    * Gets an item
    * @returns {Promise} On resolution will return Item Object
    */
    get: function() {
      var item = this
      // Step One:
      // Promise.all to return:
      //  - Fav GroupID
      //  - All Groups item is in
      //  - Item Details
      return Promise.all([
        item.request(`portals/self`).then(function (results) {
          checkError(results)
          return results.user.favGroupId
        }),
        item.groups(),
        item.request(`content/items/${itemId}`),
      ])
      .catch(function (err) {
        console.error(err)
      })
      .then(function (results) {
        checkError(results)
        // Step Two:
        //  - Add all groups to item.groups = []
        var itemDetails = results[2]
        //  - Add all groups to item.groups = []
        itemDetails.groups = results [1]
        //  - Is Fav Group ID in the All Groups List? item.favorite = bool
        var groupIds = itemDetails.groups.map(function(object) {
          return object.id
        })
        itemDetails.favorite = groupIds.indexOf(results[0]) > -1
        // Step Three:
        //  - Return the item details with new information.
        console.log(itemDetails)
        return itemDetails
      })
    },
    /**
    * If the item has hosted geographic data, returns all of that data.
    * @returns {Promise} On resolution will return a big pile of data
    */
    data: function () {
      var _item = this
      return _item.get()
      .then(function (results) {
        console.log(`${results.url}/0`)
        var dataOptions = {
          where: '1=1',
          returnGeometry: false,
          outFields: '*'
        }
        return _item.request(`/query`, dataOptions, false, `${results.url}/0`)
      })
      .then(function (results) {
        console.log(results)
        return results
      })
    },
    /**
    * Updates the Item Details Stuff
    * @param {Object} Key value pair of the stuff that needs to be updated.
    * @returns {Promise} On resolution will return the new item object, with the stuff and things updated.
    */
    update: function(options) {
      var item = this
      return item.getUsername()
      .then(function (username) {
        return item.request(`content/users/${username}/items/${itemId}/update`, options, true)
        .then(function (results) {
          return item.get(item)
        })
      })
    },
    /**
    * Rates an Item
    * @param {Number} An integer between 0 and 5. Pass a value of 'null' to delete the authenticated users rating.
{'fraction_non_alphanumeric': 0.12362788144895719, 'fraction_numerical': 0.006586169045005488, 'mean_word_length': 2.009496284062758, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '2593980', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2073, 'n_words': 629}