<commit_before>kd = require 'kd'
_  = require 'lodash'
async   = require 'async'
whoami  = require 'app/util/whoami'
globals = require 'globals'
remote  = require('app/remote').getInstance()
showError = require 'app/util/showError'
KodingKontrol = require 'app/kite/kodingkontrol'
CredentialsPageView = require '../views/stackflow/credentialspageview'
CredentialsErrorPageView = require '../views/stackflow/credentialserrorpageview'
constants = require '../constants'

module.exports = class CredentialsController extends kd.Controller

  loadData: ->

    stack = @getData()
    queue = {
      credentials  : (next) -> helpers.loadCredentials stack, next
      requirements : (next) -> helpers.loadRequirements stack, next
      kdCmd        : (next) -> helpers.getKDCmd next
    }

    async.parallel queue, (err, results) =>
      return  if showError err

      { credentials, requirements, kdCmd } = results
      @setup credentials, requirements, kdCmd


  setup: (credentials, requirements, kdCmd) ->

    stack = @getData()
    { container } = @getOptions()

    @credentialsPage = new CredentialsPageView {}, {
      stack
      credentials  : _.extend { kdCmd }, credentials
      requirements
    }
    @errorPage = new CredentialsErrorPageView()

    @forwardEvent @credentialsPage, 'InstructionsRequested'
    @credentialsPage.on 'Submitted', @bound 'onSubmitted'
    @errorPage.on 'CredentialsRequested', => container.showPage @credentialsPage

    container.appendPages @credentialsPage, @errorPage

    @emit 'ready'


  submit: -> @credentialsPage.submit()


  onSubmitted: (submissionData) ->

    { container } = @getOptions()
    { credential, requirements } = submissionData

    queue = [
      (next) => @handleSubmittedCredential credential, next
    ]

    if requirements
      queue.push (next) => @handleSubmittedRequirements requirements, next

    async.parallel queue, @bound 'handleSubmitResult'


  handleSubmitResult: (err, identifiers) ->

    unless @showError err

      @emit 'StartBuild', credentials

    @credentialsPage.buildButton.hideLoader()


  handleSubmittedCredential: (submissionData, callback) ->

    { provider, selectedItem, newData } = submissionData
    { CREDENTIAL_VERIFICATION_TIMEOUT } = constants

    pendingCredential = null

    queue = [
      (next) ->
        return next null, selectedItem  if selectedItem

        helpers.createNewCredential provider, newData, (err, newCredential) ->
          return next err  if err

          pendingCredential = newCredential
          next null, newCredential.identifier

      (identifier, next) =>

        { computeController } = kd.singletons
        computeController.getKloud()
          .checkCredential { provider, identifiers : [identifier] }
          .then (response) =>
            { err, verified } = @checkVerificationResult identifier, response
            next err, identifier
          .catch (err) -> next err.message
          .timeout constants.CREDENTIAL_VERIFICATION_TIMEOUT
    ]

    async.waterfall queue, (err, identifier) =>

      if err
        pendingCredential?.delete()
        callback err

      else

        if pendingCredential
          @credentialsPage.selectNewCredential pendingCredential

        response = {}
        response[provider] = [ identifier ]

        callback null, response


  handleSubmittedRequirements: (submissionData, callback) ->

    { provider, selectedItem, newData } = submissionData

    return callback null, selectedItem  if selectedItem

    helpers.createNewCredential provider, newData, (err, newCredential) =>
      return callback err.message  if err

      @credentialsPage.selectNewRequirements newCredential
      callback null, newCredential.identifier


  checkVerificationResult: (identifier, response) ->

    { CREDENTIAL_VERIFICATION_ERROR_MESSAGE } = constants<commit_msg>CredentialStoreController: send userInput credentials in a map<commit_after>kd = require 'kd'
_  = require 'lodash'
async   = require 'async'
whoami  = require 'app/util/whoami'
globals = require 'globals'
remote  = require('app/remote').getInstance()
showError = require 'app/util/showError'
KodingKontrol = require 'app/kite/kodingkontrol'
CredentialsPageView = require '../views/stackflow/credentialspageview'
CredentialsErrorPageView = require '../views/stackflow/credentialserrorpageview'
constants = require '../constants'

module.exports = class CredentialsController extends kd.Controller

  loadData: ->

    stack = @getData()
    queue = {
      credentials  : (next) -> helpers.loadCredentials stack, next
      requirements : (next) -> helpers.loadRequirements stack, next
      kdCmd        : (next) -> helpers.getKDCmd next
    }

    async.parallel queue, (err, results) =>
      return  if showError err

      { credentials, requirements, kdCmd } = results
      @setup credentials, requirements, kdCmd


  setup: (credentials, requirements, kdCmd) ->

    stack = @getData()
    { container } = @getOptions()

    @credentialsPage = new CredentialsPageView {}, {
      stack
      credentials  : _.extend { kdCmd }, credentials
      requirements
    }
    @errorPage = new CredentialsErrorPageView()

    @forwardEvent @credentialsPage, 'InstructionsRequested'
    @credentialsPage.on 'Submitted', @bound 'onSubmitted'
    @errorPage.on 'CredentialsRequested', => container.showPage @credentialsPage

    container.appendPages @credentialsPage, @errorPage

    @emit 'ready'


  submit: -> @credentialsPage.submit()


  onSubmitted: (submissionData) ->

    { container } = @getOptions()
    { credential, requirements } = submissionData

    queue = [
      (next) => @handleSubmittedCredential credential, next
    ]

    if requirements
      queue.push (next) => @handleSubmittedRequirements requirements, next

    async.parallel queue, @bound 'handleSubmitResult'


  handleSubmitResult: (err, identifiers) ->

    unless @showError err

      @emit 'StartBuild', credentials

    @credentialsPage.buildButton.hideLoader()


  handleSubmittedCredential: (submissionData, callback) ->

    { provider, selectedItem, newData } = submissionData
    { CREDENTIAL_VERIFICATION_TIMEOUT } = constants

    pendingCredential = null

    queue = [
      (next) ->
        return next null, selectedItem  if selectedItem

        helpers.createNewCredential provider, newData, (err, newCredential) ->
          return next err  if err

          pendingCredential = newCredential
          next null, newCredential.identifier

      (identifier, next) =>

        { computeController } = kd.singletons
        computeController.getKloud()
          .checkCredential { provider, identifiers : [identifier] }
          .then (response) =>
            { err, verified } = @checkVerificationResult identifier, response
            next err, identifier
          .catch (err) -> next err.message
          .timeout constants.CREDENTIAL_VERIFICATION_TIMEOUT
    ]

    async.waterfall queue, (err, identifier) =>

      if err
        pendingCredential?.delete()
        callback err

      else

        if pendingCredential
          @credentialsPage.selectNewCredential pendingCredential

        response = {}
        response[provider] = [ identifier ]

        callback null, response


  handleSubmittedRequirements: (submissionData, callback) ->

    { provider, selectedItem, newData } = submissionData

    kallback = (identifier) ->
      callback null, { userInput: [ identifier ] }

    return kallback selectedItem  if selectedItem

    helpers.createNewCredential provider, newData, (err, newCredential) =>
      return callback err.message  if err

      @credentialsPage.selectNewRequirements newCredential
      kallback newCredential.identifier


  checkVerificationResult: (identifier, response) ->

    { CREDENTIAL_VERIFICATION_ERROR_MESSAGE } = constants
{'fraction_non_alphanumeric': 0.09269162210338681, 'fraction_numerical': 0.0, 'mean_word_length': 3.1715347849176845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 48, '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': '3637480', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2070, 'n_words': 515}