'use strict';

const kimlikApi = require('./kimlikApi-1cd43caf.js');
const dataTransferField = require('./data-transfer-field-0ce05c1a.js');
const filter = require('./filter-d5040f65.js');

class DataApiImpl {
  constructor(endpoint, defaultHeaders = {}) {
    this.query = (collection, query) => this.api.post(`${collection}/query`, query);
    this.get = (collection, id, query) => new Promise((resolve, reject) => this.query(collection, {
      fields: [...(query.fields || []), new dataTransferField.DataTransferField('_id')],
      filter: {
        comparison: filter.Comparison.Eq,
        id: '_id',
        value: id,
      },
      limit: 1,
      skip: 0,
    }).then(response => resolve(response.results.pop()))
      .catch(error => reject(error)));
    this.update = (collection, id, body) => this.api.post(`${collection}/${id}/update`, body);
    this.addAuthInterceptor = (token) => {
      this.api.addAuthInterceptor(token);
    };
    this.api = new kimlikApi.APIBase(endpoint, defaultHeaders);
  }
}

exports.DataApiImpl = DataApiImpl;
