import { ResourceFetcher } from './resource-fetcher';
import { DataFetcher } from '../DataFetcher';
export default class GridConfigFetcher extends ResourceFetcher {
  constructor() {
    super(...arguments);
    this.GRID_CONFIG_VERSION = "V3:";
  }
  async getDefaultConfig(resourceId) {
    var _a, _b;
    const payload = { 'requestBody': { 'gridConfig': { 'configName': `GrdCfgHtml5:${resourceId}` } } };
    const response = await DataFetcher.get().callServiceBroker('GridConfig.restoreGridConfigFromJson', JSON.stringify(payload));
    const columns = (_b = (_a = response === null || response === void 0 ? void 0 : response.gridConfig) === null || _a === void 0 ? void 0 : _a.field) !== null && _b !== void 0 ? _b : [];
    return { columns };
  }
  getConfig(gridName, resourceID) {
    const completePath = this.getPath(resourceID, gridName);
    return new Promise((resolve, reject) => {
      this.loadResource(completePath)
        .then(loadedResource => {
        let config = undefined;
        if (loadedResource) {
          config = JSON.parse(loadedResource);
        }
        resolve(config);
      }).catch((error) => {
        reject(error);
      });
    });
  }
  saveConfig(config, gridName, resourceID) {
    const completePath = this.getPath(resourceID, gridName);
    return new Promise((resolve, reject) => {
      this.saveResource(config, completePath)
        .then((resp) => {
        resolve(JSON.parse(resp.resource));
      })
        .catch((error) => {
        reject(error);
      });
    });
  }
  getPath(resourceID, name) {
    if (name) {
      return `cfg://grid/${this.GRID_CONFIG_VERSION}${resourceID}/${name}`;
    }
    return `cfg://grid/${this.GRID_CONFIG_VERSION}${resourceID}`;
  }
}
