import Context from 'skypager-registry/lib/context'
import Helper from '../helper'

export class Importer extends Helper {
  static isCacheable = true

  static cacheable(setting = true) {
    return this.isCacheable = !!setting
  }

  static createRegistry = (options = {}) => new Context('importers', {
    context: require.context('../importers', false, /.js$/),
    ...options,
    sourceModule: {
      id: (module && module.id) || __filename,
      filename: __filename, dirname: __dirname,
      ...options.sourceModule || {},
    }    
  })

  static attach(project, options = {}) {
    return Helper.attach(project, Importer, {
      registryProp: 'importers',
      lookupProp: 'importer',
      registry: options.registry || Importer.createRegistry(),
      ...options,
    })
  }

  getSourceFileList() {
    return this.project.readdi
  }

  run(options = {}) {
    return Promise.resolve(this)
  }
}

export default Importer

export const attach = Importer.attach
