import { Effect } from "effect";
import { EvaluationError, TransactionBuilderError } from "../TransactionBuilder.js";
import { parseProviderError } from "./providerErrorParser.js";
/**
 * Resolve protocol parameters for a build invocation.
 *
 * @since 2.0.0
 * @category builders
 */
export const resolveProtocolParameters = (config, options) => {
  if (options?.protocolParameters !== undefined) {
    return Effect.succeed(options.protocolParameters);
  }
  const provider = config.provider;
  if (provider !== undefined) {
    return Effect.map(provider.effect.getProtocolParameters(), params => ({
      minFeeCoefficient: BigInt(params.minFeeA),
      minFeeConstant: BigInt(params.minFeeB),
      coinsPerUtxoByte: params.coinsPerUtxoByte,
      maxTxSize: params.maxTxSize,
      priceMem: params.priceMem,
      priceStep: params.priceStep,
      minFeeRefScriptCostPerByte: params.minFeeRefScriptCostPerByte
    }));
  }
  return Effect.fail(new TransactionBuilderError({
    message: "No protocol parameters provided. Either provide protocolParameters in BuildOptions or provider in config."
  }));
};
/**
 * Resolve the build change address.
 *
 * @since 2.0.0
 * @category builders
 */
export const resolveChangeAddress = (config, options) => {
  if (options?.changeAddress !== undefined) {
    return Effect.succeed(options.changeAddress);
  }
  const wallet = config.wallet;
  if (wallet !== undefined) {
    return wallet.effect.address();
  }
  return Effect.fail(new TransactionBuilderError({
    message: "No change address provided. Either provide wallet in config or changeAddress in build options."
  }));
};
/**
 * Resolve the UTxO set available to the build.
 *
 * @since 2.0.0
 * @category builders
 */
export const resolveAvailableUtxos = (config, options) => {
  if (options?.availableUtxos !== undefined) {
    return Effect.succeed(options.availableUtxos);
  }
  const wallet = config.wallet;
  const provider = config.provider;
  if (wallet !== undefined && provider !== undefined) {
    return Effect.flatMap(wallet.effect.address(), address => provider.effect.getUtxos(address));
  }
  return Effect.fail(new TransactionBuilderError({
    message: "No available UTxOs provided. Either provide wallet+provider in config or availableUtxos in build options."
  }));
};
/**
 * Resolve the evaluator used by the build.
 *
 * @since 2.0.0
 * @category builders
 */
export const resolveEvaluator = (config, options) => {
  if (options?.evaluator !== undefined) {
    return options.evaluator;
  }
  const provider = config.provider;
  if (provider !== undefined) {
    return {
      evaluate: (tx, additionalUtxos, _context) => {
        const utxosToPass = options?.passAdditionalUtxos === true ? additionalUtxos === undefined ? undefined : [...additionalUtxos] : undefined;
        return provider.effect.evaluateTx(tx, utxosToPass).pipe(Effect.mapError(providerError => new EvaluationError({
          message: `Provider evaluation failed: ${providerError.message}`,
          cause: providerError,
          failures: parseProviderError(providerError)
        })));
      }
    };
  }
  return undefined;
};
/**
 * Resolve the slot configuration used by the build.
 *
 * @since 2.0.0
 * @category builders
 */
export const resolveSlotConfig = (config, options) => options?.slotConfig ?? config.chain.slotConfig;
//# sourceMappingURL=resolve.js.map