var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);

// src/modules/gateway/dataLakes/PageProvider.ts
import {
  DataLakeRunnerMoniker,
  DataLakeViewerMoniker
} from "@xyo-network/xl1-protocol/protocol-lib";
import { DataLakesGetMethod, DataLakesInsertMethod } from "#rpc";
var AbstractDataLakePageProvider = class {
  id;
  transport;
  constructor(transport, id) {
    this.transport = transport;
    this.id = id;
  }
  async get(hashes) {
    return await this.transport.sendRequest(DataLakesGetMethod, [{ id: this.id, hashes }]);
  }
  next(_options) {
    throw new Error("Method [next] not implemented.");
  }
};
var DataLakePageViewer = class extends AbstractDataLakePageProvider {
  moniker = DataLakeViewerMoniker;
};
var DataLakePageRunner = class extends AbstractDataLakePageProvider {
  moniker = DataLakeRunnerMoniker;
  clear() {
    throw new Error("Method [clear] not implemented.");
  }
  delete(_hashes) {
    throw new Error("Method [delete] not implemented.");
  }
  async insert(items) {
    return await this.transport.sendRequest(DataLakesInsertMethod, [{ id: this.id, payloads: items }]);
  }
};

// src/modules/gateway/examples/getAccountBalance.ts
import { assertEx } from "@ariestools/sdk";
async function getAccountBalance(gateway, address) {
  const viewer = assertEx(
    gateway.connection.viewer,
    () => "Gateway has no connection.viewer \u2014 cannot read balances"
  );
  return await viewer.account.balance.accountBalance(address);
}

// src/modules/gateway/examples/transferXl1.ts
async function transferXl1(runner, to, amount, options) {
  return await runner.send(to, amount, options);
}

// src/modules/gateway/GatewayBuilder.ts
import { isDefined } from "@ariestools/sdk";
import { XyoGatewayMoniker, XyoGatewayRunnerMoniker } from "@xyo-network/xl1-protocol/protocol-lib";
import { basicRemoteRunnerLocator, basicRemoteViewerLocator } from "#providers";

// src/modules/gateway/XyoSignerWrapper.ts
import { XyoSignerMoniker } from "@xyo-network/xl1-protocol/protocol-lib";
import {
  AbstractCreatableProvider,
  creatableProvider
} from "#protocol-sdk";
var XyoSignerWrapper = class extends AbstractCreatableProvider {
  moniker = XyoSignerWrapper.defaultMoniker;
  address() {
    return this.params.signer.address();
  }
  sign(bw) {
    return this.params.signer.sign(bw);
  }
  signTransaction(tx) {
    return this.params.signer.signTransaction(tx);
  }
};
__publicField(XyoSignerWrapper, "connectionTypes", []);
__publicField(XyoSignerWrapper, "defaultMoniker", XyoSignerMoniker);
__publicField(XyoSignerWrapper, "dependencies", []);
__publicField(XyoSignerWrapper, "monikers", [XyoSignerMoniker]);
XyoSignerWrapper = __decorateClass([
  creatableProvider()
], XyoSignerWrapper);

// src/modules/gateway/GatewayBuilder.ts
var GatewayBuilder = class {
  _additionalProviders = [];
  _additionalRunnerProviders = [];
  _dataLakeEndpoint;
  _name = "gateway-client";
  _rpc;
  _validators;
  additionalProviders(factories) {
    this._additionalProviders = factories;
    return this;
  }
  additionalRunnerProviders(factories) {
    this._additionalRunnerProviders = factories;
    return this;
  }
  async build(signer) {
    if (!isDefined(this._rpc)) {
      throw new Error("GatewayBuilder: rpcUrl() or postMessage() must be set before build()");
    }
    const rpcConnection = this._rpc;
    if (signer === void 0) {
      const locator2 = await basicRemoteViewerLocator(this._name, rpcConnection, this._dataLakeEndpoint, {
        validators: this._validators,
        additionalProviders: this._additionalProviders
      });
      return await locator2.getInstance(XyoGatewayMoniker);
    }
    const signerFactory = XyoSignerWrapper.factory(XyoSignerWrapper.dependencies, { signer });
    const locator = await basicRemoteRunnerLocator(this._name, rpcConnection, void 0, this._dataLakeEndpoint, {
      validators: this._validators,
      additionalProviders: this._additionalProviders,
      additionalRunnerProviders: this._additionalRunnerProviders,
      signerFactory
    });
    return await locator.getInstance(XyoGatewayRunnerMoniker);
  }
  dataLakeEndpoint(endpoint) {
    this._dataLakeEndpoint = endpoint;
    return this;
  }
  name(name) {
    this._name = name;
    return this;
  }
  postMessage(networkId, sessionId) {
    this._rpc = {
      protocol: "postMessage",
      networkId,
      sessionId
    };
    return this;
  }
  rpcUrl(url) {
    this._rpc = { protocol: "http", url };
    return this;
  }
  validators(validators) {
    this._validators = validators;
    return this;
  }
};

// src/modules/gateway/getRestGateway.ts
import { XyoGatewayMoniker as XyoGatewayMoniker2 } from "@xyo-network/xl1-protocol/protocol-lib";
import { restConnectionFromS3Bucket } from "#protocol-sdk";
import { basicRestViewerLocator, resolveFinalizedAlias } from "#providers";
var STANDARD_ENDPOINT_SUBDOMAINS = {
  chainState: "state",
  finalized: "blocks",
  index: "indexes"
};
function restGatewayConfigFromEndpoint(endpoint) {
  const url = new URL(endpoint);
  const bucketOrigin = (subdomain) => `${url.protocol}//${subdomain}.${url.host}`;
  const finalized = { readUrl: bucketOrigin(STANDARD_ENDPOINT_SUBDOMAINS.finalized) };
  return {
    blocks: finalized,
    chainState: { readUrl: bucketOrigin(STANDARD_ENDPOINT_SUBDOMAINS.chainState) },
    finalized,
    index: { readUrl: bucketOrigin(STANDARD_ENDPOINT_SUBDOMAINS.index) }
  };
}
async function getRestGateway(config) {
  const resolved = typeof config === "string" ? restGatewayConfigFromEndpoint(config) : config;
  const restConfig = {
    finalized: restConnectionFromS3Bucket(resolveFinalizedAlias(resolved, "getRestGateway")),
    chainState: resolved.chainState ? restConnectionFromS3Bucket(resolved.chainState) : void 0,
    index: resolved.index ? restConnectionFromS3Bucket(resolved.index) : void 0
  };
  const {
    dataLakeEndpoint,
    fetchFn,
    initRewardsCache,
    name
  } = typeof config === "string" ? {} : config;
  const locator = await basicRestViewerLocator(name ?? "rest-gateway-client", restConfig, dataLakeEndpoint, { fetchFn, initRewardsCache });
  return await locator.getInstance(XyoGatewayMoniker2);
}

// src/modules/gateway/getRestGatewayRunner.ts
import { XyoGatewayRunnerMoniker as XyoGatewayRunnerMoniker2 } from "@xyo-network/xl1-protocol/protocol-lib";
import { restConnectionFromS3Bucket as restConnectionFromS3Bucket2 } from "#protocol-sdk";
import { basicRestRunnerLocator, resolveFinalizedAlias as resolveFinalizedAlias2 } from "#providers";
async function getRestGatewayRunner(config) {
  const buckets = "endpoint" in config ? restGatewayConfigFromEndpoint(config.endpoint) : config;
  const {
    dataLakeEndpoint,
    fetchFn,
    initRewardsCache,
    name,
    rpcUrl,
    signer
  } = config;
  const restConfig = {
    finalized: restConnectionFromS3Bucket2(resolveFinalizedAlias2(buckets, "getRestGatewayRunner")),
    chainState: buckets.chainState ? restConnectionFromS3Bucket2(buckets.chainState) : void 0,
    index: buckets.index ? restConnectionFromS3Bucket2(buckets.index) : void 0
  };
  const signerFactory = XyoSignerWrapper.factory(XyoSignerWrapper.dependencies, { signer });
  const locator = await basicRestRunnerLocator(
    name ?? "rest-gateway-runner-client",
    restConfig,
    dataLakeEndpoint,
    {
      fetchFn,
      initRewardsCache,
      rpcUrl,
      signerFactory
    }
  );
  return await locator.getInstance(XyoGatewayRunnerMoniker2);
}
export {
  AbstractDataLakePageProvider,
  DataLakePageRunner,
  DataLakePageViewer,
  GatewayBuilder,
  XyoSignerWrapper,
  getAccountBalance,
  getRestGateway,
  getRestGatewayRunner,
  restGatewayConfigFromEndpoint,
  transferXl1
};
//# sourceMappingURL=gateway.mjs.map
