export class DataApiDemo {
  constructor(database, logger) {
    this.get = (collection, id, query) => {
      this.logger.debugGroup('get', { collection, id, query });
      const table = this.database[collection];
      if (!table) {
        this.logger.warnGroup('get: table not found', { collection, id, query });
        return Promise.reject(`DataApiDemo.query: Table "${collection}" not found`);
      }
      const item = table[id];
      if (!item) {
        this.logger.warnGroup('get: item not found', { collection, id, query });
        return Promise.reject(`DataApiDemo.query: Item "${item}" in "${collection}" not found`);
      }
      return Promise.resolve(item);
    };
    this.logger = logger.new('DataApiDemo').setColor('river');
    this.database = database;
  }
  addAuthInterceptor(token) {
    this.logger.debugGroup('addAuthInterceptor', token);
  }
  query(collection, query) {
    this.logger.debugGroup('query', { collection, query });
    const table = this.database[collection];
    if (!table) {
      this.logger.warnGroup('query: table not found', { collection, query });
      return Promise.reject(`DataApiDemo.query: Table "${collection}" not found`);
    }
    return Promise.resolve({
      results: Object.keys(table).map(id => table[id]).filter(m => m.config.name === collection),
      meta: { total: 2, query }
    });
  }
  update(_collection, _id, _body) {
    return Promise.reject({ status: 418 });
  }
}
