<commit_before>import _ from 'lodash'
import util from './util'
import Flexio from './flexio'

export default () => {
  var retval = _.assign({}, {
    // -- state --

    pipe: {
      name: 'Javascript SDK Pipe',
      description: 'This pipe was created using the Flex.io Javascript SDK',
      task: []
    },
    processes: [],
    _params: {}, // avoid collision with `params` method name
    loading: false,
    saving: false,
    running: false,

    // -- methods --

    getJSON() {
      return _.assign({}, this.pipe)
    },

    getProcesses() {
      return [].concat(this.processes)
    },

    getLastProcess() {
      return _.last(this.processes)
    },

    addTask(task) {
      this.pipe.task.push(task)
      return this
    },

    clearTasks() {
      this.pipe.task = []
      return this
    },

    load() {
      var args = Array.from(arguments)
      var identifier = _.get(args, '[0]')
      var callback = _.get(args, '[1]')

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.load.apply(this, arguments) }, 50)
        return this
      }

      if (_.isNil(identifier))
        return util.debug.call(this, "The `identifier` parameter is required. Either the pipe's eid or pipe's alias may be used.")

      this.loading = true
      util.debug.call(this, 'Loading Pipe `' + identifier + '`...')

      Flexio.http().get('/pipes/' + identifier)
        .then(response => {
          var obj = _.get(response, 'data', {})
          this.pipe = _.assign({}, obj)
          this.loading = false
          util.debug.call(this, 'Pipe Loaded.')

          if (typeof callback == 'function')
            callback.call(this, null, obj)
        })
        .catch(error => {
          this.loading = false
          util.debug.call(this, 'Pipe Load Failed.')

          if (typeof callback == 'function')
            callback.call(this, error, null)
        })

      return this
    },

    save() {
      var args = Array.from(arguments)
      var params = _.get(args, '[0]')
      var callback = _.get(args, '[0]')

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.save.apply(this, arguments) }, 50)
        return this
      }

      if (_.isPlainObject(params))
      {
        _.assign(this.pipe, _.pick(params, ['name', 'description', 'ename']))
        callback = _.get(args, '[1]')
      }

      this.saving = true
      util.debug.call(this, 'Saving Pipe `' + _.get(this.pipe, 'name', 'Untitled Pipe') + '`...')

      Flexio.http().post('/pipes', this.pipe)
        .then(response => {
          var pipe = _.get(response, 'data', {})
          this.pipe = _.assign({}, pipe)
          this.saving = false
          util.debug.call(this, 'Pipe Saved.')

          if (typeof callback == 'function')
            callback.call(this, null, this.pipe)
        })
        .catch(error => {
          this.saving = false
          util.debug.call(this, 'Pipe Save Failed.')

          if (typeof callback == 'function')
            callback.call(this, error, null)
        })

      return this
    },

    /*
    run() {
      var args = Array.from(arguments)
      var params = _.get(args, '[0]')
      var callback = _.get(args, '[0]')
      var run_params = _.assign({}, this.getParams())

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.run.apply(this, arguments) }, 50)
        return this
      }

      if (_.isPlainObject(params))
      {
        run_params = _.assign({}, run_params, params)
        callback = _.get(args, '[1]')
      }

      this.running = true
      util.debug.call(this, 'Running Pipe `' + _.get(this.pipe, 'name', 'Untitled Pipe') + '`...')

      var create_params = _.assign({}, this.pipe)

      // if we have saved this pipe, use the pipe's eid as the parent eid
      var parent_eid = _.get(this.pipe, 'eid', '')
      if (parent_eid.length > 0)
        create_params = { parent_eid }

      // set the process to run mode
      _.assign(create_params, {
        process_mode: 'R'
      })

      Flexio.http().post('/processes', create_params)
        .then(response => {
          var obj = _.get(response, 'data', {})
          var process_eid = _.get(obj, 'eid', '')
          this.processes.push(obj)
          util.debug.call(this, 'Created Process.')

          // TODO: we need to figure out how we can allow the
          //       user to specify their own response options
          // config to pass to Axios
          var config = {
            responseType: 'arraybuffer'
          }

          Flexio.http().post('/processes/'+process_eid+'/run', run_params, config)
            .then(response => {

              this.running = false
              util.debug.call(this, 'Process Complete.')

              var arraybuffer = response.data
              var content_type =  _.get(response, 'headers.content-type', 'text/plain')

              var response_object = {
                contentType: content_type,
                buffer: arraybuffer,
                get blob() {
                  return new Blob([this.buffer], {"type":content_type})
                },
                get text() {
                  return util.arrayBufferToString(this.buffer)
                },
                get data() {
                  try {
                    return JSON.parse(util.arrayBufferToString(this.buffer))
                  }
                  catch (e) {
                    return null
                  }
                }
              }

              if (typeof callback == 'function')
                callback.call(this, null, response_object)
              //
              //
              //var obj2 = _.get(response, 'data', {})
              //util.debug.call(this, 'Process Complete.')
              //this.running = false
              //
              //if (typeof callback == 'function')
              //  callback.call(this, null, obj2)
              
            })
        })
        .catch(error => {
          util.debug.call(this, 'Process Create Failed.')
          this.running = false

          if (typeof callback == 'function')
            callback.call(this, error, null)
        })

      return this
    },
*/


    run() {
      var args = Array.from(arguments)
      var params = _.get(args, '[0]')
      var callback = _.get(args, '[0]')
      var run_params = _.assign({}, this.getParams())

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.run.apply(this, arguments) }, 50)
        return this
      }

      if (_.isPlainObject(params))
      {
        run_params = _.assign({}, run_params, params)
        callback = _.get(args, '[1]')
      }

      this.running = true
      util.debug.call(this, 'Running Pipe `' + _.get(this.pipe, 'name', 'Untitled Pipe') + '`...')

      var create_params = _.assign({}, this.pipe)

      // if we have saved this pipe, use the pipe's eid as the parent eid
      var parent_eid = _.get(this.pipe, 'eid', '')
      if (parent_eid.length > 0)
        create_params = { parent_eid }

      // set the process to run mode
      _.assign(create_params, {
        process_mode: 'R'
      })

      var pipe_eid = _.get(this.pipe, 'eid', '')

      var http_config = {
        method: 'post',
        url: '/pipes/'+pipe_eid+'/run',
        responseType: 'arraybuffer',
        data: ''
      }

      if (run_params.hasOwnProperty('data')) {
        http_config.data = run_params.data
      }

      if (run_params.hasOwnProperty('contentType')) {
        http_config.headers = { 'Content-Type': run_params.contentType }
      }

      console.log(http_config);

      var http = Flexio.http()

      http(http_config)
        .then(response => {

          this.running = false
          util.debug.call(this, 'Process Complete.')<commit_msg>real simple content type detection if none specified<commit_after>import _ from 'lodash'
import util from './util'
import Flexio from './flexio'

export default () => {
  var retval = _.assign({}, {
    // -- state --

    pipe: {
      name: 'Javascript SDK Pipe',
      description: 'This pipe was created using the Flex.io Javascript SDK',
      task: []
    },
    processes: [],
    _params: {}, // avoid collision with `params` method name
    loading: false,
    saving: false,
    running: false,

    // -- methods --

    getJSON() {
      return _.assign({}, this.pipe)
    },

    getProcesses() {
      return [].concat(this.processes)
    },

    getLastProcess() {
      return _.last(this.processes)
    },

    addTask(task) {
      this.pipe.task.push(task)
      return this
    },

    clearTasks() {
      this.pipe.task = []
      return this
    },

    load() {
      var args = Array.from(arguments)
      var identifier = _.get(args, '[0]')
      var callback = _.get(args, '[1]')

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.load.apply(this, arguments) }, 50)
        return this
      }

      if (_.isNil(identifier))
        return util.debug.call(this, "The `identifier` parameter is required. Either the pipe's eid or pipe's alias may be used.")

      this.loading = true
      util.debug.call(this, 'Loading Pipe `' + identifier + '`...')

      Flexio.http().get('/pipes/' + identifier)
        .then(response => {
          var obj = _.get(response, 'data', {})
          this.pipe = _.assign({}, obj)
          this.loading = false
          util.debug.call(this, 'Pipe Loaded.')

          if (typeof callback == 'function')
            callback.call(this, null, obj)
        })
        .catch(error => {
          this.loading = false
          util.debug.call(this, 'Pipe Load Failed.')

          if (typeof callback == 'function')
            callback.call(this, error, null)
        })

      return this
    },

    save() {
      var args = Array.from(arguments)
      var params = _.get(args, '[0]')
      var callback = _.get(args, '[0]')

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.save.apply(this, arguments) }, 50)
        return this
      }

      if (_.isPlainObject(params))
      {
        _.assign(this.pipe, _.pick(params, ['name', 'description', 'ename']))
        callback = _.get(args, '[1]')
      }

      this.saving = true
      util.debug.call(this, 'Saving Pipe `' + _.get(this.pipe, 'name', 'Untitled Pipe') + '`...')

      Flexio.http().post('/pipes', this.pipe)
        .then(response => {
          var pipe = _.get(response, 'data', {})
          this.pipe = _.assign({}, pipe)
          this.saving = false
          util.debug.call(this, 'Pipe Saved.')

          if (typeof callback == 'function')
            callback.call(this, null, this.pipe)
        })
        .catch(error => {
          this.saving = false
          util.debug.call(this, 'Pipe Save Failed.')

          if (typeof callback == 'function')
            callback.call(this, error, null)
        })

      return this
    },

    /*
    run() {
      var args = Array.from(arguments)
      var params = _.get(args, '[0]')
      var callback = _.get(args, '[0]')
      var run_params = _.assign({}, this.getParams())

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.run.apply(this, arguments) }, 50)
        return this
      }

      if (_.isPlainObject(params))
      {
        run_params = _.assign({}, run_params, params)
        callback = _.get(args, '[1]')
      }

      this.running = true
      util.debug.call(this, 'Running Pipe `' + _.get(this.pipe, 'name', 'Untitled Pipe') + '`...')

      var create_params = _.assign({}, this.pipe)

      // if we have saved this pipe, use the pipe's eid as the parent eid
      var parent_eid = _.get(this.pipe, 'eid', '')
      if (parent_eid.length > 0)
        create_params = { parent_eid }

      // set the process to run mode
      _.assign(create_params, {
        process_mode: 'R'
      })

      Flexio.http().post('/processes', create_params)
        .then(response => {
          var obj = _.get(response, 'data', {})
          var process_eid = _.get(obj, 'eid', '')
          this.processes.push(obj)
          util.debug.call(this, 'Created Process.')

          // TODO: we need to figure out how we can allow the
          //       user to specify their own response options
          // config to pass to Axios
          var config = {
            responseType: 'arraybuffer'
          }

          Flexio.http().post('/processes/'+process_eid+'/run', run_params, config)
            .then(response => {

              this.running = false
              util.debug.call(this, 'Process Complete.')

              var arraybuffer = response.data
              var content_type =  _.get(response, 'headers.content-type', 'text/plain')

              var response_object = {
                contentType: content_type,
                buffer: arraybuffer,
                get blob() {
                  return new Blob([this.buffer], {"type":content_type})
                },
                get text() {
                  return util.arrayBufferToString(this.buffer)
                },
                get data() {
                  try {
                    return JSON.parse(util.arrayBufferToString(this.buffer))
                  }
                  catch (e) {
                    return null
                  }
                }
              }

              if (typeof callback == 'function')
                callback.call(this, null, response_object)
              //
              //
              //var obj2 = _.get(response, 'data', {})
              //util.debug.call(this, 'Process Complete.')
              //this.running = false
              //
              //if (typeof callback == 'function')
              //  callback.call(this, null, obj2)
              
            })
        })
        .catch(error => {
          util.debug.call(this, 'Process Create Failed.')
          this.running = false

          if (typeof callback == 'function')
            callback.call(this, error, null)
        })

      return this
    },
*/


    run() {
      var args = Array.from(arguments)
      var params = _.get(args, '[0]')
      var callback = _.get(args, '[0]')
      var run_params = _.assign({}, this.getParams())

      if (this.loading === true || this.saving === true || this.running === true)
      {
        setTimeout(() => { this.run.apply(this, arguments) }, 50)
        return this
      }

      if (_.isPlainObject(params))
      {
        run_params = _.assign({}, run_params, params)
        callback = _.get(args, '[1]')
      }

      this.running = true
      util.debug.call(this, 'Running Pipe `' + _.get(this.pipe, 'name', 'Untitled Pipe') + '`...')

      var create_params = _.assign({}, this.pipe)

      // if we have saved this pipe, use the pipe's eid as the parent eid
      var parent_eid = _.get(this.pipe, 'eid', '')
      if (parent_eid.length > 0)
        create_params = { parent_eid }

      // set the process to run mode
      _.assign(create_params, {
        process_mode: 'R'
      })

      var pipe_eid = _.get(this.pipe, 'eid', '')

      var http_config = {
        method: 'post',
        url: '/pipes/'+pipe_eid+'/run',
        responseType: 'arraybuffer'
      }

      if (run_params.hasOwnProperty('data')) {
        http_config.data = run_params.data
      }

      if (run_params.hasOwnProperty('contentType')) {
        http_config.headers = { 'Content-Type': run_params.contentType }
      } else {
        // no content type specified; choose a sensible one unless
        // axios can detect it
        if (http_config.hasOwnProperty('data')) {
          if (_.isPlainObject(http_config.data)) {
            // axios can figure it out
          } else if (_.isString(http_config.data)) {
            http_config.headers = { 'Content-Type': 'text/plain' }
          } else {
            http_config.headers = { 'Content-Type': 'application/octet-stream' }
          }
        }
      }

      console.log(http_config);

      var http = Flexio.http()

      http(http_config)
        .then(response => {

          this.running = false
          util.debug.call(this, 'Process Complete.')
{'fraction_non_alphanumeric': 0.14667717681300024, 'fraction_numerical': 0.002789231142372059, 'mean_word_length': 1.7037704918032788, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 33, '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': '45940', 'n_tokens_mistral': 4948, 'n_tokens_neox': 4755, 'n_words': 1154}