/*!
 * Copyright © 2018 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.
 */
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId]) {
/******/ 			return installedModules[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ 		}
/******/ 	};
/******/
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/
/******/ 	// create a fake namespace object
/******/ 	// mode & 1: value is a module id, require it
/******/ 	// mode & 2: merge all properties of value into the ns
/******/ 	// mode & 4: return value when already ns object
/******/ 	// mode & 8|1: behave like require
/******/ 	__webpack_require__.t = function(value, mode) {
/******/ 		if(mode & 1) value = __webpack_require__(value);
/******/ 		if(mode & 8) return value;
/******/ 		if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ 		var ns = Object.create(null);
/******/ 		__webpack_require__.r(ns);
/******/ 		Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ 		if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ 		return ns;
/******/ 	};
/******/
/******/ 	// getDefaultExport function for compatibility with non-harmony modules
/******/ 	__webpack_require__.n = function(module) {
/******/ 		var getter = module && module.__esModule ?
/******/ 			function getDefault() { return module['default']; } :
/******/ 			function getModuleExports() { return module; };
/******/ 		__webpack_require__.d(getter, 'a', getter);
/******/ 		return getter;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = 26);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

module.exports = require("@splunk/ui-utils/i18n");

/***/ }),
/* 1 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/regenerator");

/***/ }),
/* 2 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/classCallCheck");

/***/ }),
/* 3 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/createClass");

/***/ }),
/* 4 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/asyncToGenerator");

/***/ }),
/* 5 */
/***/ (function(module, exports) {

module.exports = require("@splunk/visualization-context/Provider");

/***/ }),
/* 6 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/getPrototypeOf");

/***/ }),
/* 7 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/possibleConstructorReturn");

/***/ }),
/* 8 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/inherits");

/***/ }),
/* 9 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/defineProperty");

/***/ }),
/* 10 */,
/* 11 */,
/* 12 */,
/* 13 */
/***/ (function(module, exports) {

module.exports = require("@babel/runtime/helpers/objectSpread");

/***/ }),
/* 14 */,
/* 15 */
/***/ (function(module, exports) {

module.exports = require("uuid-v4");

/***/ }),
/* 16 */,
/* 17 */,
/* 18 */,
/* 19 */,
/* 20 */,
/* 21 */,
/* 22 */,
/* 23 */,
/* 24 */,
/* 25 */,
/* 26 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b64EncodeUnicode", function() { return b64EncodeUnicode; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "KEY", function() { return KEY; });
/* harmony import */ var _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
/* harmony import */ var _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(9);
/* harmony import */ var _babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _babel_runtime_helpers_objectSpread__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(13);
/* harmony import */ var _babel_runtime_helpers_objectSpread__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_objectSpread__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4);
/* harmony import */ var _babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var _babel_runtime_helpers_classCallCheck__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(2);
/* harmony import */ var _babel_runtime_helpers_classCallCheck__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_classCallCheck__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var _babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(3);
/* harmony import */ var _babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_5__);
/* harmony import */ var _babel_runtime_helpers_possibleConstructorReturn__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(7);
/* harmony import */ var _babel_runtime_helpers_possibleConstructorReturn__WEBPACK_IMPORTED_MODULE_6___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_possibleConstructorReturn__WEBPACK_IMPORTED_MODULE_6__);
/* harmony import */ var _babel_runtime_helpers_getPrototypeOf__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(6);
/* harmony import */ var _babel_runtime_helpers_getPrototypeOf__WEBPACK_IMPORTED_MODULE_7___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_getPrototypeOf__WEBPACK_IMPORTED_MODULE_7__);
/* harmony import */ var _babel_runtime_helpers_inherits__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(8);
/* harmony import */ var _babel_runtime_helpers_inherits__WEBPACK_IMPORTED_MODULE_8___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_inherits__WEBPACK_IMPORTED_MODULE_8__);
/* harmony import */ var uuid_v4__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(15);
/* harmony import */ var uuid_v4__WEBPACK_IMPORTED_MODULE_9___default = /*#__PURE__*/__webpack_require__.n(uuid_v4__WEBPACK_IMPORTED_MODULE_9__);
/* harmony import */ var _splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(0);
/* harmony import */ var _splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10___default = /*#__PURE__*/__webpack_require__.n(_splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10__);
/* harmony import */ var _splunk_visualization_context_Provider__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(5);
/* harmony import */ var _splunk_visualization_context_Provider__WEBPACK_IMPORTED_MODULE_11___default = /*#__PURE__*/__webpack_require__.n(_splunk_visualization_context_Provider__WEBPACK_IMPORTED_MODULE_11__);










/* eslint-disable class-methods-use-this,no-useless-constructor */



var b64EncodeUnicode = function b64EncodeUnicode(str) {
  return (// first we use encodeURIComponent to get percent-encoded UTF-8,
    // then we convert the percent encodings into raw bytes which
    // can be fed into btoa.
    btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
      return String.fromCharCode(parseInt("0x".concat(p1), 16));
    }))
  );
};
var KEY = '__icons__';
/**
 * a local icon provider
 */

var LocalIconProvider =
/*#__PURE__*/
function (_Provider) {
  _babel_runtime_helpers_inherits__WEBPACK_IMPORTED_MODULE_8___default()(LocalIconProvider, _Provider);

  function LocalIconProvider() {
    _babel_runtime_helpers_classCallCheck__WEBPACK_IMPORTED_MODULE_4___default()(this, LocalIconProvider);

    return _babel_runtime_helpers_possibleConstructorReturn__WEBPACK_IMPORTED_MODULE_6___default()(this, _babel_runtime_helpers_getPrototypeOf__WEBPACK_IMPORTED_MODULE_7___default()(LocalIconProvider).call(this));
  }
  /**
   * return provider type
   * @return {String} 'local'
   */


  _babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_5___default()(LocalIconProvider, [{
    key: "getType",
    value: function getType() {
      return 'local';
    }
    /**
     * upload one new icon
     */

  }, {
    key: "upload",
    value: function () {
      var _upload = _babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_3___default()(
      /*#__PURE__*/
      _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.mark(function _callee(dataURI, metaData) {
        var response, text, parser, xmlDoc, svg, serializer, svgStr, iconDataURI, icon, id, icons;
        return _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return fetch(dataURI);

              case 2:
                response = _context.sent;
                _context.next = 5;
                return response.text();

              case 5:
                text = _context.sent;
                parser = new DOMParser();
                xmlDoc = parser.parseFromString(text, 'text/xml');
                svg = xmlDoc.getElementsByTagName('svg')[0];

                if (svg.getAttribute('viewBox')) {
                  _context.next = 11;
                  break;
                }

                throw new Error(Object(_splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10__["_"])('Please upload a valid svg with viewBox attribute.'));

              case 11:
                // reset svg attributes
                svg.setAttribute('preserveAspectRatio', 'none'); // convert back to dataURI

                serializer = new XMLSerializer();
                svgStr = b64EncodeUnicode(serializer.serializeToString(svg));
                iconDataURI = "data:image/svg+xml;base64,".concat(svgStr); // save to localStorage

                _context.prev = 15;
                icon = {
                  dataURI: iconDataURI,
                  metaData: metaData
                };
                id = uuid_v4__WEBPACK_IMPORTED_MODULE_9___default()();
                icons = JSON.parse(localStorage.getItem(KEY) || '{}');
                icons = _babel_runtime_helpers_objectSpread__WEBPACK_IMPORTED_MODULE_2___default()({}, icons, _babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_1___default()({}, id, icon));
                localStorage.setItem(KEY, JSON.stringify(icons));
                return _context.abrupt("return", id);

              case 24:
                _context.prev = 24;
                _context.t0 = _context["catch"](15);
                throw new Error(Object(_splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10__["_"])('The icon cannot be uploaded. Please check whether your localStorage is full.'));

              case 27:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, null, [[15, 24]]);
      }));

      function upload(_x, _x2) {
        return _upload.apply(this, arguments);
      }

      return upload;
    }()
    /**
     * get one icon by icon id
     */

  }, {
    key: "getById",
    value: function getById(id) {
      var icons = JSON.parse(localStorage.getItem(KEY) || '{}');

      if (icons[id]) {
        return icons[id];
      }

      throw new Error(Object(_splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10__["_"])('The icon is not found.'));
    }
    /**
     * get all icon ids
     * @params options
     * @params options.meta   a list of meta data attribute that would be returned with each id
     * @params options.search  a search string to filter the returned icons
     */

  }, {
    key: "listIds",
    value: function listIds(_ref) {
      var _ref$offset = _ref.offset,
          offset = _ref$offset === void 0 ? 0 : _ref$offset,
          _ref$count = _ref.count,
          count = _ref$count === void 0 ? 100 : _ref$count,
          _ref$meta = _ref.meta,
          meta = _ref$meta === void 0 ? [] : _ref$meta,
          _ref$search = _ref.search,
          search = _ref$search === void 0 ? null : _ref$search;

      try {
        var icons = JSON.parse(localStorage.getItem(KEY) || '{}');
        var results = [];
        Object.keys(icons).slice(offset, count).forEach(function (id) {
          var icon = {
            id: id
          };
          var iconMeta = icons[id].metaData || {}; // If search is a valid string, return the icon with matched meta data

          var isSearchedIcon = search != null && Object.keys(iconMeta).some(function (attr) {
            var iconMetaValue = iconMeta[attr].toLowerCase();
            return iconMetaValue.indexOf(search.toLowerCase()) > -1;
          });

          if (search == null || isSearchedIcon) {
            if (meta && meta.length) {
              icon.metaData = {};
              meta.forEach(function (attr) {
                if (iconMeta[attr] != null) {
                  icon.metaData[attr] = iconMeta[attr];
                }
              });
            }

            results.push(icon);
          }
        });
        return results;
      } catch (error) {
        throw new Error(Object(_splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10__["_"])('The icon ids cannot be listed.'));
      }
    }
    /**
     * delete an icon by icon id
     */

  }, {
    key: "deleteById",
    value: function deleteById(id) {
      try {
        var icon = this.getById(id);
        var icons = JSON.parse(localStorage.getItem(KEY));
        delete icons[id];
        localStorage.setItem(KEY, JSON.stringify(icons));
        return icon;
      } catch (error) {
        throw new Error(Object(_splunk_ui_utils_i18n__WEBPACK_IMPORTED_MODULE_10__["_"])('The icon cannot be deleted.'));
      }
    }
  }]);

  return LocalIconProvider;
}(_splunk_visualization_context_Provider__WEBPACK_IMPORTED_MODULE_11___default.a);

/* harmony default export */ __webpack_exports__["default"] = (LocalIconProvider);

/***/ })
/******/ ]);
//# sourceMappingURL=LocalIconProvider.js.map