import { HTMLElement, h, proxyCustomElement, Env } from '@stencil/core/internal/client';
import { D as DropKit, J as JsonRpcProvider } from './DropKit.js';
import { d as defineCustomElement$3 } from './nk-error-message2.js';
import { d as defineCustomElement$2 } from './nk-loading2.js';

const INFURA_ENDPOINTS = {
  1: 'https://mainnet.infura.io/v3/',
  4: 'https://rinkeby.infura.io/v3/',
  137: 'https://polygon-mainnet.infura.io/v3/',
  80001: 'https://polygon-mumbai.infura.io/v3/',
};

const nkSupplyCss = ":host{display:block}";

let NkSupply$1 = class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    /**
     * Flag to enable testnet mode
     */
    this.dev = false;
    this.msg = null;
    this.drop = null;
    this.maxAmount = 0;
    this.totalSupply = 0;
  }
  componentWillLoad() {
    this.initDrop();
  }
  render() {
    const NkMsg = () => {
      if (!this.msg) {
        return null;
      }
      return (h("nk-error-message", { class: "info", exportparts: "info", onClosed: () => (this.msg = null) }, this.msg.text));
    };
    return (h("div", { part: "supply-text" }, h(NkMsg, null), this.loading ? h("nk-loading", null) : `${this.totalSupply}/${this.maxAmount}`, h("slot", null)));
  }
  async initDrop() {
    this.loading = true;
    const infuraId = Env.infuraId;
    if (!infuraId) {
      this.loading = false;
      return;
    }
    try {
      if (!this.drop) {
        const data = await DropKit.getCollectionData(this.apikey, this.dev);
        const url = INFURA_ENDPOINTS[data.chainId];
        const provider = new JsonRpcProvider(`${url}${infuraId}`);
        this.drop = await DropKit.create(this.apikey, this.dev, null, provider);
      }
      this.maxAmount = await this.drop.maxAmount();
      this.totalSupply = await this.drop.totalSupply();
    }
    catch (e) {
      this.msg = { error: true, text: e.message };
    }
    finally {
      this.loading = false;
    }
  }
  static get style() { return nkSupplyCss; }
};
NkSupply$1 = /*@__PURE__*/ proxyCustomElement(NkSupply$1, [1, "nk-supply", {
    "apikey": [1],
    "dev": [4],
    "loading": [32],
    "msg": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["nk-supply", "nk-error-message", "nk-loading"];
  components.forEach(tagName => { switch (tagName) {
    case "nk-supply":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, NkSupply$1);
      }
      break;
    case "nk-error-message":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "nk-loading":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}

const NkSupply = NkSupply$1;
const defineCustomElement = defineCustomElement$1;

export { NkSupply, defineCustomElement };
