<commit_before>/** @babel */
/** @jsx etch.dom */

import etch from 'etch'

import FilePatchListComponent from './file-patch-list-component'

export default class StagingComponent {
  constructor ({repository, didSelectFilePatch}) {
    this.setRepository(repository)
    this.didSelectFilePatch = didSelectFilePatch || function () {}
    this.focusedList = 'staged'
    this.stagedSelectedFilePatch = null
    this.unstagedSelectedFilePatch = null
    etch.initialize(this)

    this.subscriptions = atom.commands.add(this.element, {
      'core:move-up': this.didSelectPreviousFilePatch.bind(this),
      'core:move-down': this.didSelectNextFilePatch.bind(this),
      'core:confirm': () => { this.lastRepositoryStagePromise = this.didConfirmFilePatch() },
      'git:focus-unstaged-changes': () => this.didSelectUnstagedFilePatch(),
      'git:focus-staged-changes': () => this.didSelectStagedFilePatch()
    })
  }

  update ({repository}) {
    this.setRepository(repository)
    return etch.update(this)
  }

  setRepository (repository) {
    if (this.repository !== repository) {
      this.repository = repository
      this.modelData = null
      if (this.repositorySubscription) {
        this.repositorySubscription.dispose()
        this.repositorySubscription = null
      }
      if (repository) {
        this.refreshModelData()
        this.repositorySubscription = repository.onDidUpdate(this.refreshModelData.bind(this))
      }
    } else {
      return Promise.resolve()
    }
  }

  refreshModelData () {
    this.lastModelDataRefreshPromise = this.performModelDataRefresh()
    return this.lastModelDataRefreshPromise
  }

  async performModelDataRefresh () {
    const stagedFilePatches = await this.repository.getStagedChanges()
    const unstagedFilePatches = await this.repository.getUnstagedChanges()
    this.modelData = {stagedFilePatches, unstagedFilePatches}
    if (!this.stagedSelectedFilePatch) {
      this.stagedSelectedFilePatch = stagedFilePatches[0]
    }
    if (!this.unstagedSelectedFilePatch) {
      this.unstagedSelectedFilePatch = unstagedFilePatches[0]
    }
    return etch.update(this)
  }

  didConfirmStagedFilePatch (filePatch) {
    return this.repository.applyPatchToIndex(filePatch.getUnstagePatch())
  }

  didConfirmUnstagedFilePatch (filePatch) {
    return this.repository.applyPatchToIndex(filePatch)
  }

  didConfirmFilePatch () {
    if (this.focusedList === 'staged') {
      return this.didConfirmStagedFilePatch(this.stagedSelectedFilePatch)
    } else {
      return this.didConfirmUnstagedFilePatch(this.unstagedSelectedFilePatch)
    }
  }

  didSelectStagedFilePatch (filePatch) {
    if (filePatch) this.stagedSelectedFilePatch = filePatch
    this.focusedList = 'staged'
    this.didSelectFilePatch(this.stagedSelectedFilePatch, 'staged')
    return etch.update(this)
  }

  didSelectUnstagedFilePatch (filePatch) {
    if (filePatch) this.unstagedSelectedFilePatch = filePatch
    this.focusedList = 'unstaged'
    this.didSelectFilePatch(this.unstagedSelectedFilePatch, 'unstaged')
    return etch.update(this)
  }

  didSelectPreviousFilePatch () {
    const list = this.focusedList
    if (list === 'staged') {
      this.didSelectPreviousStagedFilePatch()
    } else {
      this.didSelectPreviousUnstagedFilePatch()
    }
  }

  didSelectPreviousStagedFilePatch () {
    const currentSelectedFilePatch = this.stagedSelectedFilePatch
    const filePatches = this.modelData.stagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) - 1
    if (index < 0) {
      this.didSelectLastFilePatch('unstaged')
    } else {
      this.didSelectStagedFilePatch(filePatches[index])
    }
  }

  didSelectPreviousUnstagedFilePatch () {
    const currentSelectedFilePatch = this.unstagedSelectedFilePatch
    const filePatches = this.modelData.unstagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) - 1
    if (index < 0) {
      this.didSelectLastFilePatch('staged')
    } else {
      this.didSelectUnstagedFilePatch(filePatches[index])
    }
  }

  didSelectNextFilePatch () {
    const list = this.focusedList
    if (list === 'staged') {
      this.didSelectNextStagedFilePatch()
    } else {
      this.didSelectNextUnstagedFilePatch()
    }
  }

  didSelectNextStagedFilePatch () {
    const currentSelectedFilePatch = this.stagedSelectedFilePatch
    const filePatches = this.modelData.stagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) + 1
    if (index >= filePatches.length) {
      this.didSelectFirstFilePatch('unstaged')
    } else {
      this.didSelectStagedFilePatch(filePatches[index])
    }
  }

  didSelectNextUnstagedFilePatch () {
    const currentSelectedFilePatch = this.unstagedSelectedFilePatch
    const filePatches = this.modelData.unstagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) + 1
    if (index >= filePatches.length) {
      this.didSelectFirstFilePatch('staged')
    } else {
      this.didSelectUnstagedFilePatch(filePatches[index])
    }
  }

  didSelectFirstFilePatch (list) {
    if (list === 'staged') {
      this.didSelectStagedFilePatch(this.modelData.stagedFilePatches[0])
    } else {
      this.didSelectUnstagedFilePatch(this.modelData.unstagedFilePatches[0])
    }
  }

  didSelectLastFilePatch (list) {
    if (list === 'staged') {
      const stagedFilePatches = this.modelData.stagedFilePatches
      this.didSelectStagedFilePatch(stagedFilePatches[stagedFilePatches.length - 1])
    } else {
      const unstagedFilePatches = this.modelData.unstagedFilePatches
      this.didSelectUnstagedFilePatch(unstagedFilePatches[unstagedFilePatches.length - 1])
    }
  }

  render () {
    if (this.modelData == null) {
      return <div />
    } else {
      let stagedClassName, unstagedClassName
      if (this.focusedList === 'staged') {
        stagedClassName = 'is-focused'
        unstagedClassName = ''
      } else {
        stagedClassName = ''
        unstagedClassName = 'is-focused'
      }
      return (
        <div className={`git-StagingComponent ${this.focusedList + '-changes-focused'}`} style={{width: 200}} tabIndex='-1'>
          <div className={`git-Panel-item is-flexible git-StagedChanges ${stagedClassName}`}>
            <header className='git-CommitPanel-item is-header'>Staged Changes</header>
            <FilePatchListComponent
              ref='stagedChangesComponent'
              didSelectFilePatch={this.didSelectStagedFilePatch.bind(this)}
              didConfirmFilePatch={this.didConfirmStagedFilePatch.bind(this)}
              filePatches={this.modelData.stagedFilePatches}
              selectedFilePatch={this.stagedSelectedFilePatch} />
          </div>
          <div className={`git-Panel-item is-flexible git-UnstagedChanges ${unstagedClassName}`}>
            <header className='git-CommitPanel-item is-header'>Unstaged Changes</header><commit_msg>Add buildDebugData method for DebuggerComponent use<commit_after>/** @babel */
/** @jsx etch.dom */

import etch from 'etch'

import FilePatchListComponent from './file-patch-list-component'

export default class StagingComponent {
  constructor ({repository, didSelectFilePatch}) {
    this.setRepository(repository)
    this.didSelectFilePatch = didSelectFilePatch || function () {}
    this.focusedList = 'staged'
    this.stagedSelectedFilePatch = null
    this.unstagedSelectedFilePatch = null
    etch.initialize(this)

    this.subscriptions = atom.commands.add(this.element, {
      'core:move-up': this.didSelectPreviousFilePatch.bind(this),
      'core:move-down': this.didSelectNextFilePatch.bind(this),
      'core:confirm': () => { this.lastRepositoryStagePromise = this.didConfirmFilePatch() },
      'git:focus-unstaged-changes': () => this.didSelectUnstagedFilePatch(),
      'git:focus-staged-changes': () => this.didSelectStagedFilePatch()
    })
  }

  update ({repository}) {
    this.setRepository(repository)
    return etch.update(this)
  }

  setRepository (repository) {
    if (this.repository !== repository) {
      this.repository = repository
      this.modelData = null
      if (this.repositorySubscription) {
        this.repositorySubscription.dispose()
        this.repositorySubscription = null
      }
      if (repository) {
        this.refreshModelData()
        this.repositorySubscription = repository.onDidUpdate(this.refreshModelData.bind(this))
      }
    } else {
      return Promise.resolve()
    }
  }

  refreshModelData () {
    this.lastModelDataRefreshPromise = this.performModelDataRefresh()
    return this.lastModelDataRefreshPromise
  }

  async performModelDataRefresh () {
    const stagedFilePatches = await this.repository.getStagedChanges()
    const unstagedFilePatches = await this.repository.getUnstagedChanges()
    this.modelData = {stagedFilePatches, unstagedFilePatches}
    if (!this.stagedSelectedFilePatch) {
      this.stagedSelectedFilePatch = stagedFilePatches[0]
    }
    if (!this.unstagedSelectedFilePatch) {
      this.unstagedSelectedFilePatch = unstagedFilePatches[0]
    }
    return etch.update(this)
  }

  didConfirmStagedFilePatch (filePatch) {
    return this.repository.applyPatchToIndex(filePatch.getUnstagePatch())
  }

  didConfirmUnstagedFilePatch (filePatch) {
    return this.repository.applyPatchToIndex(filePatch)
  }

  didConfirmFilePatch () {
    if (this.focusedList === 'staged') {
      return this.didConfirmStagedFilePatch(this.stagedSelectedFilePatch)
    } else {
      return this.didConfirmUnstagedFilePatch(this.unstagedSelectedFilePatch)
    }
  }

  didSelectStagedFilePatch (filePatch) {
    if (filePatch) this.stagedSelectedFilePatch = filePatch
    this.focusedList = 'staged'
    this.didSelectFilePatch(this.stagedSelectedFilePatch, 'staged')
    return etch.update(this)
  }

  didSelectUnstagedFilePatch (filePatch) {
    if (filePatch) this.unstagedSelectedFilePatch = filePatch
    this.focusedList = 'unstaged'
    this.didSelectFilePatch(this.unstagedSelectedFilePatch, 'unstaged')
    return etch.update(this)
  }

  didSelectPreviousFilePatch () {
    const list = this.focusedList
    if (list === 'staged') {
      this.didSelectPreviousStagedFilePatch()
    } else {
      this.didSelectPreviousUnstagedFilePatch()
    }
  }

  didSelectPreviousStagedFilePatch () {
    const currentSelectedFilePatch = this.stagedSelectedFilePatch
    const filePatches = this.modelData.stagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) - 1
    if (index < 0) {
      this.didSelectLastFilePatch('unstaged')
    } else {
      this.didSelectStagedFilePatch(filePatches[index])
    }
  }

  didSelectPreviousUnstagedFilePatch () {
    const currentSelectedFilePatch = this.unstagedSelectedFilePatch
    const filePatches = this.modelData.unstagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) - 1
    if (index < 0) {
      this.didSelectLastFilePatch('staged')
    } else {
      this.didSelectUnstagedFilePatch(filePatches[index])
    }
  }

  didSelectNextFilePatch () {
    const list = this.focusedList
    if (list === 'staged') {
      this.didSelectNextStagedFilePatch()
    } else {
      this.didSelectNextUnstagedFilePatch()
    }
  }

  didSelectNextStagedFilePatch () {
    const currentSelectedFilePatch = this.stagedSelectedFilePatch
    const filePatches = this.modelData.stagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) + 1
    if (index >= filePatches.length) {
      this.didSelectFirstFilePatch('unstaged')
    } else {
      this.didSelectStagedFilePatch(filePatches[index])
    }
  }

  didSelectNextUnstagedFilePatch () {
    const currentSelectedFilePatch = this.unstagedSelectedFilePatch
    const filePatches = this.modelData.unstagedFilePatches
    let index = filePatches.indexOf(currentSelectedFilePatch) + 1
    if (index >= filePatches.length) {
      this.didSelectFirstFilePatch('staged')
    } else {
      this.didSelectUnstagedFilePatch(filePatches[index])
    }
  }

  didSelectFirstFilePatch (list) {
    if (list === 'staged') {
      this.didSelectStagedFilePatch(this.modelData.stagedFilePatches[0])
    } else {
      this.didSelectUnstagedFilePatch(this.modelData.unstagedFilePatches[0])
    }
  }

  didSelectLastFilePatch (list) {
    if (list === 'staged') {
      const stagedFilePatches = this.modelData.stagedFilePatches
      this.didSelectStagedFilePatch(stagedFilePatches[stagedFilePatches.length - 1])
    } else {
      const unstagedFilePatches = this.modelData.unstagedFilePatches
      this.didSelectUnstagedFilePatch(unstagedFilePatches[unstagedFilePatches.length - 1])
    }
  }

  buildDebugData () {
    const getPath = (fp) => fp ? fp.getNewPath() : '<none>'
    return {
      focusedList: this.focusedList,
      stagedSelectedFilePatch: getPath(this.stagedSelectedFilePatch),
      unstagedSelectedFilePatch: getPath(this.unstagedSelectedFilePatch),
      stagedFilePatches: this.modelData.stagedFilePatches.map((p) => p.getNewPath()),
      unstagedFilePatches: this.modelData.unstagedFilePatches.map((p) => p.getNewPath()),
      index: index
    }
  }

  render () {
    if (this.modelData == null) {
      return <div />
    } else {
      let stagedClassName, unstagedClassName
      if (this.focusedList === 'staged') {
        stagedClassName = 'is-focused'
        unstagedClassName = ''
      } else {
        stagedClassName = ''
        unstagedClassName = 'is-focused'
      }
      return (
        <div className={`git-StagingComponent ${this.focusedList + '-changes-focused'}`} style={{width: 200}} tabIndex='-1'>
          <div className={`git-Panel-item is-flexible git-StagedChanges ${stagedClassName}`}>
            <header className='git-CommitPanel-item is-header'>Staged Changes</header>
            <FilePatchListComponent
              ref='stagedChangesComponent'
              didSelectFilePatch={this.didSelectStagedFilePatch.bind(this)}
              didConfirmFilePatch={this.didConfirmStagedFilePatch.bind(this)}
              filePatches={this.modelData.stagedFilePatches}
              selectedFilePatch={this.stagedSelectedFilePatch} />
          </div>
          <div className={`git-Panel-item is-flexible git-UnstagedChanges ${unstagedClassName}`}>
            <header className='git-CommitPanel-item is-header'>Unstaged Changes</header>
{'fraction_non_alphanumeric': 0.10405348934391977, 'fraction_numerical': 0.0022287226633235825, 'mean_word_length': 3.6229877656149387, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 37, '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': '5261766', 'n_tokens_mistral': 4325, 'n_tokens_neox': 4276, 'n_words': 634}