// `basic-ioc-container` example
import container from 'basic-ioc-container'
import serviceFactory, { Service } from './service'
import dbFactory, { Db } from './db'
import log from './console'
const { version: VERSION } = require('../package.json')
const DB_NAME = 'app-store'

// declare the shape of the IoC container
interface Container {
  version: string
  service: Service
  db: Db
  dbname: string
}

// instantiate a container of the above shape,
// and retrieve a corresponding factory registration function
const use = container()
// register factories from a map
use({
  version: () => VERSION, // register a constant
  service: serviceFactory
})
// or register factories individually
use('db', dbFactory)
use('dbname', () => DB_NAME)

// each of the above returns the container object, as well as:
const { service } = use() // pull service from container

service.save({ id: 'doc', foo: 'foo' })
.then(log('save:'))

// alternatively, inject directly (without registering):
use(({ version }) => log('version:')(version))