import { FormConfigFetcher } from "../http/data-fetcher/fetchers/form-config-fetcher";
import GridConfigFetcher from "../http/data-fetcher/fetchers/grid-config-fetcher";
import FilterBarConfigFetcher from "../http/data-fetcher/fetchers/filter-bar-config-fetcher";
import PersonalizedFilterFetcher from "../http/data-fetcher/fetchers/personalized-filter-fetcher";
export var CONFIG_SOURCE;
(function (CONFIG_SOURCE) {
  CONFIG_SOURCE["form"] = "form";
  CONFIG_SOURCE["grid"] = "grid";
  CONFIG_SOURCE["filterBar"] = "filterBar";
})(CONFIG_SOURCE || (CONFIG_SOURCE = {}));
export class ConfigStorage {
  static addFilterBarLegacyConfig(configName, legacyConfigName) {
    this.validateResourceId(legacyConfigName);
    this.filterBarLegacyConfig.set(configName, legacyConfigName);
  }
  static addFormLegacyConfig(configName, legacyConfigName) {
    this.validateResourceId(legacyConfigName);
    this.formLegacyConfig.set(configName, legacyConfigName);
  }
  static addGridLegacyConfig(configName, legacyConfigName) {
    this.validateResourceId(legacyConfigName);
    this.gridLegacyConfig.set(configName, legacyConfigName);
  }
  static preload(resourceID, configName, legacyConfigs) {
    if (configName && (legacyConfigs === null || legacyConfigs === void 0 ? void 0 : legacyConfigs.gridLegacyConfig)) {
      ConfigStorage.addGridLegacyConfig(configName, legacyConfigs.gridLegacyConfig);
    }
    if (configName && (legacyConfigs === null || legacyConfigs === void 0 ? void 0 : legacyConfigs.formLegacyConfig)) {
      ConfigStorage.addFormLegacyConfig(configName, legacyConfigs.formLegacyConfig);
    }
    ConfigStorage.loadFormConfig(configName, resourceID);
    ConfigStorage.loadGridConfig(configName, resourceID);
  }
  static async loadFilterBarConfig(name, resourceID, urlParams) {
    urlParams = this.handleLegacyConfig(name, CONFIG_SOURCE.filterBar, urlParams);
    const cacheID = ConfigStorage.buildCacheID(name, CONFIG_SOURCE.filterBar, resourceID);
    if (!ConfigStorage.configById.has(cacheID)) {
      ConfigStorage.configById.set(cacheID, ConfigStorage.filterBarConfigFetcher.getConfig(resourceID, name, urlParams));
    }
    return ConfigStorage.configById.get(cacheID);
  }
  static async loadFormConfig(name, resourceID) {
    name = this.handleLegacyConfigAsString(name, CONFIG_SOURCE.form);
    const cacheID = ConfigStorage.buildCacheID(name, CONFIG_SOURCE.form, resourceID);
    if (!ConfigStorage.configById.has(cacheID)) {
      ConfigStorage.configById.set(cacheID, ConfigStorage.formConfigFetcher.loadFormConfig(name, resourceID));
    }
    return ConfigStorage.configById.get(cacheID);
  }
  static async loadGridConfig(name, resourceID) {
    name = this.handleLegacyConfigAsString(name, CONFIG_SOURCE.grid);
    const cacheID = ConfigStorage.buildCacheID(name, CONFIG_SOURCE.grid, resourceID);
    if (!ConfigStorage.configById.has(cacheID)) {
      ConfigStorage.configById.set(cacheID, ConfigStorage.gridConfigFetcher.getConfig(name, resourceID));
    }
    return ConfigStorage.configById.get(cacheID);
  }
  static async saveFilterBarConfig(config, name, resourceID) {
    await this.deleteFilterBarConfigCache(name, resourceID);
    return this.filterBarConfigFetcher.saveConfig(config, resourceID, name);
  }
  static async saveFormConfig(config, name, resourceID) {
    name = this.handleLegacyConfigAsString(name, CONFIG_SOURCE.form);
    await this.deleteFormConfigCache(name, resourceID);
    return this.formConfigFetcher.saveConfig(config, name, resourceID);
  }
  static async saveCardState(config, name, resourceID) {
    return this.formConfigFetcher.saveCardState(config, name, resourceID);
  }
  static async getDefaultGridConfig(resourceId) {
    return await this.gridConfigFetcher.getDefaultConfig(resourceId);
  }
  static async saveGridConfig(config, name, resourceID) {
    if (config == undefined) {
      return;
    }
    name = this.handleLegacyConfigAsString(name, CONFIG_SOURCE.grid);
    await this.deleteGridConfigCache(name, resourceID);
    return this.gridConfigFetcher.saveConfig(config, name, resourceID);
  }
  static async loadDefaultFilter(filterId, resourceID, configName) {
    const legacyConfigName = this.filterBarLegacyConfig.get(configName);
    return this.personalizedFilterFetcher.loadDefaultFilter(resourceID, filterId, configName, legacyConfigName);
  }
  static async loadPersonalizedFilter(filterId, resourceID, configName) {
    const legacyConfigName = this.filterBarLegacyConfig.get(configName);
    return this.personalizedFilterFetcher.loadPersonalizedFilter(resourceID, filterId, configName, legacyConfigName);
  }
  static async savePersonalizedFilter(personalizedFilter, resourceID, configName) {
    //Ao criar ou alterar um filtro personalizado,
    //precisamos remover o cache do status da filterbar.
    await this.deleteFilterBarConfigCache(configName, resourceID);
    return this.personalizedFilterFetcher.savePersonalizedFilter(personalizedFilter, resourceID, configName);
  }
  static async saveDefaultFilter(personalizedFilter, resourceID, configName) {
    await this.deleteFilterBarConfigCache(configName, resourceID);
    return this.personalizedFilterFetcher.saveDefaultFilter(personalizedFilter, resourceID, configName);
  }
  static async removeDefaultFilter(personalizedFilter, resourceID, configName) {
    return this.personalizedFilterFetcher.removeDefaultFilter(personalizedFilter, resourceID, configName);
  }
  static async removePersonalizedFilter(personalizedFilter, resourceID, configName) {
    return this.personalizedFilterFetcher.removePersonalizedFilter(personalizedFilter, resourceID, configName);
  }
  static async validatePersonalizedFilter(dataUnitName, expression) {
    return this.personalizedFilterFetcher.validatePersonalizedFilter(dataUnitName, expression);
  }
  static async deleteGridConfigCache(name, resourceID) {
    await this.deleteConfigCache(name, resourceID, CONFIG_SOURCE.grid);
  }
  static async deleteFormConfigCache(name, resourceID) {
    await this.deleteConfigCache(name, resourceID, CONFIG_SOURCE.form);
  }
  static async deleteFilterBarConfigCache(name, resourceID) {
    await this.deleteConfigCache(name, resourceID, CONFIG_SOURCE.filterBar);
  }
  static validateResourceId(resourceId) {
    if (!resourceId)
      throw Error(this.LEGACY_CONFIG_NAME_ERROR_MESSAGE);
  }
  static handleLegacyConfig(configName, configSource, urlParams) {
    let legacyConfigName;
    switch (configSource) {
      case CONFIG_SOURCE.filterBar:
        legacyConfigName = this.filterBarLegacyConfig.get(configName);
        break;
      case CONFIG_SOURCE.grid:
        legacyConfigName = this.gridLegacyConfig.get(configName);
        break;
      case CONFIG_SOURCE.form:
        legacyConfigName = this.formLegacyConfig.get(configName);
    }
    const params = urlParams ? Object.assign({}, urlParams) : {};
    if (legacyConfigName) {
      params['legacyResourceID'] = legacyConfigName;
    }
    return params;
  }
  static handleLegacyConfigAsString(name, source) {
    const params = this.handleLegacyConfig(name, source);
    const legacyResourceID = params['legacyResourceID'];
    if (legacyResourceID) {
      name = name + `?legacyResourceID=${legacyResourceID}`;
    }
    return name;
  }
  static async deleteConfigCache(name, resourceID, source) {
    const cacheID = ConfigStorage.buildCacheID(name, source, resourceID);
    this.configById.delete(cacheID);
  }
  static buildCacheID(name, source, resourceID) {
    if (name == undefined) {
      return `req_${source}_${resourceID}`;
    }
    return `req_${source}_${name}_${resourceID}`;
  }
}
ConfigStorage.LEGACY_CONFIG_NAME_ERROR_MESSAGE = "LegacyConfigName precisa ser informado.";
ConfigStorage.filterBarLegacyConfig = new Map();
ConfigStorage.formLegacyConfig = new Map();
ConfigStorage.gridLegacyConfig = new Map();
ConfigStorage.configById = new Map();
ConfigStorage.filterBarConfigFetcher = new FilterBarConfigFetcher();
ConfigStorage.formConfigFetcher = new FormConfigFetcher();
ConfigStorage.gridConfigFetcher = new GridConfigFetcher();
ConfigStorage.personalizedFilterFetcher = new PersonalizedFilterFetcher();
