'use strict';

const index = require('./index-16c91445.js');
const dataModel = require('./data-model-f9df493f.js');
const filter$1 = require('./filter-52876f3c.js');
const filter = require('./filter-a24b968a.js');

class ItemDataProvider {
  constructor(apiData, params, table, query, fields, callback) {
    this.apiData = apiData;
    this.callback = callback;
    this.params = params;
    this.table = table;
    this.fields = fields;
    this.query = query;
  }
  fetchData(itemFetchState) {
    var _a;
    if (itemFetchState.isPending) {
      return;
    }
    this.callback(itemFetchState.beforeFetch());
    const { name } = this.table;
    const filter$2 = ((_a = this.query) === null || _a === void 0 ? void 0 : _a.filter) ? filter.replaceParamsInFilter(filter.cloneFilter(this.query.filter), this.params)
      : {
        comparison: filter$1.Comparison.Eq,
        id: '_id',
        value: this.params.itemId,
      };
    this.apiData.query(name, {
      fields: [...this.fields, new index.TextField('_id', 'ID')],
      filter: filter$2,
      limit: 1,
      skip: 0,
    }).then(response => {
      this.callback(itemFetchState.afterFetch(response.results.length > 0 ? response.results[0] : new dataModel.DataModel()));
    }, error => {
      this.callback(itemFetchState.afterFetchError(error));
    });
  }
}

exports.ItemDataProvider = ItemDataProvider;
