export class Config {
  constructor() {
    this.m = new Map();
  }
  reset(configObj) {
    this.m = new Map(Object.entries(configObj));
  }
  get(key, fallback) {
    const value = this.m.get(key);
    return value !== undefined ? value : fallback;
  }
  getBoolean(key, fallback = false) {
    const val = this.m.get(key);
    if (val === undefined) {
      return fallback;
    }
    if (typeof val === 'string') {
      return val === 'true';
    }
    return !!val;
  }
  getNumber(key, fallback) {
    const val = parseFloat(this.m.get(key));
    return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val;
  }
  set(key, value) {
    this.m.set(key, value);
  }
}
export const config = /*@__PURE__*/ new Config();
export const configFromSession = (win) => {
  try {
    const configStr = win.sessionStorage.getItem(GASCO_SESSION_KEY);
    return configStr !== null ? JSON.parse(configStr) : {};
  }
  catch (e) {
    return {};
  }
};
export const saveConfig = (win, c) => {
  try {
    win.sessionStorage.setItem(GASCO_SESSION_KEY, JSON.stringify(c));
  }
  catch (e) {
    return;
  }
};
export const configFromURL = (win) => {
  const configObj = {};
  win.location.search
    .slice(1)
    .split('&')
    .map((entry) => entry.split('='))
    .map(([key, value]) => [decodeURIComponent(key), decodeURIComponent(value)])
    .filter(([key]) => startsWith(key, GASCO_PREFIX))
    .map(([key, value]) => [key.slice(GASCO_PREFIX.length), value])
    .forEach(([key, value]) => {
    configObj[key] = value;
  });
  return configObj;
};
const startsWith = (input, search) => {
  return input.substring(0, search.length) === search;
};
const GASCO_PREFIX = 'gasco:';
const GASCO_SESSION_KEY = 'gasco-persist-config';
