/**
 * @fileoverview Blockfrost API schemas and transformation utilities
 * Internal module for Blockfrost provider implementation
 */
import { Effect, Schema } from "effect";
import * as CoreAssets from "../../../Assets.js";
import * as PoolKeyHash from "../../../PoolKeyHash.js";
import * as Redeemer from "../../../Redeemer.js";
import { ProviderError } from "../Provider.js";
// ============================================================================
// Blockfrost API Response Schemas
// ============================================================================
/**
 * Blockfrost protocol parameters response schema
 */
export const BlockfrostProtocolParameters = /*#__PURE__*/Schema.Struct({
  min_fee_a: Schema.Number,
  min_fee_b: Schema.Number,
  pool_deposit: Schema.String,
  key_deposit: Schema.String,
  min_utxo: /*#__PURE__*/Schema.optional(Schema.String),
  max_tx_size: Schema.Number,
  max_val_size: /*#__PURE__*/Schema.optional(Schema.String),
  utxo_cost_per_word: /*#__PURE__*/Schema.optional(Schema.String),
  cost_models: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Record({
    key: Schema.String,
    value: Schema.Unknown
  })),
  price_mem: /*#__PURE__*/Schema.optional(Schema.Number),
  price_step: /*#__PURE__*/Schema.optional(Schema.Number),
  max_tx_ex_mem: /*#__PURE__*/Schema.optional(Schema.String),
  max_tx_ex_steps: /*#__PURE__*/Schema.optional(Schema.String),
  max_block_ex_mem: /*#__PURE__*/Schema.optional(Schema.String),
  max_block_ex_steps: /*#__PURE__*/Schema.optional(Schema.String),
  max_block_size: Schema.Number,
  collateral_percent: /*#__PURE__*/Schema.optional(Schema.Number),
  max_collateral_inputs: /*#__PURE__*/Schema.optional(Schema.Number),
  coins_per_utxo_size: /*#__PURE__*/Schema.optional(Schema.String),
  min_fee_ref_script_cost_per_byte: /*#__PURE__*/Schema.optional(Schema.Number),
  // Conway era governance parameters
  drep_deposit: /*#__PURE__*/Schema.optional(Schema.String),
  gov_action_deposit: /*#__PURE__*/Schema.optional(Schema.String)
});
/**
 * Blockfrost UTxO amount schema (for multi-asset support)
 */
export const BlockfrostAmount = /*#__PURE__*/Schema.Struct({
  unit: Schema.String,
  quantity: Schema.String
});
/**
 * Blockfrost UTxO response schema
 */
export const BlockfrostUTxO = /*#__PURE__*/Schema.Struct({
  address: Schema.String,
  tx_hash: Schema.String,
  tx_index: Schema.Number,
  output_index: Schema.Number,
  amount: /*#__PURE__*/Schema.Array(BlockfrostAmount),
  block: Schema.String,
  data_hash: /*#__PURE__*/Schema.NullOr(Schema.String),
  inline_datum: /*#__PURE__*/Schema.NullOr(Schema.String),
  reference_script_hash: /*#__PURE__*/Schema.NullOr(Schema.String)
});
/**
 * Blockfrost delegation/account response schema
 * From /accounts/{stake_address} endpoint
 */
export const BlockfrostDelegation = /*#__PURE__*/Schema.Struct({
  stake_address: Schema.String,
  active: Schema.Boolean,
  active_epoch: /*#__PURE__*/Schema.NullOr(Schema.Number),
  pool_id: /*#__PURE__*/Schema.NullOr(Schema.String),
  controlled_amount: Schema.String,
  rewards_sum: Schema.String,
  withdrawals_sum: Schema.String,
  reserves_sum: Schema.String,
  treasury_sum: Schema.String,
  withdrawable_amount: Schema.String,
  drep_id: /*#__PURE__*/Schema.NullOr(Schema.String)
});
/**
 * Blockfrost asset address response schema (from /assets/{unit}/addresses endpoint)
 */
export const BlockfrostAssetAddress = /*#__PURE__*/Schema.Struct({
  address: Schema.String,
  quantity: Schema.String
});
/**
 * Blockfrost transaction UTxO output schema (from /txs/{hash}/utxos endpoint)
 * Different from regular UTxO - uses output_index instead of tx_index
 */
export const BlockfrostTxUtxoOutput = /*#__PURE__*/Schema.Struct({
  address: Schema.String,
  amount: /*#__PURE__*/Schema.Array(BlockfrostAmount),
  output_index: Schema.Number,
  data_hash: /*#__PURE__*/Schema.NullOr(Schema.String),
  inline_datum: /*#__PURE__*/Schema.NullOr(Schema.String),
  reference_script_hash: /*#__PURE__*/Schema.NullOr(Schema.String),
  collateral: Schema.Boolean,
  consumed_by_tx: /*#__PURE__*/Schema.NullOr(Schema.String)
});
/**
 * Blockfrost transaction UTxOs response schema (from /txs/{hash}/utxos endpoint)
 */
export const BlockfrostTxUtxos = /*#__PURE__*/Schema.Struct({
  hash: Schema.String,
  inputs: /*#__PURE__*/Schema.Array(Schema.Unknown),
  outputs: /*#__PURE__*/Schema.Array(BlockfrostTxUtxoOutput)
});
/**
 * Blockfrost transaction submit response schema
 */
export const BlockfrostSubmitResponse = Schema.String;
/**
 * Blockfrost datum response schema
 */
export const BlockfrostDatum = /*#__PURE__*/Schema.Struct({
  json_value: /*#__PURE__*/Schema.optional(Schema.Unknown),
  cbor: Schema.String
});
/**
 * Schema for JSONWSP-wrapped Ogmios evaluation response
 * Used by /utils/txs/evaluate/utxos endpoint
 * Can contain either EvaluationResult (success) or EvaluationFailure (error)
 */
export const JsonwspOgmiosEvaluationResponse = /*#__PURE__*/Schema.Struct({
  type: /*#__PURE__*/Schema.optional(Schema.String),
  version: /*#__PURE__*/Schema.optional(Schema.String),
  servicename: /*#__PURE__*/Schema.optional(Schema.String),
  methodname: /*#__PURE__*/Schema.optional(Schema.String),
  result: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Struct({
    EvaluationResult: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Record({
      key: Schema.String,
      // "spend:0", "mint:1", etc.
      value: /*#__PURE__*/Schema.Struct({
        memory: Schema.Number,
        steps: Schema.Number
      })
    })),
    EvaluationFailure: /*#__PURE__*/Schema.optional(Schema.Unknown)
  })),
  fault: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Struct({
    code: /*#__PURE__*/Schema.optional(Schema.String),
    string: /*#__PURE__*/Schema.optional(Schema.String)
  })),
  reflection: /*#__PURE__*/Schema.optional(Schema.Unknown)
});
// ============================================================================
// Transformation Functions
// ============================================================================
/**
 * Transform Blockfrost protocol parameters to Evolution SDK format
 */
export const transformProtocolParameters = blockfrostParams => {
  return {
    minFeeA: blockfrostParams.min_fee_a,
    minFeeB: blockfrostParams.min_fee_b,
    poolDeposit: BigInt(blockfrostParams.pool_deposit),
    keyDeposit: BigInt(blockfrostParams.key_deposit),
    maxTxSize: blockfrostParams.max_tx_size,
    maxValSize: blockfrostParams.max_val_size ? Number(blockfrostParams.max_val_size) : 0,
    priceMem: blockfrostParams.price_mem || 0,
    priceStep: blockfrostParams.price_step || 0,
    maxTxExMem: blockfrostParams.max_tx_ex_mem ? BigInt(blockfrostParams.max_tx_ex_mem) : 0n,
    maxTxExSteps: blockfrostParams.max_tx_ex_steps ? BigInt(blockfrostParams.max_tx_ex_steps) : 0n,
    coinsPerUtxoByte: blockfrostParams.coins_per_utxo_size ? BigInt(blockfrostParams.coins_per_utxo_size) : 0n,
    collateralPercentage: blockfrostParams.collateral_percent || 0,
    maxCollateralInputs: blockfrostParams.max_collateral_inputs || 0,
    minFeeRefScriptCostPerByte: blockfrostParams.min_fee_ref_script_cost_per_byte || 0,
    drepDeposit: blockfrostParams.drep_deposit ? BigInt(blockfrostParams.drep_deposit) : 0n,
    govActionDeposit: blockfrostParams.gov_action_deposit ? BigInt(blockfrostParams.gov_action_deposit) : 0n,
    costModels: {
      PlutusV1: blockfrostParams.cost_models?.PlutusV1 || {},
      PlutusV2: blockfrostParams.cost_models?.PlutusV2 || {},
      PlutusV3: blockfrostParams.cost_models?.PlutusV3 || {}
    }
  };
};
/**
 * Transform Blockfrost amounts to Core Assets
 */
export const transformAmounts = amounts => {
  let lovelace = 0n;
  const multiAssetEntries = [];
  for (const amount of amounts) {
    if (amount.unit === "lovelace") {
      lovelace = BigInt(amount.quantity);
    } else {
      multiAssetEntries.push([amount.unit, BigInt(amount.quantity)]);
    }
  }
  // Build Core Assets starting with lovelace
  let assets = CoreAssets.fromLovelace(lovelace);
  // Add multi-assets if any using hex strings
  for (const [unit, qty] of multiAssetEntries) {
    // Parse unit - policyId is first 56 chars, assetName is remainder
    const policyIdHex = unit.slice(0, 56);
    const assetNameHex = unit.slice(56);
    assets = CoreAssets.addByHex(assets, policyIdHex, assetNameHex, qty);
  }
  return assets;
};
/**
 * Transform Blockfrost delegation to delegation info
 */
export const transformDelegation = blockfrostDelegation => {
  if (!blockfrostDelegation.pool_id) {
    return {
      poolId: null,
      rewards: BigInt(blockfrostDelegation.withdrawable_amount)
    };
  }
  const poolId = Schema.decodeSync(PoolKeyHash.FromBech32)(blockfrostDelegation.pool_id);
  return {
    poolId,
    rewards: BigInt(blockfrostDelegation.withdrawable_amount)
  };
};
/**
 * Transform JSONWSP-wrapped Ogmios evaluation response to Evolution SDK format
 * Used by /utils/txs/evaluate/utxos endpoint
 * Format: { result: { EvaluationResult: { "spend:0": { "memory": 1100, "steps": 160100 }, ... } } }
 */
export const transformJsonwspOgmiosEvaluationResult = jsonwspResponse => {
  // Handle JSONWSP fault response (Ogmios backend error)
  if (jsonwspResponse.type === "jsonwsp/fault") {
    const faultMessage = jsonwspResponse.fault?.string ?? "unknown fault";
    return Effect.fail(new ProviderError({
      message: `Blockfrost evaluation fault: ${faultMessage}`,
      cause: jsonwspResponse
    }));
  }
  // Handle missing result field
  if (!jsonwspResponse.result) {
    return Effect.fail(new ProviderError({
      message: `Blockfrost evaluation returned no result`,
      cause: jsonwspResponse
    }));
  }
  // Check for evaluation failure
  if (jsonwspResponse.result.EvaluationFailure) {
    const failure = jsonwspResponse.result.EvaluationFailure;
    return Effect.fail(new ProviderError({
      message: `Blockfrost script evaluation failed`,
      cause: failure
    }));
  }
  // Handle success case
  const evaluationResult = jsonwspResponse.result.EvaluationResult;
  if (!evaluationResult) {
    return Effect.fail(new ProviderError({
      message: `Blockfrost evaluation returned no result`,
      cause: "No EvaluationResult in response"
    }));
  }
  const result = [];
  for (const [key, budget] of Object.entries(evaluationResult)) {
    // Parse "spend:0", "mint:1", "certificate:0", "withdrawal:0", etc.
    // Blockfrost uses Ogmios v5 JSONWSP which returns "certificate" and "withdrawal";
    // normalize to the SDK's canonical tags "cert" and "reward" (Ogmios v6 / CDDL names).
    const [rawTag, indexStr] = key.split(":");
    const index = parseInt(indexStr, 10);
    const tag = rawTag === "certificate" ? "cert" : rawTag === "withdrawal" ? "reward" : rawTag;
    result.push({
      ex_units: new Redeemer.ExUnits({
        mem: BigInt(budget.memory),
        steps: BigInt(budget.steps)
      }),
      redeemer_index: index,
      redeemer_tag: tag
    });
  }
  return Effect.succeed(result);
};
//# sourceMappingURL=Blockfrost.js.map