import { Component, h, Env, Prop, State } from '@stencil/core';
import Dropkit from 'dropkit.js';
import { ethers } from 'ethers';
import { INFURA_ENDPOINTS } from '../../config/infura';
export class NkSupply {
  constructor() {
    /**
     * 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 ethers.providers.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 is() { return "nk-supply"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["nk-supply.css"]
  }; }
  static get styleUrls() { return {
    "$": ["nk-supply.css"]
  }; }
  static get properties() { return {
    "apikey": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": true,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Drop SDK Key"
      },
      "attribute": "apikey",
      "reflect": false
    },
    "dev": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Flag to enable testnet mode"
      },
      "attribute": "dev",
      "reflect": false,
      "defaultValue": "false"
    }
  }; }
  static get states() { return {
    "loading": {},
    "msg": {}
  }; }
}
