'use strict';

var types_did_onchain_constants = require('../constants.cjs');
var types_generic_typedEnum = require('../../../generic/typed-enum.cjs');
require('../../../generic/typed-bytes.cjs');
require('../../../generic/typed-string.cjs');
require('../../../generic/typed-array.cjs');
var types_generic_typedTuple = require('../../../generic/typed-tuple.cjs');
require('../../../generic/typed-null.cjs');
require('../../../../utils/types/array.cjs');
require('crypto');
require('bs58');
require('multiformats/bases/base58');
require('varint');
require('../../../../utils/encoding/ss-58.cjs');
require('../../../generic/typed-number.cjs');
require('../../../generic/typed-set.cjs');
require('../../../generic/typed-map.cjs');
require('../../../generic/typed-struct.cjs');
var types_generic_typedUuid = require('../../../generic/typed-uuid.cjs');
var types_generic_withQualifier = require('../../../generic/with-qualifier.cjs');
var types_generic_withFrom = require('../../../generic/with-from.cjs');
require('../../../../utils/types/iterable.cjs');
require('../../../../utils/types/object.cjs');
require('../../../../utils/interfaces.cjs');
require('../../../../utils/assert.cjs');
require('../../../../utils/error.cjs');
require('../../../../utils/inheritance.cjs');
require('../../../../utils/types/set.cjs');
require('../../../generic/with-base.cjs');
require('../../../../utils/types/bytes.cjs');
require('../../../../utils/types/string.cjs');
require('../../../generic/with-catch-null.cjs');
require('../../../generic/with-eq.cjs');
require('../../../generic/any-of.cjs');
require('blake2b');
require('../../../generic/with-from-json-bytes.cjs');
require('../../../generic/create-placeholder.cjs');
require('uuid');
require('js-sha256');

/**
 * `did:cheqd:*`
 */
class CheqdDidValue extends types_generic_withQualifier(types_generic_typedUuid) {
  static Qualifier = types_did_onchain_constants.CheqdDIDQualifier;

  toEncodedString() {
    return this.value;
  }

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

class CheqdTestnetDidValue extends CheqdDidValue {
  static Qualifier = types_did_onchain_constants.CheqdDIDTestnetQualifier;
}

class CheqdMainnetDidValue extends CheqdDidValue {
  static Qualifier = types_did_onchain_constants.CheqdDIDMainnetQualifier;
}

class CheqdDid extends types_generic_withQualifier(types_generic_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 types_generic_withFrom(
  types_generic_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 = types_generic_typedUuid;

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

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

  toJSON() {
    return String(this);
  }

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

exports.CheqdDLRRef = CheqdDLRRef;
exports.CheqdDid = CheqdDid;
exports.CheqdDidValue = CheqdDidValue;
exports.CheqdMainnetDid = CheqdMainnetDid;
exports.CheqdMainnetDidValue = CheqdMainnetDidValue;
exports.CheqdTestnetDid = CheqdTestnetDid;
exports.CheqdTestnetDidValue = CheqdTestnetDidValue;
//# sourceMappingURL=cheqd-did.cjs.map
