import { channelActions } from './chunk-ZSSNU3UF.js';

// ../channel/dist/voucher.js
var VOUCHER_TYPES = {
  Voucher: [
    { name: "channelId", type: "bytes32" },
    { name: "cumulativeAmount", type: "uint128" }
  ]
};
function getVoucherDomain(chainId, verifyingContract) {
  return {
    name: "MicroPaymentChannel",
    version: "1.0.0",
    chainId,
    verifyingContract
  };
}
async function signVoucher(walletClient, params) {
  const domain = getVoucherDomain(params.chainId, params.verifyingContract);
  const account = walletClient.account;
  if (!account) {
    throw new Error("WalletClient must have an account");
  }
  return walletClient.signTypedData({
    account,
    domain,
    types: VOUCHER_TYPES,
    primaryType: "Voucher",
    message: {
      channelId: params.channelId,
      cumulativeAmount: params.cumulativeAmount
    }
  });
}

// ../channel/dist/ChannelClient.js
var ChannelClient = class {
  readActions;
  writeActions;
  config;
  constructor(config) {
    this.config = config;
    this.readActions = channelActions(config.channelAddress)(config.publicClient);
    this.writeActions = channelActions(config.channelAddress)(config.walletClient);
  }
  async openChannel(channelConfig) {
    return this.writeActions.openChannel({
      payee: channelConfig.payee,
      token: channelConfig.token,
      deposit: channelConfig.deposit,
      salt: channelConfig.salt,
      authorizedSigner: channelConfig.authorizedSigner,
      account: this.config.walletClient.account
    });
  }
  async signVoucherOffline(channelId, cumulativeAmount) {
    const signature = await signVoucher(this.config.walletClient, {
      channelId,
      cumulativeAmount,
      chainId: this.config.chainId,
      verifyingContract: this.config.channelAddress
    });
    return { channelId, cumulativeAmount, signature };
  }
  async settleChannel(voucher) {
    return this.writeActions.settleChannel({
      channelId: voucher.channelId,
      cumulativeAmount: voucher.cumulativeAmount,
      signature: voucher.signature,
      account: this.config.walletClient.account
    });
  }
  async closeChannel(voucher) {
    return this.writeActions.closeChannel({
      channelId: voucher.channelId,
      cumulativeAmount: voucher.cumulativeAmount,
      signature: voucher.signature,
      account: this.config.walletClient.account
    });
  }
  async topUpChannel(channelId, amount) {
    return this.writeActions.topUpChannel({
      channelId,
      amount,
      account: this.config.walletClient.account
    });
  }
  async requestClose(channelId) {
    return this.writeActions.requestCloseChannel({
      channelId,
      account: this.config.walletClient.account
    });
  }
  async withdraw(channelId) {
    return this.writeActions.withdrawChannel({
      channelId,
      account: this.config.walletClient.account
    });
  }
  async getChannelState(channelId) {
    return this.readActions.getChannel({ channelId });
  }
  async getCloseTimeout() {
    return this.readActions.CLOSE_TIMEOUT();
  }
  async getVersion() {
    return this.readActions.version();
  }
};

export { ChannelClient, VOUCHER_TYPES, getVoucherDomain, signVoucher };
//# sourceMappingURL=chunk-OSPRJZ5T.js.map
//# sourceMappingURL=chunk-OSPRJZ5T.js.map