import { r as registerInstance } from './index-c269755a.js';

const Countries = {
  DK: 'Denmark',
  EE: 'Estonia',
  FI: 'Finland',
  LT: 'Lithuania',
  NO: 'Norway',
  SE: 'Sweden',
  Global: 'Global',
};
Object.values(Countries);

const API_STATS = 'https://stats.api.voca.teliacompany.com/components';
const TeliaVocaStats = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    /** Name of the project. Project field is unique and required. To ensure uniqueness of this field,
     * please input it in correct format `'[projectsName]-[countryCode]'`. E.g., `'b2c-fi'`, `'b2c-ecom-fi'`, `'b2b-se'` */
    this.project = '';
    /** Country initials of the project, this prop is provided by the project that uses Voca DS.
     * Possible values: 'DK' | 'EE' | 'FI' | 'LT' | 'NO' | 'SE' | 'Global'. */
    this.country = null;
    // To avoid unneccessary logging, first leverage in-memeory Cache
    this.cache = new Set();
  }
  handleVocaInternalUStats(event) {
    if (window.location.hostname !== 'localhost') {
      return;
    }
    try {
      /** id is unique identifier for component,
       * it consists of components name, page url, projects name and projects country. */
      const country = Countries[this.country] ||
        (() => {
          throw new Error('telia-voca-stats "country" prop is invalid');
        })();
      const id = `${event.detail.voca_component}-${event.detail.voca_added}`;
      const exist = this.cache.has(id);
      if (!exist && this.project) {
        const body = Object.assign({ voca_project: this.project, voca_country: country }, event.detail);
        this.cache.add(id);
        // Post date to API
        // Sends Component usage stats data to AWS S3.
        fetch(API_STATS, {
          method: 'POST',
          body: JSON.stringify(body),
        })
          .then(response => {
          return response.json();
        })
          .catch((error) => {
          console.error(error.body);
        });
      }
    }
    catch (error) {
      console.error(error.message);
    }
  }
  render() {
    /** telia-voca-stats is not a component, so it returns null, instead of html. */
    return null;
  }
};

export { TeliaVocaStats as telia_voca_stats };
