import { Component, Prop, Listen } from '@stencil/core';
import { Countries } from '../../utils/countries';
const API_STATS = 'https://stats.api.voca.teliacompany.com/components';
export class TeliaVocaStats {
  constructor() {
    /** 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;
  }
  static get is() { return "telia-voca-stats"; }
  static get properties() { return {
    "project": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "TeliaVocaStatsProps['project']",
        "resolved": "string",
        "references": {
          "TeliaVocaStatsProps": {
            "location": "local"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Name of the project. Project field is unique and required. To ensure uniqueness of this field,\nplease input it in correct format `'[projectsName]-[countryCode]'`. E.g., `'b2c-fi'`, `'b2c-ecom-fi'`, `'b2b-se'`"
      },
      "attribute": "project",
      "reflect": false,
      "defaultValue": "''"
    },
    "country": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "TeliaVocaStatsProps['country']",
        "resolved": "\"DK\" | \"EE\" | \"FI\" | \"Global\" | \"LT\" | \"NO\" | \"SE\"",
        "references": {
          "TeliaVocaStatsProps": {
            "location": "local"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Country initials of the project, this prop is provided by the project that uses Voca DS.\nPossible values: 'DK' | 'EE' | 'FI' | 'LT' | 'NO' | 'SE' | 'Global'."
      },
      "attribute": "country",
      "reflect": false,
      "defaultValue": "null"
    }
  }; }
  static get listeners() { return [{
      "name": "vocaInternalUStats",
      "method": "handleVocaInternalUStats",
      "target": "window",
      "capture": false,
      "passive": false
    }]; }
}
