import { Host, h } from "@stencil/core";
/**
 * Klevu icon component. Uses Google Material Icons.
 *
 */
export class KlevuIcon {
  constructor() {
    this.name = undefined;
    this.iconURL = undefined;
    this.iconURLSvg = undefined;
    this.svgStyle = undefined;
  }
  async connectedCallback() {
    var _a, _b;
    const url = (_b = (_a = window["klevu_ui_settings"]) === null || _a === void 0 ? void 0 : _a.icons) === null || _b === void 0 ? void 0 : _b[this.name];
    if (url === null || url === void 0 ? void 0 : url.endsWith(".svg")) {
      try {
        const data = await window.fetch(url);
        this.iconURLSvg = await data.text();
        const fontSize = getComputedStyle(this.el).fontSize;
        this.svgStyle = {
          fill: getComputedStyle(this.el).color,
          height: fontSize,
          width: fontSize,
        };
      }
      catch (e) { }
    }
    else if (url) {
      this.iconURL = url;
    }
    if (!this.iconURL && !this.iconURLSvg) {
      // Inject material fonts to project
      const fontCssUrl = "https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@20,600,0,0";
      let element = document.querySelector(`link[href="${fontCssUrl}"]`);
      // Only inject the element if it's not yet present
      if (!element) {
        element = document.createElement("link");
        element.setAttribute("rel", "stylesheet");
        element.setAttribute("href", fontCssUrl);
        document.head.appendChild(element);
      }
    }
  }
  render() {
    return (h(Host, null, this.iconURLSvg ? (h("div", { style: this.svgStyle, innerHTML: this.iconURLSvg })) : this.iconURL ? (h("img", { src: this.iconURL })) : (h("span", null, this.name))));
  }
  static get is() { return "klevu-icon"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() {
    return {
      "$": ["klevu-icon.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["klevu-icon.css"]
    };
  }
  static get properties() {
    return {
      "name": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": true,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Name of the icon. Please use tokens of material icons"
        },
        "attribute": "name",
        "reflect": false
      }
    };
  }
  static get states() {
    return {
      "iconURL": {},
      "iconURLSvg": {},
      "svgStyle": {}
    };
  }
  static get elementRef() { return "el"; }
}
//# sourceMappingURL=klevu-icon.js.map
