/* 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 __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
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);

// src/LocalImageProvider.js
var LocalImageProvider_exports = {};
__export(LocalImageProvider_exports, {
  KEY: () => KEY,
  default: () => LocalImageProvider_default
});
module.exports = __toCommonJS(LocalImageProvider_exports);

// esm-externals:uuid-v4
var uuid_v4_exports = {};
__export(uuid_v4_exports, {
  default: () => uuid_v4_default
});
var defaultImport = __toESM(require("uuid-v4"));
__reExport(uuid_v4_exports, require("uuid-v4"));
var uuid_v4_default = "default" in defaultImport ? defaultImport.default : defaultImport;

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

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

// src/LocalImageProvider.js
var KEY = "__images__";
var LocalImageProvider = class extends Provider_default {
  constructor() {
    super();
  }
  /**
   * return provider type
   */
  getType() {
    return "local";
  }
  /**
   * add a new image
   */
  upload(dataURI, metaData) {
    try {
      const id = uuid_v4_default();
      const imageObject = { dataURI, metaData };
      let images = JSON.parse(localStorage.getItem(KEY) || "{}");
      images = __spreadProps(__spreadValues({}, images), { [id]: imageObject });
      localStorage.setItem(KEY, JSON.stringify(images));
      return id;
    } catch (error) {
      throw new Error(
        (0, i18n_exports._)("The image cannot be uploaded. Please check whether your localStorage is full.")
      );
    }
  }
  /**
   * get image by imageID
   */
  getById(imageID) {
    const images = JSON.parse(localStorage.getItem(KEY) || "{}");
    const imageObject = images[imageID];
    if (imageObject) {
      return imageObject;
    }
    throw new Error((0, i18n_exports._)("The image is not found."));
  }
  /**
   * get all imageIDs
   */
  listIds({ offset, count }) {
    try {
      const images = JSON.parse(localStorage.getItem(KEY) || "{}");
      const imageIDS = Object.keys(images);
      return imageIDS.slice(offset, count);
    } catch (error) {
      throw new Error((0, i18n_exports._)("The image ids cannot be listed."));
    }
  }
  /**
   * delete an image by dataID
   */
  deleteById(imageID) {
    try {
      const imageObject = this.getById(imageID);
      const images = JSON.parse(localStorage.getItem(KEY));
      delete images[imageID];
      localStorage.setItem(KEY, JSON.stringify(images));
      return imageObject;
    } catch (error) {
      throw new Error((0, i18n_exports._)("The image cannot be deleted."));
    }
  }
};
var LocalImageProvider_default = LocalImageProvider;
//# sourceMappingURL=LocalImageProvider.js.map
