import _extends from "@babel/runtime-corejs3/helpers/extends";
import _Symbol from "@babel/runtime-corejs3/core-js-stable/symbol";
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
var factorySymbol = _Symbol('factory-symbol');
function isProtoFactory(v) {
  return v && v[factorySymbol];
}
function proto(baseRecord) {
  var ensureArray = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'auto';
  var baseKeys = _Object$keys(baseRecord);
  function process(record) {
    var result = _extends({}, record);
    baseKeys.forEach(function (key) {
      var _a;
      if (result[key] === proto.empty) {
        delete result[key];
        return;
      }
      var base = baseRecord[key];
      if (isProtoFactory(base)) {
        result[key] = base(result[key]);
      } else {
        result[key] = (_a = result[key]) !== null && _a !== void 0 ? _a : base;
      }
    });
    return result;
  }
  function factory(arg) {
    var isEnsureArray = ensureArray === 'auto' ? Array.isArray(arg) : ensureArray;
    if (isEnsureArray) {
      if (arg == null) {
        return [];
      }
      return _mapInstanceProperty(arg).call(arg, process);
    } else {
      return process(arg);
    }
  }
  // @ts-ignore
  factory[factorySymbol] = true;
  factory.extends = function (extRecord) {
    var extFactory = proto(extRecord, ensureArray);
    return function (arg) {
      return factory(extFactory(arg));
    };
  };
  return factory;
}
proto.empty = _Symbol('proto.empty');
proto.string = function (v) {
  if (v != null && typeof v !== 'string') {
    throw new Error('must be string');
  }
  return v;
};
proto.string[factorySymbol] = true;
proto.number = function (v) {
  if (v != null && typeof v !== 'number') {
    throw new Error('must be number');
  }
  return v;
};
proto.number[factorySymbol] = true;
proto.notNull = function (v) {
  if (v == null) {
    throw new Error('must be not null');
  }
  return v;
};
proto.notNull[factorySymbol] = true;
proto.object = function (baseRecord) {
  return proto(baseRecord, false);
};
proto.array = function (baseRecord) {
  return proto(baseRecord, true);
};
export default proto;