'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var types_did_onchain_typedDid_index = require('../did/onchain/typed-did/index.cjs');
var types_generic_anyOf = require('./any-of.cjs');
var types_generic_typedUuid = require('./typed-uuid.cjs');
var types_generic_withFrom = require('./with-from.cjs');
require('../did/onchain/typed-did/did-method-key/did-method-key-public-key.cjs');
require('multiformats/bases/base58');
require('varint');
require('./typed-enum.cjs');
require('../../utils/types/array.cjs');
require('../../utils/inheritance.cjs');
require('../../utils/types/object.cjs');
require('../../utils/types/iterable.cjs');
require('../../utils/types/set.cjs');
require('crypto');
require('../../utils/interfaces.cjs');
require('../../utils/assert.cjs');
require('../../utils/error.cjs');
require('bs58');
require('../../utils/encoding/ss-58.cjs');
require('blake2b');
require('../../utils/types/bytes.cjs');
require('../../utils/types/string.cjs');
require('./with-base.cjs');
require('./with-catch-null.cjs');
require('./with-eq.cjs');
require('./with-from-json-bytes.cjs');
require('./typed-null.cjs');
require('./create-placeholder.cjs');
require('./typed-bytes.cjs');
require('./typed-string.cjs');
require('./typed-array.cjs');
require('./typed-tuple.cjs');
require('../../utils/encoding/base-58-btc.cjs');
require('./sized.cjs');
require('../../utils/types/number.cjs');
require('./typed-number.cjs');
require('./typed-set.cjs');
require('./typed-map.cjs');
require('./typed-struct.cjs');
require('uuid');
require('js-sha256');
require('./with-qualifier.cjs');
require('../public-keys/public-key.cjs');
require('../public-keys/public-key-x25519-value.cjs');
require('../public-keys/public-key-ed25519-value.cjs');
require('../../vc/crypto/constants.cjs');
require('../public-keys/public-key-secp256k1-value.cjs');
require('../public-keys/public-key-sr25519-value.cjs');
require('../did/onchain/constants.cjs');
require('../did/onchain/typed-did/signature.cjs');
require('../did/onchain/typed-did/did-method-key/did-method-key-signature.cjs');
require('../signatures/signature-value.cjs');
require('../signatures/signature-ed25519-value.cjs');
require('../signatures/signature-secp256k1-value.cjs');
require('../signatures/signature.cjs');
require('../signatures/signature-sr25519-value.cjs');
require('../did/onchain/typed-did/dock-did-value.cjs');
require('./utils.cjs');
require('./typed-bytes-array.cjs');
require('../did/onchain/typed-did/cheqd-did.cjs');

function withFromDockId(klass, fromClass, prefix) {
  return types_generic_withFrom(klass, (value, from) => {
    const parsedValue = types_generic_anyOf(klass, fromClass).from(value);

    return from(
      parsedValue instanceof fromClass
        ? types_generic_typedUuid.fromDockIdent(parsedValue, prefix)
        : parsedValue,
    );
  });
}

function patchWithFromDock(klass, Type, mapping) {
  const { from } = klass;
  const { from: from1 } = klass.Class;

  class FromClass extends klass {
    static from = from;
  }
  class FromClassClass extends klass.Class {
    static from = from1;
  }

  // eslint-disable-next-line no-param-reassign
  klass.from = function fromFn(value) {
    const parsedValue = types_generic_anyOf(FromClass, Type).from(value);

    if (parsedValue instanceof Type) {
      return new this(this.Class.from(parsedValue));
    } else {
      return from.call(this, parsedValue);
    }
  };

  // eslint-disable-next-line no-param-reassign
  klass.Class.from = function fromFn(value) {
    let id = types_generic_anyOf(FromClassClass, Type).from(value);
    if (id instanceof Type) {
      const hexId = id.toEncodedString();
      const dockDid = mapping.mainnet[hexId] ?? mapping.testnet[hexId];
      if (dockDid == null) {
        throw new Error(
          `Dock DID not found for ${hexId} - can't create ${this.name}`,
        );
      }

      id = [types_did_onchain_typedDid_index.DockDid.from(dockDid), id];
    }

    return from1.call(this, id);
  };
}

exports.default = withFromDockId;
exports.patchWithFromDock = patchWithFromDock;
//# sourceMappingURL=with-from-dock-id.cjs.map
