import '../../utils/types/array.js';
import 'crypto';
import { NotAConstructor } from '../../utils/interfaces.js';
import 'bs58';
import 'multiformats/bases/base58';
import 'varint';
import '../../utils/encoding/ss-58.js';
import '../../utils/inheritance.js';
import '../../utils/types/object.js';
import '../../utils/types/iterable.js';
import '../../utils/types/set.js';
import '../../utils/assert.js';
import '../../utils/error.js';
import 'blake2b';
import '../../utils/types/bytes.js';
import '../../utils/types/string.js';

/**
 * Extends `fromApi`/`fromJSON`/`from` methods of the provided class to check for the `null`'ish (`None`) values.
 * In case `class.from(null)` is called, returns `null` instead of throwing an error.
 *
 * @template C
 * @param {C} klass
 * @returns {C}
 */
/* eslint-disable sonarjs/cognitive-complexity */
function option(klass) {
  const name = `Option<${klass.name}>`;

  if (klass[NotAConstructor]) {
    const obj = {
      [name](...args) {
        return klass.apply(this, args);
      },
    };

    obj[name].fromApi = function fromApi(valueOrValueOpt) {
      if (valueOrValueOpt == null || valueOrValueOpt.isNone) {
        return null;
      } else if (valueOrValueOpt.isSome) {
        return klass.fromApi(valueOrValueOpt.unwrap());
      } else {
        return klass.fromApi(valueOrValueOpt);
      }
    };

    obj[name].from = function from(valueOrValueOpt) {
      if (valueOrValueOpt == null) {
        return null;
      } else if (valueOrValueOpt.isSome != null) {
        return this.fromApi(valueOrValueOpt);
      } else {
        return klass.from(valueOrValueOpt);
      }
    };

    obj[name].fromJSON = function fromJSON(valueOrNull) {
      if (valueOrNull == null) {
        return null;
      } else {
        return klass.fromJSON(valueOrNull);
      }
    };

    return obj[name];
  }

  const obj = {
    [name]: class extends klass {
      static fromApi(valueOrValueOpt) {
        if (valueOrValueOpt == null || valueOrValueOpt.isNone) {
          return null;
        } else if (valueOrValueOpt.isSome) {
          return super.fromApi(valueOrValueOpt.unwrap());
        } else {
          return super.fromApi(valueOrValueOpt);
        }
      }

      static from(valueOrValueOpt) {
        if (valueOrValueOpt == null) {
          return null;
        } else if (valueOrValueOpt.isSome != null) {
          return this.fromApi(valueOrValueOpt);
        } else {
          return super.from(valueOrValueOpt);
        }
      }

      static fromJSON(valueOrNull) {
        if (valueOrNull == null) {
          return null;
        } else {
          return super.fromJSON(valueOrNull);
        }
      }
    },
  };

  return obj[name];
}

export { option as default };
//# sourceMappingURL=option.js.map
