export default class GCode {
  constructor(configString) {
    this.configString = configString
  }

  async parse(onProgress) {
    return new Promise((resolve, reject) => {
      const worker = new Worker('../gcode-worker.js')

      worker.onmessage = (event) => {
        const { type, progress, configObject } = event.data

        if (type === 'progress') {
          // Report progress to the caller
          if (onProgress) onProgress(progress)
        } else if (type === 'result') {
          resolve(configObject)
          worker.terminate()
        }
      }

      worker.onerror = (error) => {
        reject(error)
        worker.terminate()
      }

      worker.postMessage({ configString: this.configString })
    })
  }
}
