/* Copyright © 2020 Splunk Inc.
SPLUNK CONFIDENTIAL – Use or disclosure of this material in whole or
in part without a valid written license from Splunk Inc. is PROHIBITED. */
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};

// src/GeoRegistry.js
var GeoRegistry_exports = {};
__export(GeoRegistry_exports, {
  default: () => GeoRegistry_default
});
module.exports = __toCommonJS(GeoRegistry_exports);

// esm-externals:@splunk/ui-utils/i18n
var i18n_exports = {};
__export(i18n_exports, {
  default: () => i18n_default
});
var defaultImport = __toESM(require("@splunk/ui-utils/i18n"));
__reExport(i18n_exports, require("@splunk/ui-utils/i18n"));
var i18n_default = "default" in defaultImport ? defaultImport.default : defaultImport;

// esm-externals:@splunk/visualization-context/Provider
var Provider_exports = {};
__export(Provider_exports, {
  default: () => Provider_default
});
var defaultImport2 = __toESM(require("@splunk/visualization-context/Provider"));
__reExport(Provider_exports, require("@splunk/visualization-context/Provider"));
var Provider_default = "default" in defaultImport2 ? defaultImport2.default : defaultImport2;

// src/BaseRegistry.js
var BaseRegistry = class _BaseRegistry {
  constructor() {
    this.cachedProviders = {};
    this.defaultProvider = null;
  }
  static create() {
    return new _BaseRegistry();
  }
  /**
   * check whether user defined provider extends the base provider
   * @param {Provider} provider
   * @returns {Boolean}
   */
  isValidProvider(provider) {
    if (provider instanceof Provider_default) {
      return true;
    }
    throw new Error((0, i18n_exports._)("Invalid Provider"));
  }
  /**
   * add default provider to cache
   * @param {Provider} provider
   */
  addDefaultProvider(provider) {
    if (this.isValidProvider(provider)) {
      this.addProvider(provider);
      this.defaultProvider = provider;
    }
  }
  /**
   * add provider to cache
   * @param {Provider} provider
   */
  addProvider(provider) {
    if (this.isValidProvider(provider)) {
      if (this.cachedProviders[provider.getType()]) {
        console.warn(`The provider "${provider.getType()}" existed already.`);
      }
      this.cachedProviders[provider.getType()] = provider;
    }
  }
  /**
   * delete provider
   * @param {String} providerType
   */
  deleteProvider(providerType) {
    const cachedProvider = this.cachedProviders[providerType];
    if (!cachedProvider) {
      console.warn(`The provider "${providerType}" does not existed`);
      return;
    }
    if (this.defaultProvider && providerType === this.defaultProvider.getType()) {
      this.defaultProvider = null;
    }
    delete this.cachedProviders[providerType];
  }
  /**
   * find provider from cache
   * @param {String} type
   * @returns {Provider} provider
   */
  findProvider(type) {
    const provider = this.cachedProviders[type];
    if (provider) {
      return provider;
    }
    throw new Error((0, i18n_exports._)("Provider not found"));
  }
  /**
   * list all providers
   * @return {Object} provider = { type: 'local', isDefault: true }
   */
  listProviders() {
    return Object.keys(this.cachedProviders).map((type) => ({
      type,
      isDefault: type === this.defaultProvider.getType()
    }));
  }
  /**
   * check whether src is a http url or a url used for icon/image-registry (e.g. 'kvstore://image123')
   */
  isResourceURL(url) {
    if (!url || url.indexOf("://") < 0) {
      return false;
    }
    const { type } = this.parseURL(url);
    return type !== "http" && type !== "https";
  }
  /**
   * parse provider type and id from the url
   * @params {String} url     'kvstore://image123'
   * @returns {Object} obj
   * @returns {String} obj.type   'kvstore'
   * @returns {String} obj.id     'image123'
   */
  parseURL(url) {
    if (url) {
      const [type, id] = url.split("://");
      if (type && id) {
        return { type, id };
      }
    }
    throw new Error((0, i18n_exports._)("Invalid url"));
  }
  /**
   * upload
   * @param {DataURI} dataURI
   * @param {Object} metaData
   * @param {String} providerType      e.g. 'kvstore' or 'sui' or 'local'
   * @returns {String} id               e.g. 'kvstore://image123'
   */
  upload(dataURI, metaData, providerType) {
    return __async(this, null, function* () {
      if (providerType == null && this.defaultProvider == null) {
        throw new Error((0, i18n_exports._)("No valid provider was found. Please add a provider"));
      }
      const provider = providerType ? this.findProvider(providerType) : this.defaultProvider;
      const id = yield provider.upload(dataURI, metaData);
      return `${provider.getType()}://${id}`;
    });
  }
  /**
   * get object by url
   * @param {String} url     'kvstore://image123'
   * @returns {Object} obj    e.g. an image object or icon
   */
  getByURL(url) {
    return __async(this, null, function* () {
      const { type, id } = this.parseURL(url);
      const provider = this.findProvider(type);
      const obj = yield provider.getById(id);
      return obj;
    });
  }
  /**
   * delete object by url
   * @param {String} url     'kvstore://image123' || 'sui://icon123'
   * @returns {Object} obj    e.g. an image object or icon
   */
  deleteByURL(url) {
    return __async(this, null, function* () {
      const { type, id } = this.parseURL(url);
      const provider = this.findProvider(type);
      const obj = yield provider.deleteById(id);
      return obj;
    });
  }
  /**
   * list all object ids
   * @params {String} providerType       e.g. 'kvstore' or 'sui'
   * @params {Object} options
   * @returns {Array} ids
   */
  listIds(_0) {
    return __async(this, arguments, function* (providerType, options = { offset: 0, count: 100 }) {
      const provider = this.findProvider(providerType);
      const ids = yield provider.listIds(options);
      return ids;
    });
  }
};
var BaseRegistry_default = BaseRegistry;

// src/GeoRegistry.js
var GeoRegistry = class _GeoRegistry extends BaseRegistry_default {
  static create() {
    return new _GeoRegistry();
  }
  /*
   * parse url
   * @params {String} url  the valid url should be geo://default/us
   * @return {Object}
   *         {String} providerType  'geo'
   *         {String} storageType 'default' for now // todo: add more in the future if geojson are allowed to save in other storages such as 'kvstore'
   *         {String} mapId 'us' or 'world' for now
   */
  parseURL(url) {
    if (!/.+:\/\/.*\/.+/.test(url)) {
      throw new Error((0, i18n_exports._)("Invalid url."));
    }
    const [providerType, geoId] = url.split("://");
    const [storageType, mapId] = geoId.split("/");
    return { providerType, storageType, mapId };
  }
  /*
   * get geoJson by url
   * @params {String} url  e.g. geo://default/us
   * @return {Json} geoJson
   */
  getByURL(url) {
    try {
      const { providerType, storageType, mapId } = this.parseURL(url);
      const provider = this.findProvider(providerType);
      const geoJson = provider.getById({ storageType, mapId });
      return geoJson;
    } catch (error) {
      throw new Error((0, i18n_exports._)("Cannot get geoJson data."));
    }
  }
  // Todo: return map ids from storage
  /*
   * list all available geojson ids
   * @params {Object}
   * @params {String} providerType
   * @params {Object} options
   * @return {Array} ids [{id: "us", url: 'geo://default/us'}, {id: 'world', url: 'geo://default/world'}]
   */
  listIds({ providerType = "geo", options = { offset: 0, count: 100 } }) {
    const provider = this.findProvider(providerType);
    const ids = provider.listIds(options);
    return ids;
  }
  // Todo: be able to upload geoJson in the future
  upload() {
  }
  // Todo: be able to delete geoJson in the future
  deleteByURL() {
  }
};
var GeoRegistry_default = GeoRegistry;
//# sourceMappingURL=GeoRegistry.js.map
