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

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

    return from(
      parsedValue instanceof fromClass
        ? 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 = 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 = 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 = [DockDid.from(dockDid), id];
    }

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

export { withFromDockId as default, patchWithFromDock };
//# sourceMappingURL=with-from-dock-id.js.map
