import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { c as closestElement } from './utils.js';

const preloadedIcons = [
	"expand_more",
	"thumb_up",
	"thumb_down",
	"check",
	"upload",
	"info",
	"menu",
	"close",
	"navigate_before",
	"navigate_next",
	"image_not_supported",
	"photo_camera",
	"upload",
	"search",
	"chevron_left",
	"chevron_right"
];

const klevuIconCss = ":host{display:inline-block}span{font-family:\"Material Symbols Rounded\";font-variation-settings:\"FILL\" 0, \"wght\" 600, \"GRAD\" 0, \"opsz\" 20}div svg{height:100%;width:100%}";

var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a getter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot read private member from an object whose class did not declare it");
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _KlevuIcon_instances, _KlevuIcon_setIcon;
const cache = new Map();
const KlevuIcon = /*@__PURE__*/ proxyCustomElement(class KlevuIcon extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    _KlevuIcon_instances.add(this);
    this.name = undefined;
    this.originElement = undefined;
    this.iconURL = undefined;
    this.iconURLSvg = undefined;
    this.svgStyle = undefined;
  }
  async connectedCallback() {
    var _a, _b;
    let init = closestElement("klevu-init", this.originElement || this.el);
    if (!init) {
      console.warn("could not find closest init for ", this.name);
      const initFields = document.getElementsByTagName("klevu-init");
      if (initFields.length > 0) {
        init = initFields[0];
      }
    }
    const url = (_b = (_a = init === null || init === void 0 ? void 0 : init.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")) {
      await __classPrivateFieldGet(this, _KlevuIcon_instances, "m", _KlevuIcon_setIcon).call(this, url);
    }
    else if (preloadedIcons.includes(this.name)) {
      let path = window.location.origin;
      if (init) {
        path = await init.getAssetsPath();
      }
      if (path) {
        const url = `${path}/assets/${this.name}.svg`;
        await __classPrivateFieldGet(this, _KlevuIcon_instances, "m", _KlevuIcon_setIcon).call(this, url, false);
      }
    }
    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 assetsDirs() { return ["assets"]; }
  get el() { return this; }
  static get style() { return klevuIconCss; }
}, [1, "klevu-icon", {
    "name": [1],
    "originElement": [16],
    "iconURL": [32],
    "iconURLSvg": [32],
    "svgStyle": [32]
  }]);
_KlevuIcon_instances = new WeakSet(), _KlevuIcon_setIcon = async function _KlevuIcon_setIcon(url, shouldAddStyles = true) {
  try {
    if (cache.has(url)) {
      this.iconURLSvg = cache.get(url);
    }
    else {
      const data = await window.fetch(url);
      if (data.status == 200) {
        this.iconURLSvg = await data.text();
        cache.set(url, this.iconURLSvg);
      }
    }
  }
  catch (e) { }
  if (shouldAddStyles) {
    const fontSize = getComputedStyle(this.el).fontSize;
    this.svgStyle = {
      fill: getComputedStyle(this.el).color,
      height: fontSize,
      width: fontSize,
    };
  }
};
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["klevu-icon"];
  components.forEach(tagName => { switch (tagName) {
    case "klevu-icon":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, KlevuIcon);
      }
      break;
  } });
}
defineCustomElement();

export { KlevuIcon as K, defineCustomElement as d };

//# sourceMappingURL=klevu-icon2.js.map