'use strict';

require('../../utils/types/array.cjs');
var utils_types_object = require('../../utils/types/object.cjs');
require('crypto');
var utils_inheritance = require('../../utils/inheritance.cjs');
require('bs58');
require('multiformats/bases/base58');
require('varint');
require('../../utils/encoding/ss-58.cjs');
var types_generic_typedEnum = require('./typed-enum.cjs');
var types_generic_typedStruct = require('./typed-struct.cjs');
require('../../utils/types/iterable.cjs');
require('../../utils/types/set.cjs');
require('blake2b');
require('../../utils/types/bytes.cjs');
require('../../utils/interfaces.cjs');
require('../../utils/assert.cjs');
require('../../utils/error.cjs');
require('../../utils/types/string.cjs');
require('./with-base.cjs');
require('./with-catch-null.cjs');
require('./with-eq.cjs');
require('./any-of.cjs');
require('./with-from.cjs');
require('./with-from-json-bytes.cjs');
require('./typed-null.cjs');
require('./create-placeholder.cjs');

/**
 * Extends supplied class which must be a successor of `TypedStruct`/`TypedEnum` by adding/overriding
 * its properties classes.
 *
 * @template C
 * @template K
 * @param {C} klass
 * @param {Object<string, K>} propertiesToOverride
 * @param {function(*, Object<string, K>): *} handleNested
 * @returns {C}
 */
function withProps(
  klass,
  propertiesToOverride,
  handleNested = withProps,
) {
  const fnName = `withProps(${klass.name}, ${JSON.stringify(
    utils_types_object.mapObj(propertiesToOverride, ({ name }) => name),
  )})`;
  const isStruct = utils_inheritance.isEqualToOrPrototypeOf(types_generic_typedStruct, klass);
  const isEnum = utils_inheritance.isEqualToOrPrototypeOf(types_generic_typedEnum, klass);
  if (!isStruct && !isEnum) {
    throw new Error(`Unexpected class provided: \`${klass}\``);
  }

  if (isStruct) {
    const obj = {
      [fnName]: class extends klass {
        static Classes = {
          ...klass.Classes,
          ...propertiesToOverride,
        };
      },
    };

    return obj[fnName];
  } else {
    const obj = {
      [fnName]:
        klass.Class != null
          ? class extends klass {
            static Class = handleNested(klass.Class, propertiesToOverride);
          }
          : class extends klass {
            static Variants = klass.Variants.map((variant) => handleNested(variant, propertiesToOverride));
          },
    };

    return obj[fnName];
  }
}

module.exports = withProps;
//# sourceMappingURL=with-props.cjs.map
