text: "use strict";
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.SolflareWalletAdapter = exports.SolflareWalletName = void 0;
  const wallet_adapter_base_1 = require("@solana/wallet-adapter-base");
  const web3_js_1 = require("@solana/web3.js");
  exports.SolflareWalletName = 'Solflare';
  class SolflareWalletAdapter extends wallet_adapter_base_1.BaseMessageSignerWalletAdapter {
      constructor(config = {}) {
          super();
          this.name = exports.SolflareWalletName;
          this.url = 'https://solflare.com';
          this.icon = 'data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjUwIiB2aWV3Qm94PSIwIDAgNTAgNTAiIHdpZHRoPSI1MCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGxpbmVhckdyYWRpZW50IGlkPSJhIj48c3RvcCBvZmZzZXQ9IjAiIHN0b3AtY29sb3I9IiNmZmMxMGIiLz48c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiNmYjNmMmUiLz48L2xpbmVhckdyYWRpZW50PjxsaW5lYXJHcmFkaWVudCBpZD0iYiIgZ3JhZGllbnRVbml0cz0idXNlclNwYWNlT25Vc2UiIHgxPSI2LjQ3ODM1IiB4Mj0iMzQuOTEwNyIgeGxpbms6aHJlZj0iI2EiIHkxPSI3LjkyIiB5Mj0iMzMuNjU5MyIvPjxyYWRpYWxHcmFkaWVudCBpZD0iY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40MjEuOTUwNy45NDAzcy0uNDI1Ny45NDAyLS45NTA3Ljk0MDItLjk1MDctLjQyMDktLjk1MDctLjk0MDIuNDI1Ny0uOTQwMy45NTA3LS45NDAzem0tMjQuNjI5Ni0yMi40Nzk3Yy41MjUgMCAuOTUwNi40MjA5NzMuOTUwNi45NDAyNyAwIC41MTkzLS40MjU2Ljk0MDI3LS45NTA2Ljk0MDI3LS41MjUxIDAtLjk1MDctLjQyMDk3LS45NTA3LS45NDAyNyAwLS41MTkyOTcuNDI1Ni0uOTQwMjcuOTUwNy0uOTQwMjd6IiBmaWxsPSJ1cmwoI2IpIi8+PHBhdGggZD0ibTI0LjU3MSAzMi43NzkyYzQuOTU5NiAwIDguOTgwMi0zLjk3NjUgOC45ODAyLTguODgxOSAwLTQuOTA1My00LjAyMDYtOC44ODE5LTguOTgwMi04Ljg4MTlzLTguOTgwMiAzLjk3NjYtOC45ODAyIDguODgxOWMwIDQuOTA1NCA0LjAyMDYgOC44ODE5IDguOTgwMiA4Ljg4MTl6IiBmaWxsPSJ1cmwoI2MpIi8+PC9zdmc+';
          this._readyState = typeof window === 'undefined' || typeof document === 'undefined'
              ? wallet_adapter_base_1.WalletReadyState.Unsupported
              : wallet_adapter_base_1.WalletReadyState.NotDetected;
          this._disconnected = () => {
              const wallet = this._wallet;
              if (wallet) {
                  wallet.off('disconnect', this._disconnected);
                  this._wallet = null;
                  this._publicKey = null;
                  this.emit('error', new wallet_adapter_base_1.WalletDisconnectedError());
                  this.emit('disconnect');
              }
          };
          this._connecting = false;
          this._wallet = null;
          this._publicKey = null;
          if (this._readyState !== wallet_adapter_base_1.WalletReadyState.Unsupported) {
              (0, wallet_adapter_base_1.scopePollingDetectionStrategy)(() => {
                  var _a;
                  if ((_a = window.solflare) === null || _a === void 0 ? void 0 : _a.isSolflare) {
                      this._readyState = wallet_adapter_base_1.WalletReadyState.Installed;
                      this.emit('readyStateChange', this._readyState);
                      return true;
                  }
                  return false;
              });
          }
      }
      get publicKey() {
          return this._publicKey;
      }
      get connecting() {
          return this._connecting;
      }
      get connected() {
          var _a;
          return !!((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.isConnected);
      }
      get readyState() {
          return this._readyState;
      }
      connect() {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  if (this.connected || this.connecting)
                      return;
                  if (this._readyState !== wallet_adapter_base_1.WalletReadyState.Installed)
                      throw new wallet_adapter_base_1.WalletNotReadyError();
                  this._connecting = true;
                  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                  const wallet = window.solflare;
                  if (!wallet.isConnected) {
                      try {
                          yield wallet.connect();
                      }
                      catch (error) {
                          throw new wallet_adapter_base_1.WalletConnectionError(error === null || error === void 0 ? void 0 : error.message, error);
                      }
                  }
                  // HACK: Solflare doesn't reject its promise if the popup is closed
                  if (!wallet.publicKey)
                      throw new wallet_adapter_base_1.WalletConnectionError();
                  let publicKey;
                  try {
                      publicKey = new web3_js_1.PublicKey(wallet.publicKey.toBytes());
                  }
                  catch (error) {
                      throw new wallet_adapter_base_1.WalletPublicKeyError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
                  wallet.on('disconnect', this._disconnected);
                  this._wallet = wallet;
                  this._publicKey = publicKey;
                  this.emit('connect', publicKey);
              }
              catch (error) {
                  this.emit('error', error);
                  throw error;
              }
              finally {
                  this._connecting = false;
              }
          });
      }
      disconnect() {
          return __awaiter(this, void 0, void 0, function* () {
              const wallet = this._wallet;
              if (wallet) {
                  wallet.off('disconnect', this._disconnected);
                  this._wallet = null;
                  this._publicKey = null;
                  try {
                      yield wallet.disconnect();
                  }
                  catch (error) {
                      this.emit('error', new wallet_adapter_base_1.WalletDisconnectionError(error === null || error === void 0 ? void 0 : error.message, error));
                  }
              }
              this.emit('disconnect');
          });
      }
      signTransaction(transaction) {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  const wallet = this._wallet;
                  if (!wallet)
                      throw new wallet_adapter_base_1.WalletNotConnectedError();
                  try {
                      return (yield wallet.signTransaction(transaction)) || transaction;
                  }
                  catch (error) {
                      throw new wallet_adapter_base_1.WalletSignTransactionError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
              }
              catch (error) {
                  this.emit('error', error);
                  throw error;
              }
          });
      }
      signAllTransactions(transactions) {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  const wallet = this._wallet;
                  if (!wallet)
                      throw new wallet_adapter_base_1.WalletNotConnectedError();
                  try {
                      return (yield wallet.signAllTransactions(transactions)) || transactions;
                  }
                  catch (error) {
                      throw new wallet_adapter_base_1.WalletSignTransactionError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
              }
              catch (error) {
                  this.emit('error', error);
                  throw error;
              }
          });
      }
      signMessage(message) {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  const wallet = this._wallet;
                  if (!wallet)
                      throw new wallet_adapter_base_1.WalletNotConnectedError();
                  try {
                      const { signature } = yield wallet.signMessage(message, 'utf8');
                      return signature;
                  }
                  catch (error) {
                      throw new wallet_adapter_base_1.WalletSignTransactionError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
              }
              catch (error) {
                  this.emit('error', error);
                  throw error;
              }
          });
      }
  }
  exports.SolflareWalletAdapter = SolflareWalletAdapter;
  //# sourceMappingURL=adapter.js.map