text: "use strict";
  const Web3Modal = window.Web3Modal.default;
  let web3Modal;
  let provider;
  let selectedAccount;
  
  const publicSupply = 4444;
  let isConnected = false;
  
  let contractAddress = "0x236ebe2257ed8bD59299D61fE41BaeE2ccF8eD28";
  let abi = [{
      "inputs": [{
              "internalType": "bytes",
              "name": "signature",
              "type": "bytes"
          },
          {
              "internalType": "uint256",
              "name": "freeTokensQuantity",
              "type": "uint256"
          },
          {
              "internalType": "uint256",
              "name": "allowedFreeQuantity",
              "type": "uint256"
          }
      ],
      "name": "freeMint",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
  }, {
      "inputs": [{
              "internalType": "uint256",
              "name": "tokensQuantity",
              "type": "uint256"
          },
  
      ],
      "name": "mint",
      "outputs": [],
      "stateMutability": "payable",
      "type": "function"
  }, {
      "inputs": [{
              "internalType": "uint256",
              "name": "paidTokensQuantity",
              "type": "uint256"
          },
          {
              "internalType": "bytes",
              "name": "signature",
              "type": "bytes"
          },
          {
              "internalType": "uint256",
              "name": "freeTokensQuantity",
              "type": "uint256"
          },
          {
              "internalType": "uint256",
              "name": "allowedFreeQuantity",
              "type": "uint256"
          }
      ],
      "name": "mintFP",
      "outputs": [],
      "stateMutability": "payable",
      "type": "function"
  }, {
      "inputs": [],
      "name": "_tokensMinted",
      "outputs": [{
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
      }],
      "stateMutability": "view",
      "type": "function"
  }];
  
  function changeBuyQuantity(Quantity) {
      document.getElementById("buyQuantity").innerHTML = Quantity;
  }
  
  function changeFreeQuantity(Quantity) {
      document.getElementById("freeQuantity").innerHTML = Quantity;
  }
  
  function init() {
      const providerOptions = {};
      web3Modal = new Web3Modal({
          cacheProvider: false,
          providerOptions,
          disableInjectedProvider: false,
      });
  
  }
  async function fetchAccountData() {
      const web3 = new Web3(provider);
      const accounts = await web3.eth.getAccounts();
      selectedAccount = accounts[0];
  }
  
  async function onConnect() {
      try {
          provider = await web3Modal.connect();
      } catch (e) {
          console.log("Could not get a wallet connection", e);
          return;
      }
  
      provider.on("accountsChanged", (accounts) => {
          fetchAccountData();
      });
  
      fetchAccountData();
  }
  
  async function checkPublicMinted() {
      const web3 = new Web3(provider);
      const contract = new web3.eth.Contract(abi, contractAddress);
      return await contract.methods._tokensMinted().call();
  }
  
  async function connect() {
      if (window.web3 == undefined && window.ethereum == undefined) {
          window
              .open("https://metamask.app.link/dapp/invertedmutants.io", "_blank")
              .focus();
      }
      provider = await web3Modal.connect();
      provider.on("accountsChanged", (accounts) => {
          fetchAccountData();
      });
  
      await fetchAccountData();
  
      checkPublicMinted().then(count => {
          axios.post("https://sig.invertedmutants.io/isEligible", {
              wallet: selectedAccount
          }).then(res => {
              document.getElementById("info-details").innerHTML = `Eligible Free Mints : ${res.data.freeQuantity}<br>Minted : ${count}/4444`;
              document.getElementById("freeQuantityRange").max = res.data.freeQuantity;
              changeFreeQuantity(res.data.freeQuantity)
          }).catch(error => {
              document.getElementById("info-details").innerHTML = `Eligible Free Mints : ERROR<br>Minted ${count}/4444`;
          })
      })
      if (!selectedAccount) {
          document.getElementById("connect-info").innerHTML = "Not connected";
          iziToast.error({
              title: "Error",
              message: "You need to be connected to mint.",
          });
      }
  
  
      if (selectedAccount) {
          document.getElementById("connect-info").innerHTML = "Connected";
          document.getElementById("connect-info-card").classList.remove("bg-danger");
          document.getElementById("connect-info-card").classList.add("bg-success");
          document.getElementById("connect-button").innerHTML = "Connected";
          document.getElementById("connect-button").classList.add("btn-outline-dark");
          document.getElementById("connect-button").classList.remove("btn-outline-success");
          isConnected = true;
      } else {
          document.getElementById("connect-info").innerHTML = "Not Connected";
          document.getElementById("connect-info-card").classList.add("bg-danger");
          document.getElementById("connect-info-card").classList.remove("bg-sucess");
          document.getElementById("connect-button").innerHTML = "Connect";
          document.getElementById("connect-button").classList.add("btn-outline-sucess");
          document.getElementById("connect-button").classList.remove("btn-outline-dark");
          isConnected = false;
      }
  }
  
  async function mint() {
      if (!isConnected) {
          iziToast.error({
              title: "Error",
              message: "No connected",
          });
          return
      }
      const web3 = new Web3(provider);
      const contract = new web3.eth.Contract(abi, contractAddress);
      let mintedP = await checkPublicMinted();
      if (selectedAccount && mintedP < publicSupply) {
          if (document.getElementById("buyQuantityRange").value > 0 && document.getElementById("freeQuantityRange").value > 0) {
              let signature = await web3.eth.personal.sign(`${selectedAccount}IM_WEBSITE`, selectedAccount);
              if (signature) {
                  axios.post("https://sig.invertedmutants.io/getFreeAccess/", {
                      wallet: selectedAccount,
                      signature: signature
                  }).then(data => {
                      if (data.data.signature) {
                          contract.methods
                              .mintFP(document.getElementById("buyQuantityRange").value, data.data.signature, document.getElementById("freeQuantityRange").value, data.data.quantityAllowed)
                              .send({
                                  value: web3.utils.toWei(`${document.getElementById("buyQuantityRange").value * 44440}`, "micro"),
                                  from: selectedAccount
                              }).then(function (info) {
                                  iziToast.success({
                                      title: 'OK',
                                      message: 'Successfully bought!',
                                  });
                              }).catch(function (err) {
  
                              });
                      } else {
                          iziToast.error({
                              title: 'Error',
                              message: 'Not whitelisted',
                          });
                      }
  
                  });
              } else {
                  iziToast.error({
                      title: 'Error',
                      message: 'Must sign the message',
                  });
              }
          }
          // normal mint only
          if (document.getElementById("buyQuantityRange").value > 0 && document.getElementById("freeQuantityRange").value == 0) {
              contract.methods
                  .mint(document.getElementById("buyQuantityRange").value)
                  .send({
                      value: web3.utils.toWei(`${document.getElementById("buyQuantityRange").value * 44440}`, "micro"),
                      from: selectedAccount
                  }).then(function (info) {
                      iziToast.success({
                          title: 'OK',
                          message: 'Successfully bought!',
                      });
                  }).catch(function (err) {
  
                  });
          }
          // free mint only
          if (document.getElementById("buyQuantityRange").value == 0 && document.getElementById("freeQuantityRange").value > 0) {
              let signature = await web3.eth.personal.sign(`${selectedAccount}IM_WEBSITE`, selectedAccount);
              if (signature) {
                  axios.post("https://sig.invertedmutants.io/getFreeAccess/", {
                      wallet: selectedAccount,
                      signature: signature
                  }).then(data => {
                      if (data.data.signature) {
                          contract.methods
                              .freeMint(data.data.signature, document.getElementById("freeQuantityRange").value, data.data.quantityAllowed)
                              .send({
                                  from: selectedAccount
                              }).then(function (info) {
                                  iziToast.success({
                                      title: 'OK',
                                      message: 'Successfully bought!',
                                  });
                              }).catch(function (err) {
  
                              });
                      } else {
                          iziToast.error({
                              title: 'Error',
                              message: 'Not whitelisted',
                          });
                      }
  
                  });
              } else {
                  iziToast.error({
                      title: 'Error',
                      message: 'Must sign the message',
                  });
              }
          }
      } else {
          iziToast.error({
              title: "Error",
              message: "You need to be connected to mint.",
          });
      }
  }
  
  
  window.addEventListener("load", async () => {
      init();
  });