import { Effect as Eff, FastCheck, ParseResult, Schema } from "effect";
import * as Bytes from "./Bytes.js";
import * as CBOR from "./CBOR.js";
import * as KeyHash from "./KeyHash.js";
import * as ScriptHash from "./ScriptHash.js";
/**
 * Credential schema representing either a key hash or script hash
 * credential = [0, addr_keyhash // 1, script_hash]
 * Used to identify ownership of addresses or stake rights
 *
 * @since 2.0.0
 * @category schemas
 */
export const Credential = /*#__PURE__*/Schema.Union(KeyHash.KeyHash, ScriptHash.ScriptHash);
export const makeKeyHash = hash => new KeyHash.KeyHash({
  hash
});
export const makeScriptHash = hash => new ScriptHash.ScriptHash({
  hash
});
/**
 * Convert a Credential to a hex string of its hash.
 *
 * @since 2.0.0
 * @category encoding
 */
export const toHex = credential => Bytes.toHex(credential.hash);
/**
 * Convert a Credential to a bech32 string.
 * Uses CIP-0005 prefixes: `addr_vkh` for KeyHash, `script` for ScriptHash.
 *
 * @since 2.0.0
 * @category encoding
 */
export const toBech32 = credential => {
  switch (credential._tag) {
    case "KeyHash":
      return KeyHash.toBech32(credential);
    case "ScriptHash":
      return ScriptHash.toBech32(credential);
  }
};
/**
 * Check if the given value is a valid Credential
 *
 * @since 2.0.0
 * @category predicates
 */
export const is = /*#__PURE__*/Schema.is(Credential);
export const CDDLSchema = /*#__PURE__*/Schema.Tuple(/*#__PURE__*/Schema.Literal(0n, 1n), Schema.Uint8ArrayFromSelf // hash bytes
);
/**
 * CDDL schema for Credential as defined in the specification:
 * credential = [0, addr_keyhash // 1, script_hash]
 *
 * @since 2.0.0
 * @category schemas
 */
export const FromCDDL = /*#__PURE__*/Schema.transformOrFail(CDDLSchema, /*#__PURE__*/Schema.typeSchema(Credential), {
  strict: true,
  encode: toI => Eff.gen(function* () {
    switch (toI._tag) {
      case "KeyHash":
        {
          return [0n, toI.hash];
        }
      case "ScriptHash":
        {
          return [1n, toI.hash];
        }
    }
  }),
  decode: ([tag, hashBytes]) => Eff.gen(function* () {
    switch (tag) {
      case 0n:
        {
          const keyHash = yield* ParseResult.decode(KeyHash.FromBytes)(hashBytes);
          return keyHash;
        }
      case 1n:
        {
          const scriptHash = yield* ParseResult.decode(ScriptHash.FromBytes)(hashBytes);
          return scriptHash;
        }
    }
  })
});
export const FromCBORBytes = (options = CBOR.CML_DEFAULT_OPTIONS) => Schema.compose(CBOR.FromBytes(options),
// Uint8Array → CBOR
FromCDDL // CBOR → Credential
);
export const FromCBORHex = (options = CBOR.CML_DEFAULT_OPTIONS) => Schema.compose(Schema.Uint8ArrayFromHex,
// string → Uint8Array
FromCBORBytes(options) // Uint8Array → Credential
);
/**
 * FastCheck arbitrary for generating random Credential instances.
 * Randomly selects between generating a KeyHash or ScriptHash credential.
 *
 * @since 2.0.0
 * @category testing
 */
export const arbitrary = /*#__PURE__*/FastCheck.oneof(KeyHash.arbitrary, ScriptHash.arbitrary);
// ============================================================================
// Decoding Functions
// ============================================================================
/**
 * Parse a Credential from CBOR bytes.
 *
 * @since 2.0.0
 * @category parsing
 */
export const fromCBORBytes = (bytes, options) => Schema.decodeSync(FromCBORBytes(options))(bytes);
/**
 * Parse a Credential from CBOR hex string.
 *
 * @since 2.0.0
 * @category parsing
 */
export const fromCBORHex = (hex, options) => Schema.decodeSync(FromCBORHex(options))(hex);
// ============================================================================
// Encoding Functions
// ============================================================================
/**
 * Convert a Credential to CBOR bytes.
 *
 * @since 2.0.0
 * @category encoding
 */
export const toCBORBytes = (credential, options) => Schema.encodeSync(FromCBORBytes(options))(credential);
/**
 * Convert a Credential to CBOR hex string.
 *
 * @since 2.0.0
 * @category encoding
 */
export const toCBORHex = (credential, options) => Schema.encodeSync(FromCBORHex(options))(credential);
//# sourceMappingURL=Credential.js.map