const RingBuffer = require('./RingBuffer')
const EventEmitter = require('events')
const { clipboard } = require('electron')

const DEFAULT_INTERVAL_TIME = 2000
const DEFAULT_BUFFER_SIZE = 5
const INIT_BUFFER_INDEX = -1
const ADD_ENTRY_EVENT = 'add-entry'

module.exports = class ClipboardWatcher {
  constructor ({ intervalTime, bufferSize } = {
    intervalTime: DEFAULT_INTERVAL_TIME,
    bufferSize: DEFAULT_BUFFER_SIZE
  }) {
    this.intervalTime = intervalTime
    this._buffer = new RingBuffer()
    this._bufferIndex = INIT_BUFFER_INDEX
    this._emitter = new EventEmitter()
    this._interval = null
  }
  destroy () {
    this._emitter.removeAllListeners()
    this.stopPolling()
  }
  addEntry (entry) {
    this._buffer.push(entry)
    this._emitter.emit(ADD_ENTRY_EVENT, entry)
  }
  hasSameEntry (newEntry) {
    return this._buffer.readAll().some(
      (entry) => { return entry === newEntry }
    )
  }
  getEntries (startIndex = 0) {
    const index = startIndex * -1
    return this._buffer.readAll(index).reverse()
  }
  getNextEntries () {
    this._bufferIndex += 1
    return this.getEntries(this._bufferIndex)
  }
  resetIndex () {
    this._bufferIndex = INIT_BUFFER_INDEX
  }
  writeFirstEntry () {
    clipboard.writeText(this._buffer.read(this._bufferIndex))
  }
  startPolling () {
    this._interval = setInterval(() => {
      const entry = clipboard.readText()
      if (this.hasSameEntry(entry)) return

      this.addEntry(entry)
    }, this.intervalTime)
  }
  stopPolling () {
    clearInterval(this.interval)
  }
  // TODO: このイベント最終的に不要っぽければ消しておく
  onAddEntry (callback) {
    this.emitter.on(ADD_ENTRY_EVENT, callback)
  }
}

@@ -40,47 +40,8 @@
 r')%0A
-const EventEmitter = require('events')%0A
 cons
@@ -78,16 +78,16 @@
 ctron')%0A
+
 %0Aconst D
@@ -176,44 +176,8 @@
 = -1
-%0Aconst ADD_ENTRY_EVENT = 'add-entry'
 %0A%0Amo
@@ -467,47 +467,8 @@
 DEX%0A
-    this._emitter = new EventEmitter()%0A
     
@@ -512,47 +512,8 @@
 ) %7B%0A
-    this._emitter.removeAllListeners()%0A
     
@@ -589,55 +589,8 @@
 ry)%0A
-    this._emitter.emit(ADD_ENTRY_EVENT, entry)%0A
   %7D%0A
@@ -1326,24 +1326,25 @@
 terval(this.
+_
 interval)%0A  
@@ -1349,117 +1349,6 @@
   %7D%0A
-  // TODO: %E3%81%93%E3%81%AE%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E6%9C%80%E7%B5%82%E7%9A%84%E3%81%AB%E4%B8%8D%E8%A6%81%E3%81%A3%E3%81%BD%E3%81%91%E3%82%8C%E3%81%B0%E6%B6%88%E3%81%97%E3%81%A6%E3%81%8A%E3%81%8F%0A  onAddEntry (callback) %7B%0A    this.emitter.on(ADD_ENTRY_EVENT, callback)%0A  %7D%0A
 %7D%0A

remove unused method and fix typo