import { D as DataModel } from './data-model-bb5789f5.js';

class ListFetchState {
  constructor(data, meta, isPending = false, error) {
    this.beforeFetch = () => new ListFetchState(this.data, this.meta, true, undefined);
    this.afterFetch = (response, append = true) => new ListFetchState((this.data && append) ? [...this.data, ...DataModel.decodeList(response.results)] : DataModel.decodeList(response.results), this.meta || response.meta, // keep existing meta if exists because the later ones don't have total
    false, undefined);
    this.afterFetchError = (error) => new ListFetchState(this.data, this.meta, false, error);
    this.afterUpdateItem = (itemId, data) => new ListFetchState(this.data ? [...this.data].map(item => {
      if (item._id === itemId) {
        return (Object.assign(Object.assign({}, item), data));
      }
      return item;
    }) : this.data, this.meta, false, undefined);
    this.isEndReached = () => (this.data && this.meta) ? this.data.length >= this.meta.total : false;
    this.data = data;
    this.error = error;
    this.isPending = isPending;
    this.meta = meta;
  }
}
ListFetchState.init = () => new ListFetchState();

export { ListFetchState as L };
