import { Action } from "@sankhyalabs/core";
import { DISTINCT_FILTER_NAME_PREFIX, EZ_GRID_LOADING_SOURCE } from "../../../../utils/constants";
import { DataUnit } from "@sankhyalabs/core";
export class DataSourceInterceptor {
  constructor(controller) {
    this._controller = controller;
  }
  clearColumnFilter(filters) {
    var _a;
    return (_a = filters.filter(filter => !filter.name.startsWith(DISTINCT_FILTER_NAME_PREFIX))) !== null && _a !== void 0 ? _a : [];
  }
  interceptAction(action) {
    var _a;
    if (action.type === Action.LOADING_DATA) {
      if (EZ_GRID_LOADING_SOURCE !== action.payload.source && DataUnit.CHANGING_PAGE_LOADING_SOURCE !== action.payload.source) {
        action.payload.filters = ((_a = action.payload) === null || _a === void 0 ? void 0 : _a.filters) !== undefined ? this.clearColumnFilter(action.payload.filters) : action.payload.filters;
        this._controller.clearFilter();
      }
    }
    return action;
  }
}
