import { D as DataModel } from './data-model-f7ccf826.js';
import { C as Comparison } from './filter-cbaa3d7d.js';
import { r as replaceParamsInFilter, c as cloneFilter } from './filter-8542e288.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 = ((_a = this.query) === null || _a === void 0 ? void 0 : _a.filter)
      ? replaceParamsInFilter(cloneFilter(this.query.filter), this.params)
      : {
        comparison: Comparison.Eq,
        id: '_id',
        value: this.params.itemId,
      };
    const apiQuery = {
      fields: this.fields,
      filter,
      limit: 1,
      skip: 0,
    };
    this.apiData.query(name, apiQuery).then(response => {
      this.callback(itemFetchState.afterFetch(response.results.length > 0 ? response.results[0] : new DataModel()));
    }, error => {
      this.callback(itemFetchState.afterFetchError(error));
    });
  }
}

export { ItemDataProvider as I };
