'use strict';

const getState = () => {
  const { zoom, state, topmost } = Object.fromEntries(new URLSearchParams(location.search));
  const { limit, debug, trace, display, insertion, visibility, selectorPreview } = parseJSON(state, null, {});
  return {
    limit: defaults(limit, DEFAULTS['limit']),
    debug: defaults(debug, DEFAULTS['debug']),
    zoom: zoom != null ? +zoom : DEFAULTS['zoom'],
    display: defaults(display, DEFAULTS['display']),
    trace: trace != null ? trace : DEFAULTS['trace'],
    visibility: defaults(visibility, DEFAULTS['visibility']),
    topmost: topmost != null ? topmost === 'true' : DEFAULTS['topmost'],
    smart: insertion != null ? !insertion['visible'] : DEFAULTS['smart'],
    selectorPreview: defaults(selectorPreview, DEFAULTS['selectorPreview']),
  };
};

const preventEvent = (e) => e.preventDefault();

const parseJSON = (text, reviver, defaultValue = {}) => {
  try {
    return JSON.parse(text, reviver);
  } catch {
    return defaultValue;
  }
};

const cloneDeep = (object) => JSON.parse(JSON.stringify(object));

const defaults = (object, defaults) => Object.assign({}, defaults, object);

const html = (raw, ...keys) => (keys.length === 0 ? raw[0] : String.raw({ raw }, ...keys));

const post = (data = []) => BrowserAutomationStudio_TriggerEvent('scenario.settings', JSON.stringify(data));
