{
  "version": 3,
  "sources": ["../src/index.ts", "../src/api.ts"],
  "sourcesContent": ["export {default as default} from './api';\n", "// Copyright (c) 2017 Uber Technologies, Inc.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n\ndeclare global {\n  var __SEER_INITIALIZED__: boolean; // eslint-disable-line no-var\n}\n\ntype Callback = (...args: unknown[]) => unknown;\ntype Listener = any;\n\nconst isBrowser = typeof window !== 'undefined' && window.addEventListener;\n\nconst timers = new Map();\n\n\nconst listeners = new Map<string, Listener>();\n\n/**\n * Ready check for Seer initialization\n * @returns\n */\nconst isReady = (): boolean => isBrowser && globalThis.__SEER_INITIALIZED__;\n\n/**\n * Utility method allowing to throttle a user action based on a key and a minimun delay.\n *\n * @param key  A unique key\n * @param delay { The minimal delay to throttle\n * @returns\n */\nconst throttle = (key: string, delay: number): boolean => {\n  const time = timers.get(key);\n  const now = Date.now();\n  if (time && now - time < delay) {\n    return true;\n  }\n  timers.set(key, now);\n  return false;\n};\n\nconst replacer =\n  (seen: Set<unknown>) =>\n    (key: string, value: unknown): unknown => {\n      if (value && typeof value === 'object' && seen.has(value)) {\n        return undefined;\n      }\n      seen.add(value);\n      const isArray = Object.prototype.toString.call(value).slice(8, -1).includes('Array');\n      if (isArray) {\n        return Array.prototype.slice.call(value, 0, 20);\n      }\n      return value;\n    };\n\n/**\n * Low-level api leveraging window.postMessage\n *\n * @param type The action type\n * @param data The action payload\n */\nconst send = (type: string, data: unknown = {}): void => {\n  if (!isBrowser || !isReady()) {\n    return;\n  }\n\n  const seen = new Set();\n  const payload = JSON.stringify(data, replacer(seen));\n\n  try {\n    window.postMessage({type, payload, source: 'seer-agent'}, '*');\n  } catch (e) {\n    if (throttle('seer-log', 2e3)) {\n      return;\n    }\n    console.log(e); // eslint-disable-line\n  }\n};\n\nconst listener = (message: any): void => {\n  if (!message || !message.data || message.data.source !== 'seer-core') {\n    return;\n  }\n  const {type, payload} = message.data;\n\n  const typeListeners = listeners.get(type);\n  if (typeListeners) {\n    typeListeners.forEach((cb: Callback) => cb(payload));\n  }\n};\n\n/**\n * Initilize window listener. There will be only one for the whole process\n * to prevent too many registrations.\n *\n * This method will be called automatically if you use the `listenFor` method.\n */\nconst init = (): void => {\n  // @ts-expect-error\n  if (!isBrowser || window.__SEER_LISTENER__) {\n    return;\n  }\n  window.addEventListener('message', listener);\n  // @ts-expect-error\n  window.__SEER_LISTENER__ = true;\n};\n\n/**\n * Clean listener. Can be useful in case you want to unregister upcoming events\n * or liberate memory.\n */\nconst clean = (): void => {\n  // @ts-expect-error\n  if (!isBrowser || !window.__SEER_LISTENER__) {\n    return;\n  }\n  window.removeEventListener('message', listener);\n  // @ts-expect-error\n  delete window.__SEER_LISTENER__;\n};\n\n/**\n * Create a listener that will be called upon events of the given key.\n *\n * @param type The unique tab key\n * @param cb A callback that will receive the message payload\n */\nconst listenFor = (type: string, cb: Callback): void => {\n  if (!isBrowser) {\n    return;\n  }\n  if (!type || !cb) {\n    throw new Error('Please provide a type and callback');\n  }\n  if (!listeners.has(type)) {\n    listeners.set(type, []);\n  }\n  // @ts-expect-error\n  if (!window.__SEER_LISTENER__) {\n    init();\n  }\n  listeners.get(type).push(cb);\n};\n\n/**\n * Remove an identity listener\n *\n * @param cb The callback to remove\n */\nconst removeListener = (cb: Callback): void => {\n  listeners.forEach((typeListeners, key) => {\n    listeners.set(\n      key,\n      typeListeners.filter((l: Listener) => l !== cb)\n    );\n  });\n};\n\n/**\n * Creates a new indexed list.\n * It works by index to get O(1) accessing and performance.\n *\n * @param key  The key of the tab\n * @param data The indexed object\n */\nconst list = (key: string, data: object): void => send('LIST', {key, data});\n\n/**\n * Creates an element in the indexed list, based on the itemKey.\n *\n * @param key The key of the tab\n * @param itemKey The key of the item\n * @param data The value of the item\n */\nconst listItem = (key: string, itemKey: string, data: unknown = {}): void =>\n  send('LIST_ITEM', {key, itemKey, data});\n\n/**\n * Update an item property, can be deeply nested.\n *\n * @param key The key of the tab\n * @param itemKey The key of the item\n * @param path The path of the variable you want to update\n * @param data {Object} The new value\n */\nconst updateItem = (key: string, itemKey: string, path: string, data: unknown): void =>\n  send('UPDATE_ITEM', {key, itemKey, path, data});\n\n/**\n * Similar to updateItem, but allows to pass an array with {path,data} pairs for\n * multiple update of the same item without having to send multiple messages.\n *\n * @param key The key of the tab\n * @param itemKey The key of the item\n * @param array {Array} The array of updates\n * @param array.path The path for this update\n * @param array.data {Object} The value of this update\n */\nconst multiUpdate = (key: string, itemKey: string, array: string): void =>\n  send('MULTI_UPDATE_ITEM', {key, itemKey, array});\n\n/**\n * Remove a specific item in a specific tab.\n *\n * @param key They key of the tab\n * @param itemKey The key of the item\n */\nconst deleteItem = (key: string, itemKey: string): void => send('DELETE_ITEM', {key, itemKey});\n\n/**\n * Will create a log message to an item, that will be displayde with the current time.\n *\n * @param key The key of the tab\n * @param itemKey The key of the item\n * @param msg The message to display\n */\nconst addLog = (key: string, itemKey: string, msg: string) => send('ADD_LOG', {key, itemKey, msg});\n\nexport default {\n  send,\n  throttle,\n  isReady,\n\n  list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