import { CheqdDIDQualifier, CheqdDIDTestnetQualifier, CheqdDIDMainnetQualifier } from '../constants.js';
import TypedEnum from '../../../generic/typed-enum.js';
import '../../../generic/typed-bytes.js';
import '../../../generic/typed-string.js';
import '../../../generic/typed-array.js';
import TypedTuple from '../../../generic/typed-tuple.js';
import '../../../generic/typed-null.js';
import '../../../../utils/types/array.js';
import 'crypto';
import 'bs58';
import 'multiformats/bases/base58';
import 'varint';
import '../../../../utils/encoding/ss-58.js';
import '../../../generic/typed-number.js';
import '../../../generic/typed-set.js';
import '../../../generic/typed-map.js';
import '../../../generic/typed-struct.js';
import TypedUUID from '../../../generic/typed-uuid.js';
import withQualifier from '../../../generic/with-qualifier.js';
import withFrom from '../../../generic/with-from.js';
import '../../../../utils/types/iterable.js';
import '../../../../utils/types/object.js';
import '../../../../utils/interfaces.js';
import '../../../../utils/assert.js';
import '../../../../utils/error.js';
import '../../../../utils/inheritance.js';
import '../../../../utils/types/set.js';
import '../../../generic/with-base.js';
import '../../../../utils/types/bytes.js';
import '../../../../utils/types/string.js';
import '../../../generic/with-catch-null.js';
import '../../../generic/with-eq.js';
import '../../../generic/any-of.js';
import 'blake2b';
import '../../../generic/with-from-json-bytes.js';
import '../../../generic/create-placeholder.js';
import 'uuid';
import 'js-sha256';

/**
 * `did:cheqd:*`
 */
class CheqdDidValue extends withQualifier(TypedUUID) {
  static Qualifier = CheqdDIDQualifier;

  toEncodedString() {
    return this.value;
  }

  static fromUnqualifiedString(str) {
    return new this(str);
  }
}

class CheqdTestnetDidValue extends CheqdDidValue {
  static Qualifier = CheqdDIDTestnetQualifier;
}

class CheqdMainnetDidValue extends CheqdDidValue {
  static Qualifier = CheqdDIDMainnetQualifier;
}

class CheqdDid extends withQualifier(TypedEnum, true) {
  get Qualifier() {
    return this.Class?.Qualifier;
  }

  static random(network) {
    if (this.Class != null) {
      return new this(this.Class.random());
    } else if (network === 'testnet') {
      // eslint-disable-next-line no-use-before-define
      return CheqdTestnetDid.random();
    } else if (network === 'mainnet') {
      // eslint-disable-next-line no-use-before-define
      return CheqdMainnetDid.random();
    } else {
      throw new Error(
        `Unknown network provided: \`${network}\`, expected \`mainnet\` or \`testnet\``,
      );
    }
  }

  toJSON() {
    return String(this);
  }

  toCheqdPayload() {
    return String(this);
  }
}

class CheqdTestnetDid extends CheqdDid {
  static Class = CheqdTestnetDidValue;

  static Type = 'testnet';
}
class CheqdMainnetDid extends CheqdDid {
  static Class = CheqdMainnetDidValue;

  static Type = 'mainnet';
}

CheqdDid.bindVariants(CheqdTestnetDid, CheqdMainnetDid);

class CheqdDLRRef extends withFrom(
  TypedTuple,
  function from(value, fromFn) {
    if (typeof value === 'string') {
      const resources = value.lastIndexOf('/resources/');

      if (resources === -1) {
        const lastColon = value.lastIndexOf(':');

        return new this(value.slice(0, lastColon), value.slice(lastColon + 1));
      }

      return new this(
        value.slice(0, resources),
        value.slice(resources + '/resources/'.length),
      );
    } else {
      return fromFn(value);
    }
  },
) {
  static Did = CheqdDid;

  static Id = TypedUUID;

  static get Classes() {
    return [this.Did, this.Id];
  }

  toString() {
    return `${this[0]}/resources/${this[1]}`;
  }

  toJSON() {
    return String(this);
  }

  toCheqdPayload() {
    return String(this);
  }
}

export { CheqdDLRRef, CheqdDid, CheqdDidValue, CheqdMainnetDid, CheqdMainnetDidValue, CheqdTestnetDid, CheqdTestnetDidValue };
//# sourceMappingURL=cheqd-did.js.map
